Page 1 of 1

Using SenseHAT from C

Posted: Mon Mar 07, 2016 1:52 pm
by Linkan
Hi,

How do I access the sense hat using only C? I looked in the snake example written in C but I need to know how I can access the humidity/barometer/temperature sensors.

I'd be very grateful if you anyone could provide me with an example in C on how I can fetch humidity level for example. :D

Re: Using SenseHAT from C

Posted: Mon Mar 07, 2016 2:10 pm
by scotty101
The Sense hat makes use of the RTIMU library which is written for C++.

https://github.com/richards-tech/RTIMULib

You might be able to write a C wrapper for this code.

Re: Using SenseHAT from C

Posted: Mon Mar 07, 2016 2:22 pm
by experix
I made example programs that operate the LED matrix; get joystick input; and monitor temperature reported by the pressure sensor, temperature/humidity sensor, accelerometer and CPU. Find it in https://experix.sourceforge.net. Click Dowload source files and information, select sense-hat, download the archive and follow directions in the README (which is displayed under the file list). This is as 'basic' as you can get, not using RTIMULib; I'm trying to start with fundamentals and go up from there.

Re: Using SenseHAT from C

Posted: Wed Mar 09, 2016 9:31 pm
by Linkan
experix wrote:I made example programs that operate the LED matrix; get joystick input; and monitor temperature reported by the pressure sensor, temperature/humidity sensor, accelerometer and CPU. Find it in https://experix.sourceforge.net. Click Dowload source files and information, select sense-hat, download the archive and follow directions in the README (which is displayed under the file list). This is as 'basic' as you can get, not using RTIMULib; I'm trying to start with fundamentals and go up from there.
Thanks for the example code. I understood from the code that temperature.c shows the temperatures provided from the pressure sensor and temperature/humidity sensor but how can I obtain the pressure (in mbars) and humidity (as a percentage)?

Would this code work?

Code: Select all

    // get a LPS25H pressure sample
    res = ioctl( i2c, I2C_SLAVE, LPS25H_SAD );
    buf[0] = LPS25H_STATUS_REG;
    res = write( i2c, buf, 1 );
    res = read( i2c, buf, 2 );
    LPS25H_status = buf[0];
    buf[0] = LPS25H_FIFO_STATUS;
    res = write( i2c, buf, 1 );
    res = read( i2c, buf, 2 );
    LPS25H_fifo_status = buf[0];
    buf[0] = LPS25H_PRESS_POUT | LPS25H_reg_auto;
    res = write( i2c, buf, 1 );
    res = read( i2c, buf, 2 );
    LPS25Hd16[ii] = ((__s16 *)buf)[0];

Re: Using SenseHAT from C

Posted: Thu Mar 10, 2016 2:41 pm
by experix
I am taking little baby steps with this and trying to explore all aspects of the problem. Consequently I have not yet gotten around to the humidity and pressure. With the help of the datasheet you should be able to adapt my code to read those things. My equipment is packed in a moving pod and it will be several months before I begin to work on this again.

Re: Using SenseHAT from C

Posted: Thu Mar 10, 2016 3:01 pm
by Linkan
experix wrote:I am taking little baby steps with this and trying to explore all aspects of the problem. Consequently I have not yet gotten around to the humidity and pressure. With the help of the datasheet you should be able to adapt my code to read those things. My equipment is packed in a moving pod and it will be several months before I begin to work on this again.
Okay, I will see what I can do. I'm certainly not an expert in this area but perhaps I can look at the source code of [this](https://github.com/RPi-Distro/RTIMULib) fork of RTIMULib.

Re: Using SenseHAT from C

Posted: Fri Apr 01, 2016 1:45 pm
by Linkan
basicsensor.tar.gz
(5.41 KiB) Downloaded 586 times
For any curious future readers, I did get it working based on experix code. See attachment for details. Note I tried minimizing this as much as possible so it isn't necessarily complete.

Re: Using SenseHAT from C

Posted: Thu Apr 04, 2019 5:24 am
by John_Spikowski
Can I use the Sense Hat emuator with your basicsensors.c example?