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 Ian MacArthur Oct 23, 2020  
 
I meant to post a worked example of the polling approach. But then I forgot...

Here is it anyway - I have commented out the R.Pi specific parts, and stubbed the read of the ADC, but it shows the basics of the timer polling approach...

With the ADC stuff added back in (and my dummy counter removed) this ought to pretty much work on the R.Pi, though you will need to link against wiringPi of course, as well as the fltk lib.

Something like this might do the trick to compile it:

    g++ `fltk-config --cxxflags` app.cxx -o app -lwiringPi `fltk-config --ldstaticflags`

// Start of file

#include <FL/Fl.H>
#include <FL/Fl_Double_Window.H>
#include <FL/Fl_Value_Output.H>
#include <FL/Fl_Button.H>

// You would need these headers...
// #include <wiringPi.h>
// #include <mcp3004.h>

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

static Fl_Double_Window *main_win = NULL;
static Fl_Value_Output *adc_out = NULL;

static int get_adc (void)
{
    // This is where the ADC read would go...
    //return analogRead (BASE);

    // for now, here's a dummy test stub - reomve this!
    static int dummy = 0;
    ++dummy;
    if (dummy > 255) dummy = 0;
    return dummy;
} // get_adc

// animate the meter for testing purposes
static void poll_adc (void *)
{
    static int old_level = (-1);

    int level = get_adc ();

    if (level != old_level)
    {
        old_level = level;
        adc_out->value (old_level);
        adc_out->redraw();
    }

    Fl::repeat_timeout(0.1, poll_adc);
} // poll_adc

static void cb_Exit (void *)
{
    main_win->hide();
} // cb_Exit

int main (int argc, char **argv)
{

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

    main_win = new Fl_Double_Window(247, 130);
    main_win->begin();

    adc_out = new Fl_Value_Output(100, 22, 66, 33, "ADC READ :");
    adc_out->box(FL_THIN_DOWN_BOX);
    adc_out->color(FL_WHITE);
    adc_out->textfont(4);
    adc_out->textsize(18);

    Fl_Button* o = new Fl_Button(175, 80, 60, 25, "Exit");
    o->box(FL_THIN_UP_BOX);
    o->callback((Fl_Callback*)cb_Exit);

    main_win->end();
    main_win->show(argc, argv);

    Fl::add_timeout(0.25, poll_adc);

    return Fl::run();
} // main

// end of file





-- 
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/86F89B84-CF9B-439B-90C7-560ADD925F17%40gmail.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'.