Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time

In our First Task we are going to make use of the Simulator to read from a Sensor

Simulator: Starting Point

#include "mbed.h"
#include "mbed_events.h"
#include "Sht31.h"

// An EventQueue is a very useful construct in Mbed OS, it allows you to schedule events
// and to defer from one context to another (e.g. from ISR to normal thread) without
// writing your own state machines, and while maintaining context.
// https://os.mbed.com/docs/v5.6/tutorials/the-eventqueue-api.html
EventQueue queue;

DigitalOut led1(LED1);
DigitalOut led2(LED2);

InterruptIn btn(BUTTON1);

//Setup the Sensor
Sht31 sht31(I2C_SDA, I2C_SCL);

void blink_led() {
    led1 = !led1;
        float temp = sht31.readTemperature ();
    float humidity = sht31.readHumidity();
    printf("Temp  %.2f  Humidity  %.2f\n", temp, humidity);
}

// This does not run in an ISR, so it's safe to use `printf` or other blocking calls
void btn_handler() {
    printf("Button Pressed\n");

    led2 = !led2;
}

int main() {
    // Schedule an event to run every second
    queue.call_every(1000, &blink_led);

    // Normally code in the `fall` handler runs in an ISR,
    // but you can directly defer it to the thread that runs the queue
    btn.rise(queue.event(&btn_handler));

    // Because the simulator does not support multiple threads,
    // we have to call dispatch_forever from the main thread.
    // Typically you'd run this on a separate thread within Mbed's RTOS.
    queue.dispatch_forever();
}

Simulator Task:

  • Get the Sensor to read and print the temperature value when the button is pressed
  • Store the Periodic Readings in Global Variables (no threads so no need to lock)
  • Create a new event that runs every 0.5 Seconds
    • Check the Temperature reading, and do something if it is above 30C
    • Check the Temperature reading, and get an LED to blink if it is below 5C (you will need to trigger a new event)

Using the On Board Accelerometer:

The Discovery board has an On board accelerometer.

To get this working we first need to install the relevant Libaries to platform IO

  • In the Homepage
    • Libaries window
    • Search and Install the relevant Libary

Accelerometr: Starting point

#include "mbed.h"
#include "mbed_events.h"
#include "LIS3DSH.h"

//Leds for Debugging
DigitalOut led1(LED1);
DigitalOut led2(LED2);

//Hopefully Serial
Serial uart(PC_6,PC_7);

//Setup Accelerometr
LIS3DSH acc(PA_7, PA_6, PA_5, PE_3);

//Thread for our event loop 
Thread eventThread;
//Event Loop itself
EventQueue theQueue(32 * EVENTS_EVENT_SIZE);

//Function to read the values
void readValues(){
    led2 = !led2;
    int16_t X, Y, Z;
    acc.ReadData(&X, &Y, &Z);           //read X, Y, Z values
    uart.printf("X: %d Y: %d Z: %d", X, Y, Z);
}

int main(){
    //Hopefully
    uart.baud(9600);
    uart.printf("Testing\n");

    //And setup event Queue
    eventThread.start(callback(&theQueue, &EventQueue::dispatch_forever));
    theQueue.call_every(1000, &readValues);

    while(1){
        //Standard Blinking, Let me know things are alive
        led1 = !led1;
        wait(1.0);
    }
}

TASK

  • Using the Previous task as a Guide
    • Take a reading every 0.25 Seconds
    • Store the readings in a relevant object
    • Check the Values of the X and Y Axis every second
      • Light the Relevant LED's to show which way the board is facing
    • Print a Debug message Every Second.