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@8:6105ffbaf237, 2019-03-23 (annotated)
- Committer:
- hi1000
- Date:
- Sat Mar 23 09:18:03 2019 +0000
- Revision:
- 8:6105ffbaf237
- Parent:
- 7:e0c7e624c5fa
- Child:
- 9:486f65124378
add queue;
Who changed what in which revision?
User | Revision | Line number | New 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 | 8:6105ffbaf237 | 12 | unsigned char rx_buffer[8], tx_buffer[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 | 8:6105ffbaf237 | 40 | Thread can_receivethread; |
hi1000 | 8:6105ffbaf237 | 41 | Thread can_handlethread; |
hi1000 | 1:eb499e2a1b9b | 42 | |
hi1000 | 0:765cf978c3e5 | 43 | CANMessage msg; |
hi1000 | 8:6105ffbaf237 | 44 | MemoryPool<CANMessage, 16> can_mpool; |
hi1000 | 8:6105ffbaf237 | 45 | Queue<CANMessage, 16> can_queue; |
hi1000 | 0:765cf978c3e5 | 46 | |
hi1000 | 0:765cf978c3e5 | 47 | InterruptIn button1(USER_BUTTON); |
hi1000 | 0:765cf978c3e5 | 48 | volatile bool button1_pressed = false; // Used in the main loop |
hi1000 | 0:765cf978c3e5 | 49 | volatile bool button1_enabled = true; // Used for debouncing |
hi1000 | 0:765cf978c3e5 | 50 | Timeout button1_timeout; // Used for debouncing |
hi1000 | 0:765cf978c3e5 | 51 | |
hi1000 | 0:765cf978c3e5 | 52 | // Enables button when bouncing is over |
hi1000 | 0:765cf978c3e5 | 53 | void button1_enabled_cb(void) |
hi1000 | 0:765cf978c3e5 | 54 | { |
hi1000 | 0:765cf978c3e5 | 55 | button1_enabled = true; |
hi1000 | 0:765cf978c3e5 | 56 | } |
hi1000 | 0:765cf978c3e5 | 57 | |
hi1000 | 0:765cf978c3e5 | 58 | // ISR handling button pressed event |
hi1000 | 0:765cf978c3e5 | 59 | void button1_onpressed_cb(void) |
hi1000 | 0:765cf978c3e5 | 60 | { |
hi1000 | 0:765cf978c3e5 | 61 | if (button1_enabled) { // Disabled while the button is bouncing |
hi1000 | 0:765cf978c3e5 | 62 | button1_enabled = false; |
hi1000 | 0:765cf978c3e5 | 63 | button1_pressed = true; // To be read by the main loop |
hi1000 | 0:765cf978c3e5 | 64 | button1_timeout.attach(callback(button1_enabled_cb), 0.3); // Debounce time 300 ms |
hi1000 | 0:765cf978c3e5 | 65 | } |
hi1000 | 0:765cf978c3e5 | 66 | } |
hi1000 | 0:765cf978c3e5 | 67 | |
hi1000 | 8:6105ffbaf237 | 68 | void can_rxthread() |
hi1000 | 0:765cf978c3e5 | 69 | { |
hi1000 | 0:765cf978c3e5 | 70 | while (true) { |
hi1000 | 0:765cf978c3e5 | 71 | #if 1 |
hi1000 | 0:765cf978c3e5 | 72 | if(can1.read(msg)) { |
hi1000 | 0:765cf978c3e5 | 73 | print_char(); |
hi1000 | 0:765cf978c3e5 | 74 | printf("got message id=%d 0x%08x\r\n", msg.id, msg.id); |
hi1000 | 0:765cf978c3e5 | 75 | // b = *reinterpret_cast<int*>(msg.data); |
hi1000 | 0:765cf978c3e5 | 76 | b = msg.data[0]; |
hi1000 | 8:6105ffbaf237 | 77 | CANMessage *can_message = can_mpool.alloc(); |
hi1000 | 8:6105ffbaf237 | 78 | memcpy((void *)can_message, (void *)&msg, sizeof(msg)); |
hi1000 | 8:6105ffbaf237 | 79 | if (!can_queue.full()) |
hi1000 | 8:6105ffbaf237 | 80 | can_queue.put(can_message); |
hi1000 | 8:6105ffbaf237 | 81 | else |
hi1000 | 8:6105ffbaf237 | 82 | { |
hi1000 | 8:6105ffbaf237 | 83 | printf("message queue is full. \r\n"); |
hi1000 | 8:6105ffbaf237 | 84 | } |
hi1000 | 8:6105ffbaf237 | 85 | |
hi1000 | 0:765cf978c3e5 | 86 | printf("got data %d 0x%08x \r\n", b, b); |
hi1000 | 0:765cf978c3e5 | 87 | if(msg.id == 1337) { |
hi1000 | 0:765cf978c3e5 | 88 | led2 = !led2; |
hi1000 | 0:765cf978c3e5 | 89 | |
hi1000 | 0:765cf978c3e5 | 90 | b = *reinterpret_cast<int*>(msg.data); |
hi1000 | 0:765cf978c3e5 | 91 | printf("got message %d\r\n", b); |
hi1000 | 0:765cf978c3e5 | 92 | if(b % 5 == 0) |
hi1000 | 0:765cf978c3e5 | 93 | led2 = !led2; |
hi1000 | 0:765cf978c3e5 | 94 | } |
hi1000 | 0:765cf978c3e5 | 95 | } |
hi1000 | 0:765cf978c3e5 | 96 | // wait(0.2); |
hi1000 | 0:765cf978c3e5 | 97 | #endif |
hi1000 | 0:765cf978c3e5 | 98 | } |
hi1000 | 0:765cf978c3e5 | 99 | } |
hi1000 | 0:765cf978c3e5 | 100 | |
hi1000 | 0:765cf978c3e5 | 101 | int main() |
hi1000 | 0:765cf978c3e5 | 102 | { |
hi1000 | 2:61a0169765bf | 103 | wait(1); |
hi1000 | 8:6105ffbaf237 | 104 | #ifdef LCD_1621 |
hi1000 | 4:40bb33497de4 | 105 | lcd.clear(); // clears display |
hi1000 | 4:40bb33497de4 | 106 | lcd.allsegson(); |
hi1000 | 5:4585215afd11 | 107 | |
hi1000 | 4:40bb33497de4 | 108 | // lcd.printf("ABCDEFGHI"); // Standard printf function, All ASCII characters will display |
hi1000 | 5:4585215afd11 | 109 | #endif |
hi1000 | 2:61a0169765bf | 110 | printf("\n\n*** RTOS basic example ***\r\n"); |
hi1000 | 5:4585215afd11 | 111 | #ifdef LCD_1602 |
hi1000 | 5:4585215afd11 | 112 | lcd.printf( 0, "Hello world!" ); // line# (0 or 1), string |
hi1000 | 5:4585215afd11 | 113 | lcd.printf( 1, "pi = %.6f", 3.14159265 ); |
hi1000 | 6:a9a03663fa23 | 114 | lcd.putcxy(0x55, 5, 1); |
hi1000 | 6:a9a03663fa23 | 115 | lcd.printf(5, 0, "UUU"); |
hi1000 | 5:4585215afd11 | 116 | #endif |
hi1000 | 2:61a0169765bf | 117 | init_scale(); |
hi1000 | 8:6105ffbaf237 | 118 | can_receivethread.start(can_rxthread); |
hi1000 | 8:6105ffbaf237 | 119 | can_handlethread.start(analyzePayload); |
hi1000 | 1:eb499e2a1b9b | 120 | // flashIAP.init(); |
hi1000 | 1:eb499e2a1b9b | 121 | // printf("Flash start address: 0x%08x Flash Size: %d\r\n", flashIAP.get_flash_start(), flashIAP.get_flash_size()); |
hi1000 | 0:765cf978c3e5 | 122 | // can1.reset(); |
hi1000 | 0:765cf978c3e5 | 123 | // can2.reset(); |
hi1000 | 2:61a0169765bf | 124 | can1.frequency(100000); |
hi1000 | 0:765cf978c3e5 | 125 | // can2.frequency(100000); |
hi1000 | 0:765cf978c3e5 | 126 | //button1.mode(PullUp); // Activate pull-up |
hi1000 | 2:61a0169765bf | 127 | button1.fall(callback(button1_onpressed_cb)); // Attach ISR to handle button press event |
hi1000 | 2:61a0169765bf | 128 | // eeprom_test(); |
hi1000 | 0:765cf978c3e5 | 129 | |
hi1000 | 0:765cf978c3e5 | 130 | int idx = 0; // Just for printf below |
hi1000 | 0:765cf978c3e5 | 131 | |
hi1000 | 0:765cf978c3e5 | 132 | while(1) { |
hi1000 | 0:765cf978c3e5 | 133 | if (button1_pressed) { // Set when button is pressed |
hi1000 | 1:eb499e2a1b9b | 134 | printf("scale value %f. \r\n", hx711.getGram()); |
hi1000 | 0:765cf978c3e5 | 135 | button1_pressed = false; |
hi1000 | 2:61a0169765bf | 136 | printf("Button pressed %d\r\n", idx++); |
hi1000 | 2:61a0169765bf | 137 | printf("ID=%d. \r\n", init_id + idx%10); |
hi1000 | 2:61a0169765bf | 138 | can1.write(CANMessage((init_id + idx%10), reinterpret_cast<char*>(&a), 1)); |
hi1000 | 0:765cf978c3e5 | 139 | led1 = !led1; |
hi1000 | 0:765cf978c3e5 | 140 | a++; |
hi1000 | 0:765cf978c3e5 | 141 | } |
hi1000 | 0:765cf978c3e5 | 142 | } |
hi1000 | 0:765cf978c3e5 | 143 | #if 0 |
hi1000 | 0:765cf978c3e5 | 144 | while(1) { |
hi1000 | 0:765cf978c3e5 | 145 | // can1.write(CANMessage(1337, reinterpret_cast<char*>(&a), sizeof(a))); |
hi1000 | 0:765cf978c3e5 | 146 | #if |
hi1000 | 0:765cf978c3e5 | 147 | can1.write(CANMessage(1337, reinterpret_cast<char*>(&a), 1)); |
hi1000 | 0:765cf978c3e5 | 148 | #endif |
hi1000 | 0:765cf978c3e5 | 149 | printf("loop a=%d\n", a); |
hi1000 | 0:765cf978c3e5 | 150 | led1 = !led1; |
hi1000 | 0:765cf978c3e5 | 151 | a++; |
hi1000 | 0:765cf978c3e5 | 152 | wait(0.2); |
hi1000 | 0:765cf978c3e5 | 153 | } |
hi1000 | 0:765cf978c3e5 | 154 | #endif |
hi1000 | 0:765cf978c3e5 | 155 | } |