Skip to content

Yet another Raspberry pi + SenseHat library for C++ language

License

Notifications You must be signed in to change notification settings

platu/libsensehat-cpp

Repository files navigation

Yet another Raspberry Pi + Sense HAT library in C/C++ programming language

Foreword

This repository aims to resume the Python Sense HAT API in C/C++ programming language. The functions provided by this library are intended for students who are taking their first steps in programming. Therefore, we use a very small subset of C++ programming language.

  • No classes. Okaaaayyy! I know. Don't slap me, even virtually.
  • Typed input/output through iostream. Almost avoids burdens of C stdio formatting.
  • Use of IMU RTIMULib library already written in C++. Much more convenient to get magnetic field measures from LSM9DS1 registers.
  • Use of libgpiod for GPIO functions.
  • Use of sysfs for PWM0 and PWM1 output channels.

The code in this repository has started as a compilation from different other repositories. It then evolved with the addition of GPIO and PWM functions to become a full-fledged library. Many thanks are due to the developers of the following projects:

Install and build

Open a terminal on your Raspberry Pi.

  1. Your Raspberry Pi user account must belong to a few system groups to access hardware devices and install the library file once it is compiled.

The result of the id command below shows the user account belongs to the required system groups.

id | egrep -o '(input|i2c|gpio|spi|sudo|video)'
sudo
input
gpio
i2c
spi
video

Check that the sense-hat packages are already there.

apt search sense-hat | grep install
python3-sense-hat/stable,stable,now 2.6.0-1 all  [installé, automatique]
sense-hat/stable,stable,now 1.4 all  [installé]
  1. Install development library packages
sudo apt install libi2c-dev libpng-dev libgpiod-dev
  1. Clone this repository
git clone https://github.com/platu/libsensehat-cpp.git
  1. Build the library and compile the example programs
cd libsensehat-cpp/ && make

Depending on the number of example programs, compilation may take some time.

You're done ! It is now time to open example files and run your own tests. There is a generic Makefile in the examples directory that you can copy and adapt to your needs.

./examples/01_setRGB565pixel
Settings file RTIMULib.ini loaded
Using fusion algorithm RTQF
Sense Hat led matrix points to device /dev/fb0
8x8, 16bpp
IMU is opening
Using min/max compass calibration
Using ellipsoid compass calibration
Accel calibration not in use
LSM9DS1 init complete
Joystick points to device event2
-------------------------------
Sense Hat initialization Ok.
  [ f800 ]   [ fc00 ]   [ ffe0 ]   [ 7e0 ]   [ 7ff ]   [ 1f ]   [ f81f ]   [ fc10 ] 
Waiting for keypress.
-------------------------------
Sense Hat shut down.

Example programs

Almost every function has its own example program that illustrates how it works. Source file numbering in the directory named examples designates the category of functions. Here is a list of these categories:

  • 01 Get or set a single pixel
  • 02 Get or set all pixels
  • 03 Flip or rotate all pixels
  • 04 Display a character or scroll a message
  • 05 HTS221 Humidity sensor and LPS25H Pressure sensor
  • 06 LSM9DS1 IMU Orientation and compass
  • 07 Joystick events
  • 08 GPIO read input or write output on Raspberry Pi pins subset
  • 09 2 PWM channels
  • 10 Color detection based on TCS34725 (work in progress ...)
  • 11 Console keyboard events routines

Library addons

Once the Sense HAT standard header is replaced by a stacking header, GPIO and PWM pins are available.