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@19:0356e54240cc, 2019-09-24 (annotated)
- Committer:
- hi1000
- Date:
- Tue Sep 24 14:05:58 2019 +0000
- Revision:
- 19:0356e54240cc
- Parent:
- 18:e6ed582f7022
- Child:
- 20:ec9d4f6a16ac
First Version of YODA2; -Buttons; -Menu; -LCD; -Scale; -CAN Bus; -EEPROM; work
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 | 19:0356e54240cc | 8 | #include "TextLCD.h" |
hi1000 | 9:486f65124378 | 9 | |
hi1000 | 14:2e17a27f56b2 | 10 | EventFlags button_event_flags; |
hi1000 | 19:0356e54240cc | 11 | EEPROM ep(SDA,SCL,EEPROM_ADDR,EEPROM::T24C32); |
hi1000 | 19:0356e54240cc | 12 | extern void eeprom_test(void); |
hi1000 | 19:0356e54240cc | 13 | #ifdef YODA2 |
hi1000 | 19:0356e54240cc | 14 | // I2C Communication |
hi1000 | 19:0356e54240cc | 15 | I2C i2c_lcd(PB_9, PB_8); // SDA, SCL |
hi1000 | 19:0356e54240cc | 16 | TextLCD_I2C_N lcd(&i2c_lcd, ST7032_SA, TextLCD::LCD16x2, NC, TextLCD::ST7032_3V3); |
hi1000 | 19:0356e54240cc | 17 | #endif |
hi1000 | 19:0356e54240cc | 18 | #if 1 |
hi1000 | 19:0356e54240cc | 19 | int current_weight; |
hi1000 | 19:0356e54240cc | 20 | //EEPROM ep(SDA,SCL,EEPROM_ADDR,EEPROM::T24C256); |
hi1000 | 19:0356e54240cc | 21 | int device_address = DEVICE_DEFAULT_ADDRESS; // address 11 bits: last 8 bits is device_address |
hi1000 | 19:0356e54240cc | 22 | int device_type; //address 11 bits: first 3 bits is device_type |
hi1000 | 19:0356e54240cc | 23 | int init_id = 0x00300000; // first 11 bit is the address |
hi1000 | 19:0356e54240cc | 24 | int init_filter_handle, broadcast_filter_handle; |
hi1000 | 19:0356e54240cc | 25 | int broadcast_id = 0x1ffC0000; |
hi1000 | 19:0356e54240cc | 26 | #endif |
hi1000 | 19:0356e54240cc | 27 | |
hi1000 | 19:0356e54240cc | 28 | |
hi1000 | 19:0356e54240cc | 29 | #if 1 |
hi1000 | 19:0356e54240cc | 30 | |
hi1000 | 14:2e17a27f56b2 | 31 | EventFlags LCD_update_flags; |
hi1000 | 14:2e17a27f56b2 | 32 | extern void main_menu(); |
hi1000 | 7:e0c7e624c5fa | 33 | extern void analyzePayload(); |
hi1000 | 19:0356e54240cc | 34 | extern Device_Type_d device_type_v; |
hi1000 | 19:0356e54240cc | 35 | extern data_field_d can_txdata_frame; |
hi1000 | 19:0356e54240cc | 36 | extern CANMessage tx_message; |
hi1000 | 11:8702316d7fc8 | 37 | #if 1 |
hi1000 | 11:8702316d7fc8 | 38 | #ifdef STM32F207xx |
hi1000 | 11:8702316d7fc8 | 39 | HX711 hx711(PB_11, PB_10);// data, clk |
hi1000 | 11:8702316d7fc8 | 40 | #endif |
hi1000 | 11:8702316d7fc8 | 41 | |
hi1000 | 11:8702316d7fc8 | 42 | #ifdef STM32F303xE |
hi1000 | 11:8702316d7fc8 | 43 | //HX711 hx711(D8, D9);// data, clk |
hi1000 | 19:0356e54240cc | 44 | #ifdef OWN_SOLDER_BOARD |
hi1000 | 11:8702316d7fc8 | 45 | HX711 hx711(PA_14, PA_15); |
hi1000 | 11:8702316d7fc8 | 46 | #endif |
hi1000 | 19:0356e54240cc | 47 | #ifdef YODA2 |
hi1000 | 19:0356e54240cc | 48 | HX711 hx711(PA_1, PA_0); |
hi1000 | 19:0356e54240cc | 49 | #endif |
hi1000 | 19:0356e54240cc | 50 | #endif |
hi1000 | 18:e6ed582f7022 | 51 | extern void scaleCalibration(bool release_led); |
hi1000 | 11:8702316d7fc8 | 52 | extern void init_scale(); |
hi1000 | 11:8702316d7fc8 | 53 | Thread scale_thread; |
hi1000 | 11:8702316d7fc8 | 54 | extern void scale_reading(); |
hi1000 | 9:486f65124378 | 55 | #endif |
hi1000 | 8:6105ffbaf237 | 56 | unsigned char rx_buffer[8], tx_buffer[8]; |
hi1000 | 7:e0c7e624c5fa | 57 | unsigned char rx_length, tx_length; |
hi1000 | 0:765cf978c3e5 | 58 | |
hi1000 | 19:0356e54240cc | 59 | //#define LCD_1602 |
hi1000 | 9:486f65124378 | 60 | #ifdef STM32F207xx |
hi1000 | 5:4585215afd11 | 61 | SB1602E lcd( PB_9, PB_8 ); // SDA, SCL |
hi1000 | 0:765cf978c3e5 | 62 | CAN can1(PD_0, PD_1); |
hi1000 | 0:765cf978c3e5 | 63 | CAN can2(PB_5, PB_6); |
hi1000 | 1:eb499e2a1b9b | 64 | DigitalOut led1(LED1); |
hi1000 | 1:eb499e2a1b9b | 65 | DigitalOut led2(LED2); |
hi1000 | 1:eb499e2a1b9b | 66 | //FlashIAP flashIAP; |
hi1000 | 1:eb499e2a1b9b | 67 | |
hi1000 | 5:4585215afd11 | 68 | //#define LCD_1621 |
hi1000 | 5:4585215afd11 | 69 | //digitLCD lcd(PA_5,PA_4,PB_5); // WO, CS, DATA |
hi1000 | 9:486f65124378 | 70 | #endif |
hi1000 | 9:486f65124378 | 71 | #ifdef STM32F303xE |
hi1000 | 19:0356e54240cc | 72 | |
hi1000 | 19:0356e54240cc | 73 | #ifdef LCD_1602 |
hi1000 | 9:486f65124378 | 74 | SB1602E lcd(D14, D15 ); // SDA, SCL |
hi1000 | 19:0356e54240cc | 75 | #endif |
hi1000 | 9:486f65124378 | 76 | CAN can1(PA_11, PA_12); // RD, TD |
hi1000 | 9:486f65124378 | 77 | DigitalOut led1(LED1); // only one LED PA_5 |
hi1000 | 9:486f65124378 | 78 | DigitalOut led2(LED2); // only one LED PA_5 |
hi1000 | 9:486f65124378 | 79 | #endif |
hi1000 | 7:e0c7e624c5fa | 80 | |
hi1000 | 19:0356e54240cc | 81 | #if 0 |
hi1000 | 12:5cb359f981f3 | 82 | DigitalOut output1(PC_3); |
hi1000 | 12:5cb359f981f3 | 83 | DigitalOut output2(PC_2); |
hi1000 | 12:5cb359f981f3 | 84 | DigitalOut output3(PB_7); |
hi1000 | 12:5cb359f981f3 | 85 | DigitalIn input1(PC_6); |
hi1000 | 12:5cb359f981f3 | 86 | DigitalIn input2(PC_8); |
hi1000 | 19:0356e54240cc | 87 | #endif |
hi1000 | 19:0356e54240cc | 88 | |
hi1000 | 1:eb499e2a1b9b | 89 | |
hi1000 | 9:486f65124378 | 90 | uint8_t can_tx_data[8]; |
hi1000 | 9:486f65124378 | 91 | uint8_t can_rx_data[8]; |
hi1000 | 19:0356e54240cc | 92 | #endif |
hi1000 | 0:765cf978c3e5 | 93 | void print_char(char c = '*') |
hi1000 | 0:765cf978c3e5 | 94 | { |
hi1000 | 0:765cf978c3e5 | 95 | printf("%c\r\n", c); |
hi1000 | 0:765cf978c3e5 | 96 | fflush(stdout); |
hi1000 | 0:765cf978c3e5 | 97 | } |
hi1000 | 0:765cf978c3e5 | 98 | |
hi1000 | 19:0356e54240cc | 99 | #if 1 |
hi1000 | 8:6105ffbaf237 | 100 | Thread can_receivethread; |
hi1000 | 8:6105ffbaf237 | 101 | Thread can_handlethread; |
hi1000 | 14:2e17a27f56b2 | 102 | Thread mainmenu_thread; |
hi1000 | 19:0356e54240cc | 103 | bool can_register_success = false; |
hi1000 | 0:765cf978c3e5 | 104 | CANMessage msg; |
hi1000 | 8:6105ffbaf237 | 105 | MemoryPool<CANMessage, 16> can_mpool; |
hi1000 | 8:6105ffbaf237 | 106 | Queue<CANMessage, 16> can_queue; |
hi1000 | 19:0356e54240cc | 107 | #endif |
hi1000 | 12:5cb359f981f3 | 108 | InterruptIn button0(USER_BUTTON); |
hi1000 | 12:5cb359f981f3 | 109 | volatile bool button0_pressed = false; // Used in the main loop |
hi1000 | 12:5cb359f981f3 | 110 | volatile bool button0_enabled = true; // Used for debouncing |
hi1000 | 12:5cb359f981f3 | 111 | Timeout button0_timeout; // Used for debouncing |
hi1000 | 19:0356e54240cc | 112 | InterruptIn button1(BUTTON1); |
hi1000 | 0:765cf978c3e5 | 113 | volatile bool button1_pressed = false; // Used in the main loop |
hi1000 | 0:765cf978c3e5 | 114 | volatile bool button1_enabled = true; // Used for debouncing |
hi1000 | 0:765cf978c3e5 | 115 | Timeout button1_timeout; // Used for debouncing |
hi1000 | 19:0356e54240cc | 116 | InterruptIn button2(BUTTON2); |
hi1000 | 12:5cb359f981f3 | 117 | volatile bool button2_pressed = false; // Used in the main loop |
hi1000 | 12:5cb359f981f3 | 118 | volatile bool button2_enabled = true; // Used for debouncing |
hi1000 | 12:5cb359f981f3 | 119 | Timeout button2_timeout; // Used for debouncing |
hi1000 | 19:0356e54240cc | 120 | InterruptIn button3(BUTTON3); |
hi1000 | 12:5cb359f981f3 | 121 | volatile bool button3_pressed = false; // Used in the main loop |
hi1000 | 12:5cb359f981f3 | 122 | volatile bool button3_enabled = true; // Used for debouncing |
hi1000 | 12:5cb359f981f3 | 123 | Timeout button3_timeout; // Used for debouncing |
hi1000 | 19:0356e54240cc | 124 | InterruptIn button4(BUTTON4); |
hi1000 | 12:5cb359f981f3 | 125 | volatile bool button4_pressed = false; // Used in the main loop |
hi1000 | 12:5cb359f981f3 | 126 | volatile bool button4_enabled = true; // Used for debouncing |
hi1000 | 12:5cb359f981f3 | 127 | Timeout button4_timeout; // Used for debouncing |
hi1000 | 12:5cb359f981f3 | 128 | |
hi1000 | 0:765cf978c3e5 | 129 | |
hi1000 | 0:765cf978c3e5 | 130 | // Enables button when bouncing is over |
hi1000 | 12:5cb359f981f3 | 131 | //button0 |
hi1000 | 12:5cb359f981f3 | 132 | void button0_enabled_cb(void) |
hi1000 | 12:5cb359f981f3 | 133 | { |
hi1000 | 12:5cb359f981f3 | 134 | int button_status; |
hi1000 | 12:5cb359f981f3 | 135 | button_status = button0.read(); |
hi1000 | 12:5cb359f981f3 | 136 | if (button_status == 0) |
hi1000 | 12:5cb359f981f3 | 137 | { |
hi1000 | 12:5cb359f981f3 | 138 | printf("button0 down\r\n"); |
hi1000 | 18:e6ed582f7022 | 139 | scaleCalibration(true); |
hi1000 | 14:2e17a27f56b2 | 140 | button_status = button0.read(); |
hi1000 | 14:2e17a27f56b2 | 141 | if (button_status == 0) |
hi1000 | 14:2e17a27f56b2 | 142 | { |
hi1000 | 14:2e17a27f56b2 | 143 | printf("button0 hold\r\n"); |
hi1000 | 14:2e17a27f56b2 | 144 | } |
hi1000 | 14:2e17a27f56b2 | 145 | else |
hi1000 | 14:2e17a27f56b2 | 146 | { |
hi1000 | 14:2e17a27f56b2 | 147 | printf("button0 press hold and release\r\n"); |
hi1000 | 14:2e17a27f56b2 | 148 | } |
hi1000 | 12:5cb359f981f3 | 149 | } |
hi1000 | 12:5cb359f981f3 | 150 | else |
hi1000 | 12:5cb359f981f3 | 151 | printf("button0 released\r\n"); |
hi1000 | 12:5cb359f981f3 | 152 | button0_enabled = true; |
hi1000 | 12:5cb359f981f3 | 153 | } |
hi1000 | 12:5cb359f981f3 | 154 | |
hi1000 | 12:5cb359f981f3 | 155 | // ISR handling button pressed event |
hi1000 | 12:5cb359f981f3 | 156 | void button0_onpressed_cb(void) |
hi1000 | 12:5cb359f981f3 | 157 | { |
hi1000 | 12:5cb359f981f3 | 158 | if (button0_enabled) { // Disabled while the button is bouncing |
hi1000 | 12:5cb359f981f3 | 159 | button0_enabled = false; |
hi1000 | 12:5cb359f981f3 | 160 | button0_pressed = true; // To be read by the main loop |
hi1000 | 12:5cb359f981f3 | 161 | button0_timeout.attach(callback(button0_enabled_cb), 0.3); // Debounce time 300 ms |
hi1000 | 12:5cb359f981f3 | 162 | } |
hi1000 | 12:5cb359f981f3 | 163 | } |
hi1000 | 12:5cb359f981f3 | 164 | //button0-- |
hi1000 | 12:5cb359f981f3 | 165 | //button1 |
hi1000 | 0:765cf978c3e5 | 166 | void button1_enabled_cb(void) |
hi1000 | 0:765cf978c3e5 | 167 | { |
hi1000 | 12:5cb359f981f3 | 168 | int button_status; |
hi1000 | 12:5cb359f981f3 | 169 | button_status = button1.read(); |
hi1000 | 12:5cb359f981f3 | 170 | if (button_status == 0) |
hi1000 | 12:5cb359f981f3 | 171 | { |
hi1000 | 12:5cb359f981f3 | 172 | printf("button1 down\r\n"); |
hi1000 | 14:2e17a27f56b2 | 173 | button_event_flags.set(BUTTON1_HOLD_EVENT); |
hi1000 | 12:5cb359f981f3 | 174 | } |
hi1000 | 12:5cb359f981f3 | 175 | else |
hi1000 | 14:2e17a27f56b2 | 176 | { |
hi1000 | 12:5cb359f981f3 | 177 | printf("button1 released\r\n"); |
hi1000 | 14:2e17a27f56b2 | 178 | button_event_flags.set(BUTTON1_PRESSED_EVENT); |
hi1000 | 14:2e17a27f56b2 | 179 | } |
hi1000 | 0:765cf978c3e5 | 180 | button1_enabled = true; |
hi1000 | 0:765cf978c3e5 | 181 | } |
hi1000 | 0:765cf978c3e5 | 182 | |
hi1000 | 0:765cf978c3e5 | 183 | // ISR handling button pressed event |
hi1000 | 0:765cf978c3e5 | 184 | void button1_onpressed_cb(void) |
hi1000 | 0:765cf978c3e5 | 185 | { |
hi1000 | 0:765cf978c3e5 | 186 | if (button1_enabled) { // Disabled while the button is bouncing |
hi1000 | 0:765cf978c3e5 | 187 | button1_enabled = false; |
hi1000 | 0:765cf978c3e5 | 188 | button1_pressed = true; // To be read by the main loop |
hi1000 | 0:765cf978c3e5 | 189 | button1_timeout.attach(callback(button1_enabled_cb), 0.3); // Debounce time 300 ms |
hi1000 | 0:765cf978c3e5 | 190 | } |
hi1000 | 0:765cf978c3e5 | 191 | } |
hi1000 | 12:5cb359f981f3 | 192 | //button1-- |
hi1000 | 12:5cb359f981f3 | 193 | //button2 |
hi1000 | 12:5cb359f981f3 | 194 | void button2_enabled_cb(void) |
hi1000 | 12:5cb359f981f3 | 195 | { |
hi1000 | 12:5cb359f981f3 | 196 | int button_status; |
hi1000 | 12:5cb359f981f3 | 197 | button_status = button2.read(); |
hi1000 | 12:5cb359f981f3 | 198 | if (button_status == 0) |
hi1000 | 12:5cb359f981f3 | 199 | { |
hi1000 | 12:5cb359f981f3 | 200 | printf("button2 down\r\n"); |
hi1000 | 14:2e17a27f56b2 | 201 | button_event_flags.set(BUTTON2_HOLD_EVENT); |
hi1000 | 12:5cb359f981f3 | 202 | } |
hi1000 | 12:5cb359f981f3 | 203 | else |
hi1000 | 14:2e17a27f56b2 | 204 | { |
hi1000 | 12:5cb359f981f3 | 205 | printf("button2 released\r\n"); |
hi1000 | 14:2e17a27f56b2 | 206 | button_event_flags.set(BUTTON2_PRESSED_EVENT); |
hi1000 | 14:2e17a27f56b2 | 207 | } |
hi1000 | 12:5cb359f981f3 | 208 | button2_enabled = true; |
hi1000 | 12:5cb359f981f3 | 209 | } |
hi1000 | 0:765cf978c3e5 | 210 | |
hi1000 | 12:5cb359f981f3 | 211 | // ISR handling button pressed event |
hi1000 | 12:5cb359f981f3 | 212 | void button2_onpressed_cb(void) |
hi1000 | 12:5cb359f981f3 | 213 | { |
hi1000 | 12:5cb359f981f3 | 214 | if (button2_enabled) { // Disabled while the button is bouncing |
hi1000 | 12:5cb359f981f3 | 215 | button2_enabled = false; |
hi1000 | 12:5cb359f981f3 | 216 | button2_pressed = true; // To be read by the main loop |
hi1000 | 12:5cb359f981f3 | 217 | button2_timeout.attach(callback(button2_enabled_cb), 0.3); // Debounce time 300 ms |
hi1000 | 12:5cb359f981f3 | 218 | } |
hi1000 | 12:5cb359f981f3 | 219 | } |
hi1000 | 12:5cb359f981f3 | 220 | //button2-- |
hi1000 | 12:5cb359f981f3 | 221 | //button3 |
hi1000 | 12:5cb359f981f3 | 222 | void button3_enabled_cb(void) |
hi1000 | 12:5cb359f981f3 | 223 | { |
hi1000 | 12:5cb359f981f3 | 224 | int button_status; |
hi1000 | 12:5cb359f981f3 | 225 | button_status = button3.read(); |
hi1000 | 12:5cb359f981f3 | 226 | if (button_status == 0) |
hi1000 | 12:5cb359f981f3 | 227 | { |
hi1000 | 12:5cb359f981f3 | 228 | printf("button3 down\r\n"); |
hi1000 | 14:2e17a27f56b2 | 229 | button_event_flags.set(BUTTON3_HOLD_EVENT); |
hi1000 | 12:5cb359f981f3 | 230 | } |
hi1000 | 12:5cb359f981f3 | 231 | else |
hi1000 | 14:2e17a27f56b2 | 232 | { |
hi1000 | 12:5cb359f981f3 | 233 | printf("button3 released\r\n"); |
hi1000 | 14:2e17a27f56b2 | 234 | button_event_flags.set(BUTTON3_PRESSED_EVENT); |
hi1000 | 14:2e17a27f56b2 | 235 | } |
hi1000 | 12:5cb359f981f3 | 236 | button3_enabled = true; |
hi1000 | 12:5cb359f981f3 | 237 | } |
hi1000 | 12:5cb359f981f3 | 238 | |
hi1000 | 12:5cb359f981f3 | 239 | // ISR handling button pressed event |
hi1000 | 12:5cb359f981f3 | 240 | void button3_onpressed_cb(void) |
hi1000 | 12:5cb359f981f3 | 241 | { |
hi1000 | 12:5cb359f981f3 | 242 | if (button3_enabled) { // Disabled while the button is bouncing |
hi1000 | 12:5cb359f981f3 | 243 | button3_enabled = false; |
hi1000 | 12:5cb359f981f3 | 244 | button3_pressed = true; // To be read by the main loop |
hi1000 | 12:5cb359f981f3 | 245 | button3_timeout.attach(callback(button3_enabled_cb), 0.3); // Debounce time 300 ms |
hi1000 | 12:5cb359f981f3 | 246 | } |
hi1000 | 12:5cb359f981f3 | 247 | } |
hi1000 | 12:5cb359f981f3 | 248 | //button3-- |
hi1000 | 12:5cb359f981f3 | 249 | //button4 |
hi1000 | 12:5cb359f981f3 | 250 | void button4_enabled_cb(void) |
hi1000 | 12:5cb359f981f3 | 251 | { |
hi1000 | 12:5cb359f981f3 | 252 | int button_status; |
hi1000 | 12:5cb359f981f3 | 253 | button_status = button4.read(); |
hi1000 | 12:5cb359f981f3 | 254 | if (button_status == 0) |
hi1000 | 12:5cb359f981f3 | 255 | { |
hi1000 | 12:5cb359f981f3 | 256 | printf("button4 down\r\n"); |
hi1000 | 14:2e17a27f56b2 | 257 | button_event_flags.set(BUTTON4_HOLD_EVENT); |
hi1000 | 12:5cb359f981f3 | 258 | } |
hi1000 | 12:5cb359f981f3 | 259 | else |
hi1000 | 14:2e17a27f56b2 | 260 | { |
hi1000 | 12:5cb359f981f3 | 261 | printf("button4 released\r\n"); |
hi1000 | 14:2e17a27f56b2 | 262 | button_event_flags.set(BUTTON4_PRESSED_EVENT); |
hi1000 | 14:2e17a27f56b2 | 263 | } |
hi1000 | 12:5cb359f981f3 | 264 | button4_enabled = true; |
hi1000 | 12:5cb359f981f3 | 265 | } |
hi1000 | 12:5cb359f981f3 | 266 | |
hi1000 | 12:5cb359f981f3 | 267 | // ISR handling button pressed event |
hi1000 | 12:5cb359f981f3 | 268 | void button4_onpressed_cb(void) |
hi1000 | 12:5cb359f981f3 | 269 | { |
hi1000 | 12:5cb359f981f3 | 270 | if (button4_enabled) { // Disabled while the button is bouncing |
hi1000 | 12:5cb359f981f3 | 271 | button4_enabled = false; |
hi1000 | 12:5cb359f981f3 | 272 | button4_pressed = true; // To be read by the main loop |
hi1000 | 12:5cb359f981f3 | 273 | button4_timeout.attach(callback(button4_enabled_cb), 0.3); // Debounce time 300 ms |
hi1000 | 12:5cb359f981f3 | 274 | } |
hi1000 | 12:5cb359f981f3 | 275 | } |
hi1000 | 12:5cb359f981f3 | 276 | //button4-- |
hi1000 | 19:0356e54240cc | 277 | #if 1 |
hi1000 | 17:faa4d4976d22 | 278 | void can_sendData(int can_id, uint8_t *tx_data, int length) |
hi1000 | 17:faa4d4976d22 | 279 | { |
hi1000 | 17:faa4d4976d22 | 280 | CANMessage txmsg; |
hi1000 | 17:faa4d4976d22 | 281 | |
hi1000 | 17:faa4d4976d22 | 282 | txmsg.format = CANExtended; |
hi1000 | 17:faa4d4976d22 | 283 | txmsg.id = can_id; |
hi1000 | 17:faa4d4976d22 | 284 | txmsg.len = length; |
hi1000 | 17:faa4d4976d22 | 285 | txmsg.data[0] = tx_data[0]; |
hi1000 | 17:faa4d4976d22 | 286 | txmsg.data[1] = tx_data[1]; |
hi1000 | 17:faa4d4976d22 | 287 | txmsg.data[2] = tx_data[2]; |
hi1000 | 17:faa4d4976d22 | 288 | txmsg.data[3] = tx_data[3]; |
hi1000 | 17:faa4d4976d22 | 289 | txmsg.data[4] = tx_data[4]; |
hi1000 | 17:faa4d4976d22 | 290 | txmsg.data[5] = tx_data[5]; |
hi1000 | 17:faa4d4976d22 | 291 | txmsg.data[6] = tx_data[6]; |
hi1000 | 17:faa4d4976d22 | 292 | txmsg.data[7] = tx_data[7]; |
hi1000 | 17:faa4d4976d22 | 293 | |
hi1000 | 19:0356e54240cc | 294 | // printf("can_sendData can_id=0x%08x \r\n", can_id); |
hi1000 | 17:faa4d4976d22 | 295 | can1.write(txmsg); |
hi1000 | 17:faa4d4976d22 | 296 | } |
hi1000 | 17:faa4d4976d22 | 297 | |
hi1000 | 8:6105ffbaf237 | 298 | void can_rxthread() |
hi1000 | 0:765cf978c3e5 | 299 | { |
hi1000 | 9:486f65124378 | 300 | int loop; |
hi1000 | 0:765cf978c3e5 | 301 | while (true) { |
hi1000 | 9:486f65124378 | 302 | #if 1 |
hi1000 | 0:765cf978c3e5 | 303 | if(can1.read(msg)) { |
hi1000 | 0:765cf978c3e5 | 304 | print_char(); |
hi1000 | 0:765cf978c3e5 | 305 | printf("got message id=%d 0x%08x\r\n", msg.id, msg.id); |
hi1000 | 0:765cf978c3e5 | 306 | // b = *reinterpret_cast<int*>(msg.data); |
hi1000 | 9:486f65124378 | 307 | for (loop = 0; loop < msg.len; loop++) |
hi1000 | 8:6105ffbaf237 | 308 | { |
hi1000 | 9:486f65124378 | 309 | can_rx_data[loop] = msg.data[loop]; |
hi1000 | 8:6105ffbaf237 | 310 | } |
hi1000 | 8:6105ffbaf237 | 311 | |
hi1000 | 9:486f65124378 | 312 | printf("got data: length:%d\r\n", msg.len); |
hi1000 | 9:486f65124378 | 313 | for (loop = 0; loop < msg.len; loop++) |
hi1000 | 9:486f65124378 | 314 | { |
hi1000 | 9:486f65124378 | 315 | printf("data[%d]=%d\r\n", loop, can_rx_data[loop]); |
hi1000 | 9:486f65124378 | 316 | } |
hi1000 | 14:2e17a27f56b2 | 317 | // if(msg.id == 1337) |
hi1000 | 14:2e17a27f56b2 | 318 | { |
hi1000 | 9:486f65124378 | 319 | //only queue the message belongs to you |
hi1000 | 9:486f65124378 | 320 | CANMessage *can_message = can_mpool.alloc(); |
hi1000 | 9:486f65124378 | 321 | memcpy((void *)can_message, (void *)&msg, sizeof(msg)); |
hi1000 | 9:486f65124378 | 322 | if (!can_queue.full()) |
hi1000 | 9:486f65124378 | 323 | can_queue.put(can_message); |
hi1000 | 9:486f65124378 | 324 | else |
hi1000 | 9:486f65124378 | 325 | { |
hi1000 | 9:486f65124378 | 326 | printf("message queue is full. \r\n"); |
hi1000 | 9:486f65124378 | 327 | } |
hi1000 | 0:765cf978c3e5 | 328 | led2 = !led2; |
hi1000 | 0:765cf978c3e5 | 329 | } |
hi1000 | 0:765cf978c3e5 | 330 | } |
hi1000 | 9:486f65124378 | 331 | #endif |
hi1000 | 0:765cf978c3e5 | 332 | // wait(0.2); |
hi1000 | 0:765cf978c3e5 | 333 | } |
hi1000 | 0:765cf978c3e5 | 334 | } |
hi1000 | 19:0356e54240cc | 335 | #endif |
hi1000 | 19:0356e54240cc | 336 | void i2c_scanner(PinName sda, PinName scl) |
hi1000 | 19:0356e54240cc | 337 | { |
hi1000 | 19:0356e54240cc | 338 | I2C i2c(sda, scl); |
hi1000 | 19:0356e54240cc | 339 | |
hi1000 | 19:0356e54240cc | 340 | int error, address; |
hi1000 | 19:0356e54240cc | 341 | int nDevices; |
hi1000 | 19:0356e54240cc | 342 | |
hi1000 | 19:0356e54240cc | 343 | i2c.frequency(100000); |
hi1000 | 19:0356e54240cc | 344 | printf("i2c_scanner sda=%d scl=%d \r\n", (int)sda, (int)scl); |
hi1000 | 19:0356e54240cc | 345 | printf("Scanning...\r\n"); |
hi1000 | 19:0356e54240cc | 346 | |
hi1000 | 19:0356e54240cc | 347 | nDevices = 0; |
hi1000 | 19:0356e54240cc | 348 | |
hi1000 | 19:0356e54240cc | 349 | for(address = 0; address < 127; address++ ) |
hi1000 | 19:0356e54240cc | 350 | { |
hi1000 | 19:0356e54240cc | 351 | i2c.start(); |
hi1000 | 19:0356e54240cc | 352 | // error = i2c.write(address << 1); //We shift it left because mbed takes in 8 bit addreses |
hi1000 | 19:0356e54240cc | 353 | error = i2c.write(address << 1, "1", 1, false); |
hi1000 | 19:0356e54240cc | 354 | i2c.stop(); |
hi1000 | 19:0356e54240cc | 355 | if (error == 0) |
hi1000 | 19:0356e54240cc | 356 | { |
hi1000 | 19:0356e54240cc | 357 | printf("I2C device found at address 7bit:0x%X (8bit:0x%X)\r\n", address, (address<<1)); //Returns 8-bit addres |
hi1000 | 19:0356e54240cc | 358 | nDevices++; |
hi1000 | 19:0356e54240cc | 359 | } |
hi1000 | 19:0356e54240cc | 360 | } |
hi1000 | 19:0356e54240cc | 361 | if (nDevices == 0) |
hi1000 | 19:0356e54240cc | 362 | printf("No I2C devices found\r\n"); |
hi1000 | 19:0356e54240cc | 363 | else |
hi1000 | 19:0356e54240cc | 364 | printf("\r\ndone\r\n"); |
hi1000 | 19:0356e54240cc | 365 | } |
hi1000 | 0:765cf978c3e5 | 366 | int main() |
hi1000 | 0:765cf978c3e5 | 367 | { |
hi1000 | 9:486f65124378 | 368 | int loop = 0; |
hi1000 | 9:486f65124378 | 369 | int8_t ival; |
hi1000 | 19:0356e54240cc | 370 | unsigned int can_id; |
hi1000 | 14:2e17a27f56b2 | 371 | printf("\n\n*** RTOS starts ***\r\n"); |
hi1000 | 9:486f65124378 | 372 | |
hi1000 | 9:486f65124378 | 373 | // wait(1); |
hi1000 | 19:0356e54240cc | 374 | #if 1 |
hi1000 | 14:2e17a27f56b2 | 375 | ep.read((uint32_t)EEPROM_DEVICE_ADDRESS_ADDRESS,device_address); |
hi1000 | 14:2e17a27f56b2 | 376 | printf("EEPROM: read device address:%d 0x%08x\r\n", device_address, device_address); |
hi1000 | 14:2e17a27f56b2 | 377 | if ((device_address == 0) || (device_address == 0xFFFFFFFF)) |
hi1000 | 14:2e17a27f56b2 | 378 | device_address = DEVICE_DEFAULT_ADDRESS; |
hi1000 | 16:f4277e9b8612 | 379 | device_type = (device_address & 0x00000700) >> 8; |
hi1000 | 19:0356e54240cc | 380 | device_type_v = (Device_Type_d)device_type; |
hi1000 | 8:6105ffbaf237 | 381 | #ifdef LCD_1621 |
hi1000 | 4:40bb33497de4 | 382 | lcd.clear(); // clears display |
hi1000 | 4:40bb33497de4 | 383 | lcd.allsegson(); |
hi1000 | 5:4585215afd11 | 384 | |
hi1000 | 4:40bb33497de4 | 385 | // lcd.printf("ABCDEFGHI"); // Standard printf function, All ASCII characters will display |
hi1000 | 5:4585215afd11 | 386 | #endif |
hi1000 | 19:0356e54240cc | 387 | #ifdef YODA2 |
hi1000 | 19:0356e54240cc | 388 | lcd.cls(); |
hi1000 | 19:0356e54240cc | 389 | lcd.setContrast(31); |
hi1000 | 19:0356e54240cc | 390 | lcd.setCursor(TextLCD::CurOff_BlkOff); |
hi1000 | 19:0356e54240cc | 391 | lcd.setAddress(0,0); |
hi1000 | 19:0356e54240cc | 392 | lcd.printf("Hello Yoda2!"); |
hi1000 | 19:0356e54240cc | 393 | #endif |
hi1000 | 5:4585215afd11 | 394 | #ifdef LCD_1602 |
hi1000 | 16:f4277e9b8612 | 395 | // lcd.printf( 0, "Hello world!" ); // line# (0 or 1), string |
hi1000 | 16:f4277e9b8612 | 396 | // lcd.printf( 1, "pi = %.6f", 3.14159265 ); |
hi1000 | 16:f4277e9b8612 | 397 | // lcd.putcxy(0x55, 5, 1); |
hi1000 | 16:f4277e9b8612 | 398 | // lcd.printf(5, 0, "UUU"); |
hi1000 | 16:f4277e9b8612 | 399 | // lcd.printf(0, 0, "pressed!" ); |
hi1000 | 9:486f65124378 | 400 | #endif |
hi1000 | 11:8702316d7fc8 | 401 | |
hi1000 | 19:0356e54240cc | 402 | // input1.mode(PullUp); |
hi1000 | 19:0356e54240cc | 403 | // input2.mode(PullUp); |
hi1000 | 13:9f581a090e53 | 404 | |
hi1000 | 0:765cf978c3e5 | 405 | // can1.reset(); |
hi1000 | 0:765cf978c3e5 | 406 | // can2.reset(); |
hi1000 | 16:f4277e9b8612 | 407 | printf("device_address =0x%08x \r\n", (device_address<<18)); |
hi1000 | 2:61a0169765bf | 408 | can1.frequency(100000); |
hi1000 | 14:2e17a27f56b2 | 409 | can1.filter((device_address<<18), 0x1FFC0000, CANExtended, init_filter_handle); // 0x1FFC0000 to filter the last 18bits 0-17 |
hi1000 | 16:f4277e9b8612 | 410 | device_address = (device_address & 0x000000FF); |
hi1000 | 14:2e17a27f56b2 | 411 | //only support one filter |
hi1000 | 14:2e17a27f56b2 | 412 | // can1.filter(broadcast_id, 0x1FFC0000, CANExtended, broadcast_filter_handle); // the broadcast id |
hi1000 | 0:765cf978c3e5 | 413 | // can2.frequency(100000); |
hi1000 | 12:5cb359f981f3 | 414 | //button0.mode(PullUp); // Activate pull-up |
hi1000 | 13:9f581a090e53 | 415 | can_receivethread.start(can_rxthread); |
hi1000 | 13:9f581a090e53 | 416 | can_handlethread.start(analyzePayload); |
hi1000 | 19:0356e54240cc | 417 | #endif |
hi1000 | 19:0356e54240cc | 418 | button1.mode(PullUp); |
hi1000 | 19:0356e54240cc | 419 | button2.mode(PullUp); |
hi1000 | 19:0356e54240cc | 420 | button3.mode(PullUp); |
hi1000 | 19:0356e54240cc | 421 | button4.mode(PullUp); |
hi1000 | 12:5cb359f981f3 | 422 | button0.fall(callback(button0_onpressed_cb)); // Attach ISR to handle button press event |
hi1000 | 12:5cb359f981f3 | 423 | button0.rise(callback(button0_onpressed_cb)); // Attach ISR to handle button press event |
hi1000 | 2:61a0169765bf | 424 | button1.fall(callback(button1_onpressed_cb)); // Attach ISR to handle button press event |
hi1000 | 12:5cb359f981f3 | 425 | button1.rise(callback(button1_onpressed_cb)); // Attach ISR to handle button press event |
hi1000 | 12:5cb359f981f3 | 426 | button2.fall(callback(button2_onpressed_cb)); // Attach ISR to handle button press event |
hi1000 | 12:5cb359f981f3 | 427 | button2.rise(callback(button2_onpressed_cb)); // Attach ISR to handle button press event |
hi1000 | 12:5cb359f981f3 | 428 | button3.fall(callback(button3_onpressed_cb)); // Attach ISR to handle button press event |
hi1000 | 12:5cb359f981f3 | 429 | button3.rise(callback(button3_onpressed_cb)); // Attach ISR to handle button press event |
hi1000 | 12:5cb359f981f3 | 430 | button4.fall(callback(button4_onpressed_cb)); // Attach ISR to handle button press event |
hi1000 | 12:5cb359f981f3 | 431 | button4.rise(callback(button4_onpressed_cb)); // Attach ISR to handle button press event |
hi1000 | 19:0356e54240cc | 432 | // i2c_scanner(PB_9, PB_8); |
hi1000 | 11:8702316d7fc8 | 433 | // eeprom_test(); |
hi1000 | 0:765cf978c3e5 | 434 | |
hi1000 | 11:8702316d7fc8 | 435 | #if 1 |
hi1000 | 18:e6ed582f7022 | 436 | // scaleCalibration(true); |
hi1000 | 19:0356e54240cc | 437 | switch (device_type_v) |
hi1000 | 19:0356e54240cc | 438 | { |
hi1000 | 19:0356e54240cc | 439 | case CupTrack: |
hi1000 | 19:0356e54240cc | 440 | case JamTrack: |
hi1000 | 19:0356e54240cc | 441 | case TeaTrack: |
hi1000 | 19:0356e54240cc | 442 | break; |
hi1000 | 19:0356e54240cc | 443 | case Tea: |
hi1000 | 19:0356e54240cc | 444 | case Jam: |
hi1000 | 19:0356e54240cc | 445 | init_scale(); |
hi1000 | 19:0356e54240cc | 446 | scale_thread.start(scale_reading); |
hi1000 | 19:0356e54240cc | 447 | break; |
hi1000 | 19:0356e54240cc | 448 | case Shaker: |
hi1000 | 19:0356e54240cc | 449 | break; |
hi1000 | 19:0356e54240cc | 450 | default: |
hi1000 | 19:0356e54240cc | 451 | break; |
hi1000 | 19:0356e54240cc | 452 | } |
hi1000 | 19:0356e54240cc | 453 | |
hi1000 | 14:2e17a27f56b2 | 454 | mainmenu_thread.start(main_menu); |
hi1000 | 14:2e17a27f56b2 | 455 | |
hi1000 | 11:8702316d7fc8 | 456 | #endif |
hi1000 | 14:2e17a27f56b2 | 457 | while(1) { |
hi1000 | 19:0356e54240cc | 458 | wait(2); |
hi1000 | 19:0356e54240cc | 459 | #if 1 |
hi1000 | 19:0356e54240cc | 460 | if (!can_register_success) |
hi1000 | 19:0356e54240cc | 461 | { |
hi1000 | 19:0356e54240cc | 462 | can_txdata_frame.cmd = COMMAND_REGISTER; |
hi1000 | 19:0356e54240cc | 463 | can_txdata_frame.value1 = 0; |
hi1000 | 19:0356e54240cc | 464 | can_txdata_frame.value2 = 0; |
hi1000 | 19:0356e54240cc | 465 | can_txdata_frame.value3 = 0; |
hi1000 | 19:0356e54240cc | 466 | memcpy(can_tx_data, (unsigned char *)&can_txdata_frame, sizeof(can_tx_data)); |
hi1000 | 19:0356e54240cc | 467 | // printf("cmd=0x%08x value1=0x%08x size=%d %d\r\n", can_txdata_frame.cmd, can_txdata_frame.value1, sizeof(can_tx_data), sizeof(can_txdata_frame)); |
hi1000 | 19:0356e54240cc | 468 | // printf("data 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x \r\n", can_tx_data[0], can_tx_data[1], can_tx_data[2], can_tx_data[3], can_tx_data[4], can_tx_data[5], can_tx_data[6], can_tx_data[7]); |
hi1000 | 19:0356e54240cc | 469 | can_id = (RASPBERRY_PI_CAN_ADDRESS << 18) | (((device_type << 8) |device_address) << 7) | 0x80000000; |
hi1000 | 19:0356e54240cc | 470 | // printf("device_address = 0x%08x can_id=0x%08x \r\n", device_address, can_id); |
hi1000 | 19:0356e54240cc | 471 | can_sendData(can_id, can_tx_data, 8); |
hi1000 | 19:0356e54240cc | 472 | } |
hi1000 | 19:0356e54240cc | 473 | #endif |
hi1000 | 14:2e17a27f56b2 | 474 | } |
hi1000 | 14:2e17a27f56b2 | 475 | |
hi1000 | 14:2e17a27f56b2 | 476 | #if 0 |
hi1000 | 0:765cf978c3e5 | 477 | int idx = 0; // Just for printf below |
hi1000 | 9:486f65124378 | 478 | can_tx_data[0] = 0; |
hi1000 | 0:765cf978c3e5 | 479 | while(1) { |
hi1000 | 12:5cb359f981f3 | 480 | if (button0_pressed) { // Set when button is pressed |
hi1000 | 9:486f65124378 | 481 | #if 0 |
hi1000 | 1:eb499e2a1b9b | 482 | printf("scale value %f. \r\n", hx711.getGram()); |
hi1000 | 9:486f65124378 | 483 | #endif |
hi1000 | 9:486f65124378 | 484 | can_tx_data[1] = can_tx_data[0]+1; |
hi1000 | 9:486f65124378 | 485 | can_tx_data[2] = can_tx_data[1]+1; |
hi1000 | 9:486f65124378 | 486 | can_tx_data[3] = can_tx_data[2]+1; |
hi1000 | 9:486f65124378 | 487 | can_tx_data[4] = can_tx_data[3]+1; |
hi1000 | 9:486f65124378 | 488 | can_tx_data[5] = can_tx_data[4]+1; |
hi1000 | 9:486f65124378 | 489 | can_tx_data[6] = can_tx_data[5]+1; |
hi1000 | 9:486f65124378 | 490 | can_tx_data[7] = can_tx_data[6]+1; |
hi1000 | 12:5cb359f981f3 | 491 | button0_pressed = false; |
hi1000 | 2:61a0169765bf | 492 | printf("Button pressed %d\r\n", idx++); |
hi1000 | 9:486f65124378 | 493 | printf("ID=%d data[0]=%d. \r\n", init_id + idx%10, can_tx_data[0]); |
hi1000 | 9:486f65124378 | 494 | #ifdef LCD_1602 |
hi1000 | 9:486f65124378 | 495 | lcd.printf(0, 0, "%d ", idx ); // line# (0 or 1), string |
hi1000 | 9:486f65124378 | 496 | #endif |
hi1000 | 17:faa4d4976d22 | 497 | can1.write(CANMessage((init_id + idx%10), reinterpret_cast<char*>(can_tx_data), 8, CANData,CANExtended)); |
hi1000 | 0:765cf978c3e5 | 498 | led1 = !led1; |
hi1000 | 9:486f65124378 | 499 | can_tx_data[0]++; |
hi1000 | 0:765cf978c3e5 | 500 | } |
hi1000 | 0:765cf978c3e5 | 501 | } |
hi1000 | 14:2e17a27f56b2 | 502 | #endif |
hi1000 | 9:486f65124378 | 503 | |
hi1000 | 0:765cf978c3e5 | 504 | } |