User avatar
mike808
Posts: 21
Joined: Mon Jun 03, 2013 12:55 am

registers on pi?

Mon Jun 03, 2013 1:10 am

I just got my pi and am excited to get started. I was wondering if there are any registers or eeprom on the pi I can access from C++? I woud like to build a program to read data from a temperature sensor but have the read time (aka polling time) be dynamically configured while my C++ program is running. Here is what i want to do in pseudocode.

Code: Select all

int main ()
{
   volatile int seconds_to_sleep = 1;
   write_to_register (seconds_to_sleep);  // <-- how do i do this?
   while (1)
   {
       TemperatureData *tempData = read_data_from_temp_sensor ();
       cout << "Temperature is " << tempData->fahrenheight() << "F" << endl;
       sleep (seconds_to_sleep * 1000); // sleep
   }
}
Essentially, I'd like to make the reading of the temperature data dynamic. I would like to have a button or potentiometer to control that value. When button is pressed or potentiometer is used, it would write a new value to the register and thus cause the read to be faster or slower. Can anyone offer ideas or point me to an example please?

Aloha,
Mike

User avatar
joan
Posts: 14274
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: registers on pi?

Mon Jun 03, 2013 7:55 am

Just store the value in a file on the sd card. The sd card is just like a disc drive.

There isn't any accessible permanent storage on the board itself.

User avatar
rpdom
Posts: 15242
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: registers on pi?

Mon Jun 03, 2013 8:30 am

mike808 wrote:I just got my pi and am excited to get started. I was wondering if there are any registers or eeprom on the pi I can access from C++?
You seem to be approaching this problem as if the Pi were a microcontroller like a PIC or Arduino. It isn't. It is a full computer running a proper multi-tasking OS.

There are a number of ways for programs to communicate with each other. One is to write a file on the card, but the other program will have to keep checking that file to see if it has changed. There are various ways of handling this, including shared memory (not used so much nowadays, I believe) and inter-process communication (some setting up involved). But for this situation I would probably just use a signal (USR1 would be a good choice).

Very rough pseudocode here

Code: Select all

/* Program 1 - monitor sensor */
    read file to get current interval
    set up handler() to trap signal USR1
    {
        do stuff here
        wait( pauselength)
    }

handler() {
    read file to get new interval
}

/* Program 2 - monitor up/down button presses */
    read file to get current interval
    b1 = get_button1_state()
    b2 = get_button2_state()
    {
        b1a = get_button1_state()
        if  b1a != b1 {
            b1 = b1a
            if b1 = 1 ;# Button has been pressed
            {
                interval++
                write file with updated interval
                send kill -USR1 to program1 (if running) to force re-read of file
            }
       }
       same for button 2, but with interval--
    }
Rubbish coding, but I hope you get the idea

User avatar
gordon@drogon.net
Posts: 2020
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website

Re: registers on pi?

Mon Jun 03, 2013 8:34 am

If you're running under Linux, then you need to stop thinking microcontrollers and start thinking Linux. (Or Unix)

There are many inter-process communication channels you can use for example.

So you write your program that reads the temperature sensor, stores it to disk (ie. sd card), then sleeps for the duration. A second program can send a message to the first by a variety of means - it could write a file, then send a signal to the first program which reads the value from the file (this is a very common way to get system programs to re-read their configuration file), or you can open a socket and listen to it - then the program could run anywhere - on the same Pi, on another Pi, etc. You could use shared-memory where one program reads it and another writes it. And so on.

Or you could use one of the many GPIO access libraries (e.g. http://wiringpi.com/ and read the GPIO pins which you connect to switches)

You have many options... Maybe too many!

-Gordon

Ps. the sleep() system call sleeps for whole seconds - if you multiply it by 1000, then its going to wait for a very long time indeed.
--
Gordons projects: https://projects.drogon.net/

User avatar
mike808
Posts: 21
Joined: Mon Jun 03, 2013 12:55 am

Re: registers on pi?

Tue Jun 04, 2013 12:55 am

thanks everyone for the feedback. you are all right, need to stop thinking microcontrollers!

@rpdom - i ended up doing something similar to what you suggested (using SIGUSR1)

@gordon - i'm going to install your wiring library next and play with the GPIO as i wanted to hook up external devices. i also realized about the sleep() call... too much java! haha.

User avatar
gordon@drogon.net
Posts: 2020
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website

Re: registers on pi?

Tue Jun 04, 2013 1:56 pm

mike808 wrote:thanks everyone for the feedback. you are all right, need to stop thinking microcontrollers!

@rpdom - i ended up doing something similar to what you suggested (using SIGUSR1)

@gordon - i'm going to install your wiring library next and play with the GPIO as i wanted to hook up external devices. i also realized about the sleep() call... too much java! haha.
wiringPi has a delay() function that delays in mS and delayMicroseconds() - which ought to be obvious!

However in shell scripts the sleep command takes fractions of a second - sleep 0.5 is perfectly valid. So much for consistency... (it never used to be that way!)

-Gordon
--
Gordons projects: https://projects.drogon.net/

Return to “C/C++”