Skip to content

moshegottlieb/libsense

Repository files navigation

libsense logo

C/C++ CI

Sense HAT for the Raspberry Pi C/C++ API

I don't know about you guys, but I'm not much of a python guy.
While I can code in just about any programming language - I tend to stick to compiled languages.
Anyway, everything is written in C and wrapped in some other language binding. Right? that's the only way to make a cross-language project.

When I got my Sense HAT for my (4th? 5th? dunno) Raspberry Pi - I was surprised to see it only came with python API wrappers.
It even comes with an emulator which is very cool, however, it didn't feel right to me.
That's when libsense was born.
It currently covers the framebuffer only but I promise to add support for the gyroscope, accelerometer, magnetometer and temperature, barometric pressure and humidity sensors.
Find everything you need in fb.h or fb.hpp for the C++ flavor.

Install

Depending on your setup, you may want to allow the current user access to the devices, otherwise you'd need root access to run apps linked to libsense.
In Raspbian:
sudo usermod -aG input,video,i2c {your username here} Or change the permissions (not recommended) of:
/dev/fb1, /dev/i2c-1, /dev/input/event0
Currently, since libsense only works with the LED matrix (as a framebuffer), there are no dependencies.
However, future versions will require i2c, in Raspbian:
sudo apt-get install libi2c-dev and also: sudo raspi-config --> advanced options --> enable i2c

That said, the build process is dead simple:

make
sudo make install

Then from your C files:

#include <sense/fb.h>

Or from your C++ files (can use the C version too, of course):

#include <sense/fb.hpp>

Link with -lsense.

Documentation

doxygen docs available as well, to build just type:
make doc
HTML Documentation will be available in the docs/html folder.

Examples

See the examples/ directory.
Type:
make to build examples, sensepaint,senseimage,senserainbow and sensetext.
Built binaries are placed in the build directory.
Both sensepaint and senseimage require the Image Wand library (sudo apt-get install libmagickwand-dev on Raspbian and probably Ubuntu), it's a part of the Image Magick project.
Also, if you plan to use clang instead of gcc, make sure to install the openmp libraries (required by Image Magick):
sudo apt install libomp-dev
Once built, try something like:
build/sensepaint green or build/sensepaint "#ffbb00" to paint all LEDs.
build/senseimage sense_logo.png will draw the logo on the LED matrix.
The later will resize any image to 8x8 and draw it on the matrix.
Don't expect your DSLR camera photos to look very good using this method.
How about writing something?
Luckily, Jared Sanson has a nice collection of 6x8 fonts exactly for this purpose (well, I suppose not exactly for this purpose).
It's available here and it's used by the sensetext example.
Just type something like: build/sensetext "hello world!" and enjoy the magic of text printing out on your 8x8 LED matrix.
I do not recommend on reading Ulysses this way.

New example OpenWeatherMap

Shows the temperature on the sensehat LED matrix.
Supports scrolling for long figures (-10 and colder) and color coding, becomes bluer as it gets colder, and more red as it's hotter.
You want it to be nice and green :-).
It currently takes the temperature value from a small ESP8266 project I have (will github it soon) that simply reads the temperature upon request to it's webserver, but there's also a provider interface, just edit the code and use a different REST API provider to fetch the weather on your own.
A darksky API sample is available, but you can't sign up for a new API key, after they were bought by Apple, but it should get you started on using a different provider.
Just use your own URL and change the code that fetches the data from the JSON response.
...oh yeh, the temp is in celsius, I live in europe.
To switch to Fahrenheit, use F = C * 1.8 + 32.
You may want to do it after it figures out the color coding, as the color coding thresholds are set to celsius.
To install it as a systemd daeomn:

  1. make && sudo make install
  2. Inside examples, run make
  3. Copy build/openweathermap to /usr/local/bin/openweatheramp.
  4. Copy openweathermap/openweathermap.service to /etc/systemd/system/
  5. Install the service: sudo systemctl enable openweathermap
  6. Run it (it will start on boot otherwise): sudo service openweathermap start

Why not use the built in sensehat temp sensor(s)? Because there's a board, LEDs and a CPU right next to it, and those generate a lot of heat.
It may work reasonably in space, but not on earth :-)


C++

Writing shared libraries in C is easy.
The rules for when to bump up the so name is simple and clear.
When dealing with C++ - that's not the case.
Sorry for all the warnings about specifying exception type, should port it to modern C++ at some point.
The list of things that break binary compatibility is very long, and it's super easy to break.
Since the C++ code is just a wrapper - I decided to include it as an inline header, that way the shared library doesn't have any C++ issues.

Contributing to libsense

Yes please! see the guideline first.