FLTK logo

Re: [fltk.general] Read Signal from ADC on Raspberry Pi with FLTK

FLTK matrix user chat room
(using Element browser app)   FLTK gitter user chat room   GitHub FLTK Project   FLTK News RSS Feed  
  FLTK Apps      FLTK Library      Forums      Links     Login 
 All Forums  |  Back to fltk.general  ]
 
Previous Message ]New Message | Reply ]Next Message ]

Re: Read Signal from ADC on Raspberry Pi with FLTK LosCelos Oct 21, 2020  
  before i come to this step, i think i should describe a earlier problem:

my ADC (MCP3008) is on a development board with some other hardware on it for example the potentiometer (which is not changeable). The hole board is  connected with the Raspberry Pi with a ribbon cable.
 
i used this programm to read values from the adc:

#include <stdio.h>
#include <wiringPi.h>
#include <mcp3004.h>

#define BASE 100
#define SPI_CHAN 0
#define chan0 0 /* Poti */
#define chan1 1 /* LDR */

int main(void){

int wert;

// Setup wiringPi and mcp3004
wiringPiSetup(); 
mcp3004Setup(BASE, SPI_CHAN);

while(1){

wert=analogRead(BASE);
printf(" %d\n",wert);
delay(2000);
return 0; 
}

it just works with a make-file.
i tried to put this code in my fltk App. The includes dousnt throw a error or warning but the usage of it like wiringPiSetup(); or mcp3004Setup(BASE, SPI_CHAN); 
throw : undefined reference to wiringPiSetup, mcp3004Setup, analogRead, delay

so i dont know how i should read the data from the ADC in my FLTK App.
How i use this API with fltk or should i take another way ? 

the goal is that every sampled data should shown as instant as possible in the fltk scope. For example the adc samples the value 150 from the poti and this value should be availabe for the fltk App to draw it, and so on.


i hope i could describe my problem better,

Thank you very much for your help!
Greg Ercolano schrieb am Dienstag, 20. Oktober 2020 um 16:36:49 UTC+2:
On 2020-10-20 03:24, Ian MacArthur wrote:
> So, if you are comfortable with threads and such, that should be pretty
> straightforward. I you are not comfortable with threads... then this might
> be too complex a solution (sorry!)

A possibly easier solution would just be to call fork() to start
a separate process, then the child can handle sending data to the
"Oszilloskop-App" as a realtime process, and the FLTK app can just
kill the process ID to stop it.

So if the FLTK app is simply a Start and Stop button for starting
the data hose to the Oszilloskop-App, then the start button starts
the child process to feed data to the scope, and stop kills the child
to stop it, e.g.

#include <unistd.h> // fork
#include <sys/types.h> // kill()
#include <signal.h> // kill()
..
pid_t G_child = 0;

// Your start button callback
void start_callback() {
if ( (G_child = fork()) == 0 ) {
// We are the child.. forever loop feed data to scope app
while ( 1 ) {
val = read_adc(); // whatever code reads the ADC
write_scope(val); // whatever code writes to the "Oszilloskop-App"
usleep(100); // yield some cpu to the rest of the machine for the scope app to run
}
}
}

// Your stop button callback
void stop_callback() {
if ( G_child == 0 ) return; // child already stopped? early exit
kill(G_child, 9); // stop child
if ( waitpid(G_child, NULL, 0); // wait for child to stop
, G_child = 0;
}

Depending on how "realtime" the data flow needs to be, you can use linux's
realtime API to adjust the child process to be unaffected by some system
hiccups, e.g. http://www.isy.liu.se/edu/kurs/TSEA81/lecture_linux_realtime.html

--
You received this message because you are subscribed to the Google Groups "fltk.general" group.
To unsubscribe from this group and stop receiving emails from it, send an email to fltkgeneral+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/fltkgeneral/b49df53a-25ad-414d-9910-184d3965d675n%40googlegroups.com.
Direct Link to Message ]
 
     
Previous Message ]New Message | Reply ]Next Message ]
 
 

Comments are owned by the poster. All other content is copyright 1998-2024 by Bill Spitzak and others. This project is hosted by The FLTK Team. Please report site problems to 'erco@seriss.com'.