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