Running C/C++ using Mbed on the BrainPad TechTalk - 046

Using the ARM GCC Cross Compiler in Visual Studio (Vers. 15.5.0 Prview 2.0), working with Mbed and C++ got more comfortable.

Using an ST-Link adapter and the STM32 ST-LINK Utility makes deploying quite easy

Here is a simple example for the BrainPad using LEDs, Button, Buzzer, Display, Light/Temperatur Sensors and Accelerometer.
The following Mbed libraries are used:
Adafruit_GFX by Neal Horman -Adafruit_GFX - A derived version of the BSD licensed Adafrut GFX… | Mbed
MMA8453 by Givanni Bruno -MMA8453 - Accelerometer library (simple) | Mbed
beep by Peter Drescher -beep - Make noise with a piezo buzzer. Use a pwm pin. | Mbed

In the VS Project folder CppProperties.json must be:
    {
      "configurations": [
        {
          "inheritEnvironments": [
            "gcc_arm"
          ],
              "name": "gccarm",
          "includePath": [
            "${env.INCLUDE}"
          ],
            "defines": [
            "DEVICE_I2C",
            "DEVICE_SERIAL",
        "DEVICE_ANALOGIN",       
      ]
    }
  ]
}

Here is the Example Code for the Brain:

#include "mbed.h"
#include "Adafruit_GFX/Adafruit_GFX.h"
#include "Adafruit_GFX/Adafruit_GFX_Config.h"
#include "Adafruit_GFX/Adafruit_SSD1306.h"
#include "MMA8453/MMA8453.h"
#include "beep/beep.h"

#define SSD1306_I2C_ADDR (0x78) // SSD1306 I2C Address

DigitalOut myBlueLed(PC_6, 0);
DigitalOut myRedLed(PC_9, 0);
DigitalOut myGreenLed(PC_8, 0);

DigitalIn u_Button(PA_5, PullUp);

AnalogIn LightSensor(PB_1);
AnalogIn TempSensor(PB_0);

Serial pc(SERIAL_TX, SERIAL_RX);

I2C gI2C(PB_7, PB_6);

MMA8453 accel(PB_7, PB_6);

Adafruit_SSD1306_I2c gOled(gI2C, PC_8, SSD1306_I2C_ADDR, 64, 128);

Beep myBuzzer(PB_8);

int main()
{
    myGreenLed = 0;

    wait(4.0);
    gOled.clearDisplay();
    gOled.setTextCursor(0, 0);
    gOled.printf("Hallo BrainPad\r\nU-Button beeps");
    gOled.display();

    myBuzzer.beep(440.0, 0.5);
    wait(4.0);
    
    while (1) {
        gOled.clearDisplay();
        gOled.setTextCursor(0, 0);
                
        gOled.printf("X: %f\r\n", (accel.getX() * 100));
        gOled.printf("Y: %f\r\n", (accel.getY() * 100));
        gOled.printf("Z: %f\r\n\r\n", (accel.getZ() * 100));

        uint32_t reading = LightSensor.read_u16();
        // some arbitrary calculations
        float lightLevel = (reading - 2000) / (float)700;
        gOled.printf("L: %.2f\r\n", lightLevel);        

        reading = TempSensor.read_u16();        
        // The scaling is probably not correct
        float tempLevel = (reading - 2000) / (float)700 ;
        gOled.printf("T: %.2f\r\n", tempLevel);
        gOled.display();
     
        if (u_Button.read() == 0) {
            myRedLed = 0;
            myBlueLed = !myBlueLed;
            myBuzzer.beep(440.0, 0.5);
        }
        else {
            myBlueLed = 0;
            myRedLed = !myRedLed;		
        }
         wait(2.0);      
    }

}

4 Likes