Dan Goldsmith
void main() in our code
int main() {
// put your setup code here, to run once:
while(1) {
ledOne = 1;
ledTwo = 1;
thread_sleep_for(500);
ledOne = 0;
ledTwo = 0;
thread_sleep_for(500);
}
}
while(1){
if (State == 0){
// Do State 0 Things
}
else if (State == 1){
// Do State 1 Things
}
}
while(1) {
if buttonPressed {
...Do Something
}
}
What Happens Here
#include "mbed.h"
DigitalOut ledOne(LED1);
DigitalIn theButton(BUTTON1);
int main() {
//Loop
while(1){
if (theButton){
ledOne = !ledOne;
}
thread_sleep_for(1000);
}
}
Or with a sensor that is IO Bound
#include "mbed.h"
DigitalOut ledOne(LED1);
DigitalIn theKeyboard();
int main() {
//Loop
while(1){
//Set value to be current keyboard press
out = theKeyboard.press()
ledOne = !ledOne;
thread_sleep_for(1000);
}
}
InterruptIn button(BUTTON1)
//Function called when button is pressed.
void handler(){
... //Do Something
}
int main(){
//Attach interrupt
button.rise(&handler);
}
int temp; //Global
function swap(int x, int y){
temp = x;
x = y;
y = temp;
}
function swap(int x, int y){
int temp; //Local
temp = x;
x = y;
y = temp;
}
volatile int value;
attach (Callback< void()> func, float t)
Ticker theticker;
DigitalOut led1(LED1);
DigitalOut led2(LED2);
void handler() {
led2 = !led2;
}
int main() {
theticker.attach(&handler, 1.0); // call flip function every 1 seconds
// spin in a main loop. ticker will interrupt it to call handler
while(1) {
led1 = !led1;
thread_sleep_for(500);
}
}
void handler() {
for (int x=0; x<5; x++){
led2 = !led2;
thread_sleep_for(100);
}
}
DigitalOut led1(LED1);
DigitalOut led2(LED2);
DigitalOut led3(LED3);
InterruptIn btn(BUTTON1);
Ticker theticker;
void buttonHander(){
printf("BUTTON PRESSED")
led3 = !led3;
}
void handler(){
for (int x=0; x<10; x++){
led2 = !led2;
thread_sleep_for(250);
}
}
int main() {
btn.rise(&buttonHandler);
theticker.attach(&hander, 5.0);
while (1){
led1 = !led1;
thread_sleep_for(500);
}
}
#include "mbed.h"
// Create a queue that can hold a maximum of 32 events
EventQueue queue(32 * EVENTS_EVENT_SIZE);
// Create a thread that'll run the event queue's dispatch function
Thread t;
int main () {
// Start the event queue's dispatch thread
t.start(callback(&queue, &EventQueue::dispatch_forever));
}
We may also need to enable threading in our platformio.ini configuration file
[env:nucleo_f401re]
platform = ststm32
board = nucleo_f401re
framework = mbed
build_flags = -D PIO_FRAMEWORK_MBED_RTOS_PRESENT
//Any interrupt object here can post
button.rise(queue.event(handler));
void ledEvent(){
//Add a new event to toggle the LED
queue.event(ledThreeEvent);
printf("Event Called");
for (int x=0; x<10; x++){
led2 = !led2;
thread_sleep_for(100);
}
}
#include "mbed.h"
DigitalOut led1(LED1);
DigitalOut led2(LED2);
DigitalOut led3(LED3);
//Event Queue Version
// Create a queue that can hold a maximum of 32 events
EventQueue queue(32 * EVENTS_EVENT_SIZE);
//Create an empty thread to run objects in the event queue
Thread thread;
InterruptIn button(USER_BUTTON);
buttonState = 0;
void led3Event(void){
led3 = !led3;
}
void ledEvent(void){
//queue.call(led3Event);
queue.call(printf, "rise_handler in context %p\n", ThisThread::get_id());
queue.call(led3Event);
for (int x=0; x<10; x++){
led2 = !led2;
thread_sleep_for(500);
}
}
//Main Program
int main() {
// Start the event queue
thread.start(callback(&queue, &EventQueue::dispatch_forever));
//And make the button interrupt add an event to the queue
button.rise(queue.event(ledEvent));
while (true) {
led1 = !led1;
thread_sleep_for(250);
//Debug with Light
led3=buttonState;
}
}


