Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
- Committer:
- hi1000
- Date:
- 2019-02-16
- Revision:
- 0:765cf978c3e5
- Child:
- 1:eb499e2a1b9b
File content as of revision 0:765cf978c3e5:
#include "mbed.h" CAN can1(PD_0, PD_1); CAN can2(PB_5, PB_6); int a = 0; int b = 0; void print_char(char c = '*') { printf("%c\r\n", c); fflush(stdout); } Thread thread; DigitalOut led1(LED1); DigitalOut led2(LED2); CANMessage msg; InterruptIn button1(USER_BUTTON); volatile bool button1_pressed = false; // Used in the main loop volatile bool button1_enabled = true; // Used for debouncing Timeout button1_timeout; // Used for debouncing // Enables button when bouncing is over void button1_enabled_cb(void) { button1_enabled = true; } // ISR handling button pressed event void button1_onpressed_cb(void) { if (button1_enabled) { // Disabled while the button is bouncing button1_enabled = false; button1_pressed = true; // To be read by the main loop button1_timeout.attach(callback(button1_enabled_cb), 0.3); // Debounce time 300 ms } } void print_thread() { while (true) { #if 1 if(can1.read(msg)) { print_char(); printf("got message id=%d 0x%08x\r\n", msg.id, msg.id); // b = *reinterpret_cast<int*>(msg.data); b = msg.data[0]; printf("got data %d 0x%08x \r\n", b, b); if(msg.id == 1337) { led2 = !led2; b = *reinterpret_cast<int*>(msg.data); printf("got message %d\r\n", b); if(b % 5 == 0) led2 = !led2; } } // wait(0.2); #endif } } int main() { printf("\n\n*** RTOS basic example ***\n"); thread.start(print_thread); // can1.reset(); // can2.reset(); // can1.frequency(100000); // can2.frequency(100000); //button1.mode(PullUp); // Activate pull-up button1.fall(callback(button1_onpressed_cb)); // Attach ISR to handle button press event int idx = 0; // Just for printf below while(1) { if (button1_pressed) { // Set when button is pressed button1_pressed = false; printf("Button pressed %d\n", idx++); can1.write(CANMessage(1337, reinterpret_cast<char*>(&a), 1)); led1 = !led1; a++; } } #if 0 while(1) { // can1.write(CANMessage(1337, reinterpret_cast<char*>(&a), sizeof(a))); #if can1.write(CANMessage(1337, reinterpret_cast<char*>(&a), 1)); #endif printf("loop a=%d\n", a); led1 = !led1; a++; wait(0.2); } #endif }