Feng Hong / Mbed OS Nucleo_rtos_basic
Committer:
hi1000
Date:
Sat Mar 23 07:08:27 2019 +0000
Revision:
7:e0c7e624c5fa
Parent:
6:a9a03663fa23
Child:
8:6105ffbaf237
tidy up the codes, separate to several files

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hi1000 0:765cf978c3e5 1 #include "mbed.h"
hi1000 1:eb499e2a1b9b 2 #include <HX711.h>
hi1000 2:61a0169765bf 3 #include <eeprom.h>
hi1000 7:e0c7e624c5fa 4 #include "eeprom_cust.h"
hi1000 5:4585215afd11 5 //#include "digitLCD.h"
hi1000 5:4585215afd11 6 #include "SB1602E.h"
hi1000 7:e0c7e624c5fa 7 extern void analyzePayload();
hi1000 7:e0c7e624c5fa 8 extern void scaleCalibration();
hi1000 7:e0c7e624c5fa 9 void init_scale();
hi1000 7:e0c7e624c5fa 10 extern HX711 hx711;
hi1000 7:e0c7e624c5fa 11
hi1000 7:e0c7e624c5fa 12 unsigned char rx[8], tx[8];
hi1000 7:e0c7e624c5fa 13 unsigned char rx_length, tx_length;
hi1000 0:765cf978c3e5 14
hi1000 5:4585215afd11 15 #define LCD_1602
hi1000 5:4585215afd11 16 SB1602E lcd( PB_9, PB_8 ); // SDA, SCL
hi1000 0:765cf978c3e5 17 CAN can1(PD_0, PD_1);
hi1000 0:765cf978c3e5 18 CAN can2(PB_5, PB_6);
hi1000 1:eb499e2a1b9b 19 DigitalOut led1(LED1);
hi1000 1:eb499e2a1b9b 20 DigitalOut led2(LED2);
hi1000 1:eb499e2a1b9b 21 //FlashIAP flashIAP;
hi1000 1:eb499e2a1b9b 22
hi1000 5:4585215afd11 23 //#define LCD_1621
hi1000 5:4585215afd11 24 //digitLCD lcd(PA_5,PA_4,PB_5); // WO, CS, DATA
hi1000 1:eb499e2a1b9b 25
hi1000 7:e0c7e624c5fa 26
hi1000 2:61a0169765bf 27
hi1000 7:e0c7e624c5fa 28 extern EEPROM ep;
hi1000 2:61a0169765bf 29 int init_id = 0x537; // first 8 bit is the address
hi1000 1:eb499e2a1b9b 30
hi1000 0:765cf978c3e5 31 int a = 0;
hi1000 0:765cf978c3e5 32 int b = 0;
hi1000 0:765cf978c3e5 33
hi1000 0:765cf978c3e5 34 void print_char(char c = '*')
hi1000 0:765cf978c3e5 35 {
hi1000 0:765cf978c3e5 36 printf("%c\r\n", c);
hi1000 0:765cf978c3e5 37 fflush(stdout);
hi1000 0:765cf978c3e5 38 }
hi1000 0:765cf978c3e5 39
hi1000 0:765cf978c3e5 40 Thread thread;
hi1000 0:765cf978c3e5 41
hi1000 1:eb499e2a1b9b 42
hi1000 0:765cf978c3e5 43 CANMessage msg;
hi1000 0:765cf978c3e5 44
hi1000 0:765cf978c3e5 45
hi1000 0:765cf978c3e5 46 InterruptIn button1(USER_BUTTON);
hi1000 0:765cf978c3e5 47 volatile bool button1_pressed = false; // Used in the main loop
hi1000 0:765cf978c3e5 48 volatile bool button1_enabled = true; // Used for debouncing
hi1000 0:765cf978c3e5 49 Timeout button1_timeout; // Used for debouncing
hi1000 0:765cf978c3e5 50
hi1000 0:765cf978c3e5 51 // Enables button when bouncing is over
hi1000 0:765cf978c3e5 52 void button1_enabled_cb(void)
hi1000 0:765cf978c3e5 53 {
hi1000 0:765cf978c3e5 54 button1_enabled = true;
hi1000 0:765cf978c3e5 55 }
hi1000 0:765cf978c3e5 56
hi1000 0:765cf978c3e5 57 // ISR handling button pressed event
hi1000 0:765cf978c3e5 58 void button1_onpressed_cb(void)
hi1000 0:765cf978c3e5 59 {
hi1000 0:765cf978c3e5 60 if (button1_enabled) { // Disabled while the button is bouncing
hi1000 0:765cf978c3e5 61 button1_enabled = false;
hi1000 0:765cf978c3e5 62 button1_pressed = true; // To be read by the main loop
hi1000 0:765cf978c3e5 63 button1_timeout.attach(callback(button1_enabled_cb), 0.3); // Debounce time 300 ms
hi1000 0:765cf978c3e5 64 }
hi1000 0:765cf978c3e5 65 }
hi1000 0:765cf978c3e5 66
hi1000 0:765cf978c3e5 67 void print_thread()
hi1000 0:765cf978c3e5 68 {
hi1000 0:765cf978c3e5 69 while (true) {
hi1000 0:765cf978c3e5 70 #if 1
hi1000 0:765cf978c3e5 71 if(can1.read(msg)) {
hi1000 0:765cf978c3e5 72 print_char();
hi1000 0:765cf978c3e5 73 printf("got message id=%d 0x%08x\r\n", msg.id, msg.id);
hi1000 0:765cf978c3e5 74 // b = *reinterpret_cast<int*>(msg.data);
hi1000 0:765cf978c3e5 75 b = msg.data[0];
hi1000 0:765cf978c3e5 76 printf("got data %d 0x%08x \r\n", b, b);
hi1000 0:765cf978c3e5 77 if(msg.id == 1337) {
hi1000 0:765cf978c3e5 78 led2 = !led2;
hi1000 0:765cf978c3e5 79
hi1000 0:765cf978c3e5 80 b = *reinterpret_cast<int*>(msg.data);
hi1000 0:765cf978c3e5 81 printf("got message %d\r\n", b);
hi1000 0:765cf978c3e5 82 if(b % 5 == 0)
hi1000 0:765cf978c3e5 83 led2 = !led2;
hi1000 0:765cf978c3e5 84 }
hi1000 0:765cf978c3e5 85 }
hi1000 0:765cf978c3e5 86 // wait(0.2);
hi1000 0:765cf978c3e5 87 #endif
hi1000 0:765cf978c3e5 88 }
hi1000 0:765cf978c3e5 89 }
hi1000 0:765cf978c3e5 90
hi1000 0:765cf978c3e5 91 int main()
hi1000 0:765cf978c3e5 92 {
hi1000 2:61a0169765bf 93 wait(1);
hi1000 5:4585215afd11 94 #ifdef LCD_1621
hi1000 4:40bb33497de4 95 lcd.clear(); // clears display
hi1000 4:40bb33497de4 96 lcd.allsegson();
hi1000 5:4585215afd11 97
hi1000 4:40bb33497de4 98 // lcd.printf("ABCDEFGHI"); // Standard printf function, All ASCII characters will display
hi1000 5:4585215afd11 99 #endif
hi1000 2:61a0169765bf 100 printf("\n\n*** RTOS basic example ***\r\n");
hi1000 7:e0c7e624c5fa 101 analyzePayload();
hi1000 5:4585215afd11 102 #ifdef LCD_1602
hi1000 5:4585215afd11 103 lcd.printf( 0, "Hello world!" ); // line# (0 or 1), string
hi1000 5:4585215afd11 104 lcd.printf( 1, "pi = %.6f", 3.14159265 );
hi1000 6:a9a03663fa23 105 lcd.putcxy(0x55, 5, 1);
hi1000 6:a9a03663fa23 106 lcd.printf(5, 0, "UUU");
hi1000 5:4585215afd11 107 #endif
hi1000 2:61a0169765bf 108 init_scale();
hi1000 1:eb499e2a1b9b 109 thread.start(print_thread);
hi1000 0:765cf978c3e5 110
hi1000 1:eb499e2a1b9b 111 // flashIAP.init();
hi1000 1:eb499e2a1b9b 112 // printf("Flash start address: 0x%08x Flash Size: %d\r\n", flashIAP.get_flash_start(), flashIAP.get_flash_size());
hi1000 0:765cf978c3e5 113 // can1.reset();
hi1000 0:765cf978c3e5 114 // can2.reset();
hi1000 2:61a0169765bf 115 can1.frequency(100000);
hi1000 0:765cf978c3e5 116 // can2.frequency(100000);
hi1000 0:765cf978c3e5 117 //button1.mode(PullUp); // Activate pull-up
hi1000 2:61a0169765bf 118 button1.fall(callback(button1_onpressed_cb)); // Attach ISR to handle button press event
hi1000 2:61a0169765bf 119 // eeprom_test();
hi1000 0:765cf978c3e5 120
hi1000 0:765cf978c3e5 121 int idx = 0; // Just for printf below
hi1000 0:765cf978c3e5 122
hi1000 0:765cf978c3e5 123 while(1) {
hi1000 0:765cf978c3e5 124 if (button1_pressed) { // Set when button is pressed
hi1000 1:eb499e2a1b9b 125 printf("scale value %f. \r\n", hx711.getGram());
hi1000 0:765cf978c3e5 126 button1_pressed = false;
hi1000 2:61a0169765bf 127 printf("Button pressed %d\r\n", idx++);
hi1000 2:61a0169765bf 128 printf("ID=%d. \r\n", init_id + idx%10);
hi1000 2:61a0169765bf 129 can1.write(CANMessage((init_id + idx%10), reinterpret_cast<char*>(&a), 1));
hi1000 0:765cf978c3e5 130 led1 = !led1;
hi1000 0:765cf978c3e5 131 a++;
hi1000 0:765cf978c3e5 132 }
hi1000 0:765cf978c3e5 133 }
hi1000 0:765cf978c3e5 134 #if 0
hi1000 0:765cf978c3e5 135 while(1) {
hi1000 0:765cf978c3e5 136 // can1.write(CANMessage(1337, reinterpret_cast<char*>(&a), sizeof(a)));
hi1000 0:765cf978c3e5 137 #if
hi1000 0:765cf978c3e5 138 can1.write(CANMessage(1337, reinterpret_cast<char*>(&a), 1));
hi1000 0:765cf978c3e5 139 #endif
hi1000 0:765cf978c3e5 140 printf("loop a=%d\n", a);
hi1000 0:765cf978c3e5 141 led1 = !led1;
hi1000 0:765cf978c3e5 142 a++;
hi1000 0:765cf978c3e5 143 wait(0.2);
hi1000 0:765cf978c3e5 144 }
hi1000 0:765cf978c3e5 145 #endif
hi1000 0:765cf978c3e5 146 }