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@18:e6ed582f7022, 2019-07-20 (annotated)
- Committer:
- hi1000
- Date:
- Sat Jul 20 03:57:26 2019 +0000
- Revision:
- 18:e6ed582f7022
- Parent:
- 17:faa4d4976d22
- Child:
- 19:0356e54240cc
Add Menu: Calibration Weight and Calibration
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 | 18:e6ed582f7022 | 7 | #include "yoda2.h" |
hi1000 | 9:486f65124378 | 8 | |
hi1000 | 14:2e17a27f56b2 | 9 | EventFlags button_event_flags; |
hi1000 | 14:2e17a27f56b2 | 10 | EventFlags LCD_update_flags; |
hi1000 | 14:2e17a27f56b2 | 11 | extern void main_menu(); |
hi1000 | 7:e0c7e624c5fa | 12 | extern void analyzePayload(); |
hi1000 | 11:8702316d7fc8 | 13 | #if 1 |
hi1000 | 11:8702316d7fc8 | 14 | #ifdef STM32F207xx |
hi1000 | 11:8702316d7fc8 | 15 | HX711 hx711(PB_11, PB_10);// data, clk |
hi1000 | 11:8702316d7fc8 | 16 | #endif |
hi1000 | 11:8702316d7fc8 | 17 | |
hi1000 | 11:8702316d7fc8 | 18 | #ifdef STM32F303xE |
hi1000 | 11:8702316d7fc8 | 19 | //HX711 hx711(D8, D9);// data, clk |
hi1000 | 11:8702316d7fc8 | 20 | HX711 hx711(PA_14, PA_15); |
hi1000 | 11:8702316d7fc8 | 21 | #endif |
hi1000 | 18:e6ed582f7022 | 22 | extern void scaleCalibration(bool release_led); |
hi1000 | 11:8702316d7fc8 | 23 | extern void init_scale(); |
hi1000 | 11:8702316d7fc8 | 24 | Thread scale_thread; |
hi1000 | 11:8702316d7fc8 | 25 | extern void scale_reading(); |
hi1000 | 9:486f65124378 | 26 | #endif |
hi1000 | 8:6105ffbaf237 | 27 | unsigned char rx_buffer[8], tx_buffer[8]; |
hi1000 | 7:e0c7e624c5fa | 28 | unsigned char rx_length, tx_length; |
hi1000 | 0:765cf978c3e5 | 29 | |
hi1000 | 5:4585215afd11 | 30 | #define LCD_1602 |
hi1000 | 9:486f65124378 | 31 | #ifdef STM32F207xx |
hi1000 | 5:4585215afd11 | 32 | SB1602E lcd( PB_9, PB_8 ); // SDA, SCL |
hi1000 | 0:765cf978c3e5 | 33 | CAN can1(PD_0, PD_1); |
hi1000 | 0:765cf978c3e5 | 34 | CAN can2(PB_5, PB_6); |
hi1000 | 1:eb499e2a1b9b | 35 | DigitalOut led1(LED1); |
hi1000 | 1:eb499e2a1b9b | 36 | DigitalOut led2(LED2); |
hi1000 | 1:eb499e2a1b9b | 37 | //FlashIAP flashIAP; |
hi1000 | 1:eb499e2a1b9b | 38 | |
hi1000 | 5:4585215afd11 | 39 | //#define LCD_1621 |
hi1000 | 5:4585215afd11 | 40 | //digitLCD lcd(PA_5,PA_4,PB_5); // WO, CS, DATA |
hi1000 | 9:486f65124378 | 41 | #endif |
hi1000 | 9:486f65124378 | 42 | #ifdef STM32F303xE |
hi1000 | 9:486f65124378 | 43 | SB1602E lcd(D14, D15 ); // SDA, SCL |
hi1000 | 9:486f65124378 | 44 | CAN can1(PA_11, PA_12); // RD, TD |
hi1000 | 9:486f65124378 | 45 | DigitalOut led1(LED1); // only one LED PA_5 |
hi1000 | 9:486f65124378 | 46 | DigitalOut led2(LED2); // only one LED PA_5 |
hi1000 | 9:486f65124378 | 47 | #endif |
hi1000 | 7:e0c7e624c5fa | 48 | |
hi1000 | 12:5cb359f981f3 | 49 | DigitalOut output1(PC_3); |
hi1000 | 12:5cb359f981f3 | 50 | DigitalOut output2(PC_2); |
hi1000 | 12:5cb359f981f3 | 51 | DigitalOut output3(PB_7); |
hi1000 | 12:5cb359f981f3 | 52 | DigitalIn input1(PC_6); |
hi1000 | 12:5cb359f981f3 | 53 | DigitalIn input2(PC_8); |
hi1000 | 17:faa4d4976d22 | 54 | int current_weight; |
hi1000 | 10:d1104e320de7 | 55 | EEPROM ep(SDA,SCL,EEPROM_ADDR,EEPROM::T24C256); |
hi1000 | 9:486f65124378 | 56 | extern void eeprom_test(void); |
hi1000 | 16:f4277e9b8612 | 57 | int device_address = DEVICE_DEFAULT_ADDRESS; // address 11 bits: last 8 bits is device_address |
hi1000 | 16:f4277e9b8612 | 58 | int device_type; //address 11 bits: first 3 bits is device_type |
hi1000 | 14:2e17a27f56b2 | 59 | int init_id = 0x00300000; // first 11 bit is the address |
hi1000 | 14:2e17a27f56b2 | 60 | int init_filter_handle, broadcast_filter_handle; |
hi1000 | 14:2e17a27f56b2 | 61 | int broadcast_id = 0x1ffC0000; |
hi1000 | 1:eb499e2a1b9b | 62 | |
hi1000 | 9:486f65124378 | 63 | uint8_t can_tx_data[8]; |
hi1000 | 9:486f65124378 | 64 | uint8_t can_rx_data[8]; |
hi1000 | 0:765cf978c3e5 | 65 | |
hi1000 | 0:765cf978c3e5 | 66 | void print_char(char c = '*') |
hi1000 | 0:765cf978c3e5 | 67 | { |
hi1000 | 0:765cf978c3e5 | 68 | printf("%c\r\n", c); |
hi1000 | 0:765cf978c3e5 | 69 | fflush(stdout); |
hi1000 | 0:765cf978c3e5 | 70 | } |
hi1000 | 0:765cf978c3e5 | 71 | |
hi1000 | 11:8702316d7fc8 | 72 | |
hi1000 | 8:6105ffbaf237 | 73 | Thread can_receivethread; |
hi1000 | 8:6105ffbaf237 | 74 | Thread can_handlethread; |
hi1000 | 14:2e17a27f56b2 | 75 | Thread mainmenu_thread; |
hi1000 | 1:eb499e2a1b9b | 76 | |
hi1000 | 0:765cf978c3e5 | 77 | CANMessage msg; |
hi1000 | 8:6105ffbaf237 | 78 | MemoryPool<CANMessage, 16> can_mpool; |
hi1000 | 8:6105ffbaf237 | 79 | Queue<CANMessage, 16> can_queue; |
hi1000 | 0:765cf978c3e5 | 80 | |
hi1000 | 12:5cb359f981f3 | 81 | InterruptIn button0(USER_BUTTON); |
hi1000 | 12:5cb359f981f3 | 82 | volatile bool button0_pressed = false; // Used in the main loop |
hi1000 | 12:5cb359f981f3 | 83 | volatile bool button0_enabled = true; // Used for debouncing |
hi1000 | 12:5cb359f981f3 | 84 | Timeout button0_timeout; // Used for debouncing |
hi1000 | 12:5cb359f981f3 | 85 | InterruptIn button1(PB_4); |
hi1000 | 0:765cf978c3e5 | 86 | volatile bool button1_pressed = false; // Used in the main loop |
hi1000 | 0:765cf978c3e5 | 87 | volatile bool button1_enabled = true; // Used for debouncing |
hi1000 | 0:765cf978c3e5 | 88 | Timeout button1_timeout; // Used for debouncing |
hi1000 | 12:5cb359f981f3 | 89 | InterruptIn button2(PC_1); |
hi1000 | 12:5cb359f981f3 | 90 | volatile bool button2_pressed = false; // Used in the main loop |
hi1000 | 12:5cb359f981f3 | 91 | volatile bool button2_enabled = true; // Used for debouncing |
hi1000 | 12:5cb359f981f3 | 92 | Timeout button2_timeout; // Used for debouncing |
hi1000 | 12:5cb359f981f3 | 93 | InterruptIn button3(PC_7); |
hi1000 | 12:5cb359f981f3 | 94 | volatile bool button3_pressed = false; // Used in the main loop |
hi1000 | 12:5cb359f981f3 | 95 | volatile bool button3_enabled = true; // Used for debouncing |
hi1000 | 12:5cb359f981f3 | 96 | Timeout button3_timeout; // Used for debouncing |
hi1000 | 12:5cb359f981f3 | 97 | InterruptIn button4(PA_9); |
hi1000 | 12:5cb359f981f3 | 98 | volatile bool button4_pressed = false; // Used in the main loop |
hi1000 | 12:5cb359f981f3 | 99 | volatile bool button4_enabled = true; // Used for debouncing |
hi1000 | 12:5cb359f981f3 | 100 | Timeout button4_timeout; // Used for debouncing |
hi1000 | 12:5cb359f981f3 | 101 | |
hi1000 | 0:765cf978c3e5 | 102 | |
hi1000 | 0:765cf978c3e5 | 103 | // Enables button when bouncing is over |
hi1000 | 12:5cb359f981f3 | 104 | //button0 |
hi1000 | 12:5cb359f981f3 | 105 | void button0_enabled_cb(void) |
hi1000 | 12:5cb359f981f3 | 106 | { |
hi1000 | 12:5cb359f981f3 | 107 | int button_status; |
hi1000 | 12:5cb359f981f3 | 108 | button_status = button0.read(); |
hi1000 | 12:5cb359f981f3 | 109 | if (button_status == 0) |
hi1000 | 12:5cb359f981f3 | 110 | { |
hi1000 | 12:5cb359f981f3 | 111 | printf("button0 down\r\n"); |
hi1000 | 18:e6ed582f7022 | 112 | scaleCalibration(true); |
hi1000 | 14:2e17a27f56b2 | 113 | button_status = button0.read(); |
hi1000 | 14:2e17a27f56b2 | 114 | if (button_status == 0) |
hi1000 | 14:2e17a27f56b2 | 115 | { |
hi1000 | 14:2e17a27f56b2 | 116 | printf("button0 hold\r\n"); |
hi1000 | 14:2e17a27f56b2 | 117 | } |
hi1000 | 14:2e17a27f56b2 | 118 | else |
hi1000 | 14:2e17a27f56b2 | 119 | { |
hi1000 | 14:2e17a27f56b2 | 120 | printf("button0 press hold and release\r\n"); |
hi1000 | 14:2e17a27f56b2 | 121 | } |
hi1000 | 12:5cb359f981f3 | 122 | } |
hi1000 | 12:5cb359f981f3 | 123 | else |
hi1000 | 12:5cb359f981f3 | 124 | printf("button0 released\r\n"); |
hi1000 | 12:5cb359f981f3 | 125 | button0_enabled = true; |
hi1000 | 12:5cb359f981f3 | 126 | } |
hi1000 | 12:5cb359f981f3 | 127 | |
hi1000 | 12:5cb359f981f3 | 128 | // ISR handling button pressed event |
hi1000 | 12:5cb359f981f3 | 129 | void button0_onpressed_cb(void) |
hi1000 | 12:5cb359f981f3 | 130 | { |
hi1000 | 12:5cb359f981f3 | 131 | if (button0_enabled) { // Disabled while the button is bouncing |
hi1000 | 12:5cb359f981f3 | 132 | button0_enabled = false; |
hi1000 | 12:5cb359f981f3 | 133 | button0_pressed = true; // To be read by the main loop |
hi1000 | 12:5cb359f981f3 | 134 | button0_timeout.attach(callback(button0_enabled_cb), 0.3); // Debounce time 300 ms |
hi1000 | 12:5cb359f981f3 | 135 | } |
hi1000 | 12:5cb359f981f3 | 136 | } |
hi1000 | 12:5cb359f981f3 | 137 | //button0-- |
hi1000 | 12:5cb359f981f3 | 138 | //button1 |
hi1000 | 0:765cf978c3e5 | 139 | void button1_enabled_cb(void) |
hi1000 | 0:765cf978c3e5 | 140 | { |
hi1000 | 12:5cb359f981f3 | 141 | int button_status; |
hi1000 | 12:5cb359f981f3 | 142 | button_status = button1.read(); |
hi1000 | 12:5cb359f981f3 | 143 | if (button_status == 0) |
hi1000 | 12:5cb359f981f3 | 144 | { |
hi1000 | 12:5cb359f981f3 | 145 | printf("button1 down\r\n"); |
hi1000 | 14:2e17a27f56b2 | 146 | button_event_flags.set(BUTTON1_HOLD_EVENT); |
hi1000 | 12:5cb359f981f3 | 147 | } |
hi1000 | 12:5cb359f981f3 | 148 | else |
hi1000 | 14:2e17a27f56b2 | 149 | { |
hi1000 | 12:5cb359f981f3 | 150 | printf("button1 released\r\n"); |
hi1000 | 14:2e17a27f56b2 | 151 | button_event_flags.set(BUTTON1_PRESSED_EVENT); |
hi1000 | 14:2e17a27f56b2 | 152 | } |
hi1000 | 0:765cf978c3e5 | 153 | button1_enabled = true; |
hi1000 | 0:765cf978c3e5 | 154 | } |
hi1000 | 0:765cf978c3e5 | 155 | |
hi1000 | 0:765cf978c3e5 | 156 | // ISR handling button pressed event |
hi1000 | 0:765cf978c3e5 | 157 | void button1_onpressed_cb(void) |
hi1000 | 0:765cf978c3e5 | 158 | { |
hi1000 | 0:765cf978c3e5 | 159 | if (button1_enabled) { // Disabled while the button is bouncing |
hi1000 | 0:765cf978c3e5 | 160 | button1_enabled = false; |
hi1000 | 0:765cf978c3e5 | 161 | button1_pressed = true; // To be read by the main loop |
hi1000 | 0:765cf978c3e5 | 162 | button1_timeout.attach(callback(button1_enabled_cb), 0.3); // Debounce time 300 ms |
hi1000 | 0:765cf978c3e5 | 163 | } |
hi1000 | 0:765cf978c3e5 | 164 | } |
hi1000 | 12:5cb359f981f3 | 165 | //button1-- |
hi1000 | 12:5cb359f981f3 | 166 | //button2 |
hi1000 | 12:5cb359f981f3 | 167 | void button2_enabled_cb(void) |
hi1000 | 12:5cb359f981f3 | 168 | { |
hi1000 | 12:5cb359f981f3 | 169 | int button_status; |
hi1000 | 12:5cb359f981f3 | 170 | button_status = button2.read(); |
hi1000 | 12:5cb359f981f3 | 171 | if (button_status == 0) |
hi1000 | 12:5cb359f981f3 | 172 | { |
hi1000 | 12:5cb359f981f3 | 173 | printf("button2 down\r\n"); |
hi1000 | 14:2e17a27f56b2 | 174 | button_event_flags.set(BUTTON2_HOLD_EVENT); |
hi1000 | 12:5cb359f981f3 | 175 | } |
hi1000 | 12:5cb359f981f3 | 176 | else |
hi1000 | 14:2e17a27f56b2 | 177 | { |
hi1000 | 12:5cb359f981f3 | 178 | printf("button2 released\r\n"); |
hi1000 | 14:2e17a27f56b2 | 179 | button_event_flags.set(BUTTON2_PRESSED_EVENT); |
hi1000 | 14:2e17a27f56b2 | 180 | } |
hi1000 | 12:5cb359f981f3 | 181 | button2_enabled = true; |
hi1000 | 12:5cb359f981f3 | 182 | } |
hi1000 | 0:765cf978c3e5 | 183 | |
hi1000 | 12:5cb359f981f3 | 184 | // ISR handling button pressed event |
hi1000 | 12:5cb359f981f3 | 185 | void button2_onpressed_cb(void) |
hi1000 | 12:5cb359f981f3 | 186 | { |
hi1000 | 12:5cb359f981f3 | 187 | if (button2_enabled) { // Disabled while the button is bouncing |
hi1000 | 12:5cb359f981f3 | 188 | button2_enabled = false; |
hi1000 | 12:5cb359f981f3 | 189 | button2_pressed = true; // To be read by the main loop |
hi1000 | 12:5cb359f981f3 | 190 | button2_timeout.attach(callback(button2_enabled_cb), 0.3); // Debounce time 300 ms |
hi1000 | 12:5cb359f981f3 | 191 | } |
hi1000 | 12:5cb359f981f3 | 192 | } |
hi1000 | 12:5cb359f981f3 | 193 | //button2-- |
hi1000 | 12:5cb359f981f3 | 194 | //button3 |
hi1000 | 12:5cb359f981f3 | 195 | void button3_enabled_cb(void) |
hi1000 | 12:5cb359f981f3 | 196 | { |
hi1000 | 12:5cb359f981f3 | 197 | int button_status; |
hi1000 | 12:5cb359f981f3 | 198 | button_status = button3.read(); |
hi1000 | 12:5cb359f981f3 | 199 | if (button_status == 0) |
hi1000 | 12:5cb359f981f3 | 200 | { |
hi1000 | 12:5cb359f981f3 | 201 | printf("button3 down\r\n"); |
hi1000 | 14:2e17a27f56b2 | 202 | button_event_flags.set(BUTTON3_HOLD_EVENT); |
hi1000 | 12:5cb359f981f3 | 203 | } |
hi1000 | 12:5cb359f981f3 | 204 | else |
hi1000 | 14:2e17a27f56b2 | 205 | { |
hi1000 | 12:5cb359f981f3 | 206 | printf("button3 released\r\n"); |
hi1000 | 14:2e17a27f56b2 | 207 | button_event_flags.set(BUTTON3_PRESSED_EVENT); |
hi1000 | 14:2e17a27f56b2 | 208 | } |
hi1000 | 12:5cb359f981f3 | 209 | button3_enabled = true; |
hi1000 | 12:5cb359f981f3 | 210 | } |
hi1000 | 12:5cb359f981f3 | 211 | |
hi1000 | 12:5cb359f981f3 | 212 | // ISR handling button pressed event |
hi1000 | 12:5cb359f981f3 | 213 | void button3_onpressed_cb(void) |
hi1000 | 12:5cb359f981f3 | 214 | { |
hi1000 | 12:5cb359f981f3 | 215 | if (button3_enabled) { // Disabled while the button is bouncing |
hi1000 | 12:5cb359f981f3 | 216 | button3_enabled = false; |
hi1000 | 12:5cb359f981f3 | 217 | button3_pressed = true; // To be read by the main loop |
hi1000 | 12:5cb359f981f3 | 218 | button3_timeout.attach(callback(button3_enabled_cb), 0.3); // Debounce time 300 ms |
hi1000 | 12:5cb359f981f3 | 219 | } |
hi1000 | 12:5cb359f981f3 | 220 | } |
hi1000 | 12:5cb359f981f3 | 221 | //button3-- |
hi1000 | 12:5cb359f981f3 | 222 | //button4 |
hi1000 | 12:5cb359f981f3 | 223 | void button4_enabled_cb(void) |
hi1000 | 12:5cb359f981f3 | 224 | { |
hi1000 | 12:5cb359f981f3 | 225 | int button_status; |
hi1000 | 12:5cb359f981f3 | 226 | button_status = button4.read(); |
hi1000 | 12:5cb359f981f3 | 227 | if (button_status == 0) |
hi1000 | 12:5cb359f981f3 | 228 | { |
hi1000 | 12:5cb359f981f3 | 229 | printf("button4 down\r\n"); |
hi1000 | 14:2e17a27f56b2 | 230 | button_event_flags.set(BUTTON4_HOLD_EVENT); |
hi1000 | 12:5cb359f981f3 | 231 | } |
hi1000 | 12:5cb359f981f3 | 232 | else |
hi1000 | 14:2e17a27f56b2 | 233 | { |
hi1000 | 12:5cb359f981f3 | 234 | printf("button4 released\r\n"); |
hi1000 | 14:2e17a27f56b2 | 235 | button_event_flags.set(BUTTON4_PRESSED_EVENT); |
hi1000 | 14:2e17a27f56b2 | 236 | } |
hi1000 | 12:5cb359f981f3 | 237 | button4_enabled = true; |
hi1000 | 12:5cb359f981f3 | 238 | } |
hi1000 | 12:5cb359f981f3 | 239 | |
hi1000 | 12:5cb359f981f3 | 240 | // ISR handling button pressed event |
hi1000 | 12:5cb359f981f3 | 241 | void button4_onpressed_cb(void) |
hi1000 | 12:5cb359f981f3 | 242 | { |
hi1000 | 12:5cb359f981f3 | 243 | if (button4_enabled) { // Disabled while the button is bouncing |
hi1000 | 12:5cb359f981f3 | 244 | button4_enabled = false; |
hi1000 | 12:5cb359f981f3 | 245 | button4_pressed = true; // To be read by the main loop |
hi1000 | 12:5cb359f981f3 | 246 | button4_timeout.attach(callback(button4_enabled_cb), 0.3); // Debounce time 300 ms |
hi1000 | 12:5cb359f981f3 | 247 | } |
hi1000 | 12:5cb359f981f3 | 248 | } |
hi1000 | 12:5cb359f981f3 | 249 | //button4-- |
hi1000 | 17:faa4d4976d22 | 250 | void can_sendData(int can_id, uint8_t *tx_data, int length) |
hi1000 | 17:faa4d4976d22 | 251 | { |
hi1000 | 17:faa4d4976d22 | 252 | CANMessage txmsg; |
hi1000 | 17:faa4d4976d22 | 253 | |
hi1000 | 17:faa4d4976d22 | 254 | txmsg.format = CANExtended; |
hi1000 | 17:faa4d4976d22 | 255 | txmsg.id = can_id; |
hi1000 | 17:faa4d4976d22 | 256 | txmsg.len = length; |
hi1000 | 17:faa4d4976d22 | 257 | txmsg.data[0] = tx_data[0]; |
hi1000 | 17:faa4d4976d22 | 258 | txmsg.data[1] = tx_data[1]; |
hi1000 | 17:faa4d4976d22 | 259 | txmsg.data[2] = tx_data[2]; |
hi1000 | 17:faa4d4976d22 | 260 | txmsg.data[3] = tx_data[3]; |
hi1000 | 17:faa4d4976d22 | 261 | txmsg.data[4] = tx_data[4]; |
hi1000 | 17:faa4d4976d22 | 262 | txmsg.data[5] = tx_data[5]; |
hi1000 | 17:faa4d4976d22 | 263 | txmsg.data[6] = tx_data[6]; |
hi1000 | 17:faa4d4976d22 | 264 | txmsg.data[7] = tx_data[7]; |
hi1000 | 17:faa4d4976d22 | 265 | |
hi1000 | 17:faa4d4976d22 | 266 | printf("can_sendData can_id=0x%08x \r\n", can_id); |
hi1000 | 17:faa4d4976d22 | 267 | can1.write(txmsg); |
hi1000 | 17:faa4d4976d22 | 268 | } |
hi1000 | 17:faa4d4976d22 | 269 | |
hi1000 | 8:6105ffbaf237 | 270 | void can_rxthread() |
hi1000 | 0:765cf978c3e5 | 271 | { |
hi1000 | 9:486f65124378 | 272 | int loop; |
hi1000 | 0:765cf978c3e5 | 273 | while (true) { |
hi1000 | 9:486f65124378 | 274 | #if 1 |
hi1000 | 0:765cf978c3e5 | 275 | if(can1.read(msg)) { |
hi1000 | 0:765cf978c3e5 | 276 | print_char(); |
hi1000 | 0:765cf978c3e5 | 277 | printf("got message id=%d 0x%08x\r\n", msg.id, msg.id); |
hi1000 | 0:765cf978c3e5 | 278 | // b = *reinterpret_cast<int*>(msg.data); |
hi1000 | 9:486f65124378 | 279 | for (loop = 0; loop < msg.len; loop++) |
hi1000 | 8:6105ffbaf237 | 280 | { |
hi1000 | 9:486f65124378 | 281 | can_rx_data[loop] = msg.data[loop]; |
hi1000 | 8:6105ffbaf237 | 282 | } |
hi1000 | 8:6105ffbaf237 | 283 | |
hi1000 | 9:486f65124378 | 284 | printf("got data: length:%d\r\n", msg.len); |
hi1000 | 9:486f65124378 | 285 | for (loop = 0; loop < msg.len; loop++) |
hi1000 | 9:486f65124378 | 286 | { |
hi1000 | 9:486f65124378 | 287 | printf("data[%d]=%d\r\n", loop, can_rx_data[loop]); |
hi1000 | 9:486f65124378 | 288 | } |
hi1000 | 14:2e17a27f56b2 | 289 | // if(msg.id == 1337) |
hi1000 | 14:2e17a27f56b2 | 290 | { |
hi1000 | 9:486f65124378 | 291 | //only queue the message belongs to you |
hi1000 | 9:486f65124378 | 292 | CANMessage *can_message = can_mpool.alloc(); |
hi1000 | 9:486f65124378 | 293 | memcpy((void *)can_message, (void *)&msg, sizeof(msg)); |
hi1000 | 9:486f65124378 | 294 | if (!can_queue.full()) |
hi1000 | 9:486f65124378 | 295 | can_queue.put(can_message); |
hi1000 | 9:486f65124378 | 296 | else |
hi1000 | 9:486f65124378 | 297 | { |
hi1000 | 9:486f65124378 | 298 | printf("message queue is full. \r\n"); |
hi1000 | 9:486f65124378 | 299 | } |
hi1000 | 0:765cf978c3e5 | 300 | led2 = !led2; |
hi1000 | 0:765cf978c3e5 | 301 | } |
hi1000 | 0:765cf978c3e5 | 302 | } |
hi1000 | 9:486f65124378 | 303 | #endif |
hi1000 | 0:765cf978c3e5 | 304 | // wait(0.2); |
hi1000 | 0:765cf978c3e5 | 305 | } |
hi1000 | 0:765cf978c3e5 | 306 | } |
hi1000 | 0:765cf978c3e5 | 307 | |
hi1000 | 0:765cf978c3e5 | 308 | int main() |
hi1000 | 0:765cf978c3e5 | 309 | { |
hi1000 | 9:486f65124378 | 310 | int loop = 0; |
hi1000 | 9:486f65124378 | 311 | int8_t ival; |
hi1000 | 14:2e17a27f56b2 | 312 | printf("\n\n*** RTOS starts ***\r\n"); |
hi1000 | 9:486f65124378 | 313 | |
hi1000 | 9:486f65124378 | 314 | // wait(1); |
hi1000 | 14:2e17a27f56b2 | 315 | ep.read((uint32_t)EEPROM_DEVICE_ADDRESS_ADDRESS,device_address); |
hi1000 | 14:2e17a27f56b2 | 316 | printf("EEPROM: read device address:%d 0x%08x\r\n", device_address, device_address); |
hi1000 | 14:2e17a27f56b2 | 317 | if ((device_address == 0) || (device_address == 0xFFFFFFFF)) |
hi1000 | 14:2e17a27f56b2 | 318 | device_address = DEVICE_DEFAULT_ADDRESS; |
hi1000 | 16:f4277e9b8612 | 319 | device_type = (device_address & 0x00000700) >> 8; |
hi1000 | 16:f4277e9b8612 | 320 | |
hi1000 | 8:6105ffbaf237 | 321 | #ifdef LCD_1621 |
hi1000 | 4:40bb33497de4 | 322 | lcd.clear(); // clears display |
hi1000 | 4:40bb33497de4 | 323 | lcd.allsegson(); |
hi1000 | 5:4585215afd11 | 324 | |
hi1000 | 4:40bb33497de4 | 325 | // lcd.printf("ABCDEFGHI"); // Standard printf function, All ASCII characters will display |
hi1000 | 5:4585215afd11 | 326 | #endif |
hi1000 | 9:486f65124378 | 327 | |
hi1000 | 5:4585215afd11 | 328 | #ifdef LCD_1602 |
hi1000 | 16:f4277e9b8612 | 329 | // lcd.printf( 0, "Hello world!" ); // line# (0 or 1), string |
hi1000 | 16:f4277e9b8612 | 330 | // lcd.printf( 1, "pi = %.6f", 3.14159265 ); |
hi1000 | 16:f4277e9b8612 | 331 | // lcd.putcxy(0x55, 5, 1); |
hi1000 | 16:f4277e9b8612 | 332 | // lcd.printf(5, 0, "UUU"); |
hi1000 | 16:f4277e9b8612 | 333 | // lcd.printf(0, 0, "pressed!" ); |
hi1000 | 9:486f65124378 | 334 | #endif |
hi1000 | 11:8702316d7fc8 | 335 | |
hi1000 | 12:5cb359f981f3 | 336 | input1.mode(PullUp); |
hi1000 | 12:5cb359f981f3 | 337 | input2.mode(PullUp); |
hi1000 | 13:9f581a090e53 | 338 | |
hi1000 | 1:eb499e2a1b9b | 339 | // flashIAP.init(); |
hi1000 | 1:eb499e2a1b9b | 340 | // printf("Flash start address: 0x%08x Flash Size: %d\r\n", flashIAP.get_flash_start(), flashIAP.get_flash_size()); |
hi1000 | 0:765cf978c3e5 | 341 | // can1.reset(); |
hi1000 | 0:765cf978c3e5 | 342 | // can2.reset(); |
hi1000 | 16:f4277e9b8612 | 343 | printf("device_address =0x%08x \r\n", (device_address<<18)); |
hi1000 | 2:61a0169765bf | 344 | can1.frequency(100000); |
hi1000 | 14:2e17a27f56b2 | 345 | can1.filter((device_address<<18), 0x1FFC0000, CANExtended, init_filter_handle); // 0x1FFC0000 to filter the last 18bits 0-17 |
hi1000 | 16:f4277e9b8612 | 346 | device_address = (device_address & 0x000000FF); |
hi1000 | 14:2e17a27f56b2 | 347 | //only support one filter |
hi1000 | 14:2e17a27f56b2 | 348 | // can1.filter(broadcast_id, 0x1FFC0000, CANExtended, broadcast_filter_handle); // the broadcast id |
hi1000 | 0:765cf978c3e5 | 349 | // can2.frequency(100000); |
hi1000 | 12:5cb359f981f3 | 350 | //button0.mode(PullUp); // Activate pull-up |
hi1000 | 13:9f581a090e53 | 351 | can_receivethread.start(can_rxthread); |
hi1000 | 13:9f581a090e53 | 352 | can_handlethread.start(analyzePayload); |
hi1000 | 9:486f65124378 | 353 | |
hi1000 | 12:5cb359f981f3 | 354 | button0.fall(callback(button0_onpressed_cb)); // Attach ISR to handle button press event |
hi1000 | 12:5cb359f981f3 | 355 | button0.rise(callback(button0_onpressed_cb)); // Attach ISR to handle button press event |
hi1000 | 2:61a0169765bf | 356 | button1.fall(callback(button1_onpressed_cb)); // Attach ISR to handle button press event |
hi1000 | 12:5cb359f981f3 | 357 | button1.rise(callback(button1_onpressed_cb)); // Attach ISR to handle button press event |
hi1000 | 12:5cb359f981f3 | 358 | button2.fall(callback(button2_onpressed_cb)); // Attach ISR to handle button press event |
hi1000 | 12:5cb359f981f3 | 359 | button2.rise(callback(button2_onpressed_cb)); // Attach ISR to handle button press event |
hi1000 | 12:5cb359f981f3 | 360 | button3.fall(callback(button3_onpressed_cb)); // Attach ISR to handle button press event |
hi1000 | 12:5cb359f981f3 | 361 | button3.rise(callback(button3_onpressed_cb)); // Attach ISR to handle button press event |
hi1000 | 12:5cb359f981f3 | 362 | button4.fall(callback(button4_onpressed_cb)); // Attach ISR to handle button press event |
hi1000 | 12:5cb359f981f3 | 363 | button4.rise(callback(button4_onpressed_cb)); // Attach ISR to handle button press event |
hi1000 | 11:8702316d7fc8 | 364 | // eeprom_test(); |
hi1000 | 0:765cf978c3e5 | 365 | |
hi1000 | 11:8702316d7fc8 | 366 | #if 1 |
hi1000 | 18:e6ed582f7022 | 367 | // scaleCalibration(true); |
hi1000 | 11:8702316d7fc8 | 368 | init_scale(); |
hi1000 | 11:8702316d7fc8 | 369 | scale_thread.start(scale_reading); |
hi1000 | 14:2e17a27f56b2 | 370 | mainmenu_thread.start(main_menu); |
hi1000 | 14:2e17a27f56b2 | 371 | |
hi1000 | 11:8702316d7fc8 | 372 | #endif |
hi1000 | 14:2e17a27f56b2 | 373 | while(1) { |
hi1000 | 14:2e17a27f56b2 | 374 | wait(1); |
hi1000 | 14:2e17a27f56b2 | 375 | } |
hi1000 | 14:2e17a27f56b2 | 376 | |
hi1000 | 14:2e17a27f56b2 | 377 | #if 0 |
hi1000 | 0:765cf978c3e5 | 378 | int idx = 0; // Just for printf below |
hi1000 | 9:486f65124378 | 379 | can_tx_data[0] = 0; |
hi1000 | 0:765cf978c3e5 | 380 | while(1) { |
hi1000 | 12:5cb359f981f3 | 381 | if (button0_pressed) { // Set when button is pressed |
hi1000 | 9:486f65124378 | 382 | #if 0 |
hi1000 | 1:eb499e2a1b9b | 383 | printf("scale value %f. \r\n", hx711.getGram()); |
hi1000 | 9:486f65124378 | 384 | #endif |
hi1000 | 9:486f65124378 | 385 | can_tx_data[1] = can_tx_data[0]+1; |
hi1000 | 9:486f65124378 | 386 | can_tx_data[2] = can_tx_data[1]+1; |
hi1000 | 9:486f65124378 | 387 | can_tx_data[3] = can_tx_data[2]+1; |
hi1000 | 9:486f65124378 | 388 | can_tx_data[4] = can_tx_data[3]+1; |
hi1000 | 9:486f65124378 | 389 | can_tx_data[5] = can_tx_data[4]+1; |
hi1000 | 9:486f65124378 | 390 | can_tx_data[6] = can_tx_data[5]+1; |
hi1000 | 9:486f65124378 | 391 | can_tx_data[7] = can_tx_data[6]+1; |
hi1000 | 12:5cb359f981f3 | 392 | button0_pressed = false; |
hi1000 | 2:61a0169765bf | 393 | printf("Button pressed %d\r\n", idx++); |
hi1000 | 9:486f65124378 | 394 | printf("ID=%d data[0]=%d. \r\n", init_id + idx%10, can_tx_data[0]); |
hi1000 | 9:486f65124378 | 395 | #ifdef LCD_1602 |
hi1000 | 9:486f65124378 | 396 | lcd.printf(0, 0, "%d ", idx ); // line# (0 or 1), string |
hi1000 | 9:486f65124378 | 397 | #endif |
hi1000 | 17:faa4d4976d22 | 398 | can1.write(CANMessage((init_id + idx%10), reinterpret_cast<char*>(can_tx_data), 8, CANData,CANExtended)); |
hi1000 | 0:765cf978c3e5 | 399 | led1 = !led1; |
hi1000 | 9:486f65124378 | 400 | can_tx_data[0]++; |
hi1000 | 0:765cf978c3e5 | 401 | } |
hi1000 | 0:765cf978c3e5 | 402 | } |
hi1000 | 14:2e17a27f56b2 | 403 | #endif |
hi1000 | 9:486f65124378 | 404 | |
hi1000 | 0:765cf978c3e5 | 405 | } |