Feng Hong / Mbed OS Nucleo_rtos_basic
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?

UserRevisionLine numberNew contents of line
hi1000 7:e0c7e624c5fa 1 #include "mbed.h"
hi1000 18:e6ed582f7022 2 #include "yoda2.h"
hi1000 19:0356e54240cc 3 #if 1
hi1000 8:6105ffbaf237 4 extern unsigned char rx_buffer[8], tx_buffer[8];
hi1000 7:e0c7e624c5fa 5 extern unsigned char rx_length, tx_length;
hi1000 9:486f65124378 6 extern uint8_t can_tx_data[8];
hi1000 9:486f65124378 7 extern uint8_t can_rx_data[8];
hi1000 17:faa4d4976d22 8 extern int current_weight;
hi1000 19:0356e54240cc 9 extern bool can_register_success;
hi1000 16:f4277e9b8612 10
hi1000 16:f4277e9b8612 11 typedef struct can_id_d {
hi1000 16:f4277e9b8612 12 int dest_id;
hi1000 16:f4277e9b8612 13 int source_id;
hi1000 16:f4277e9b8612 14 int message_id;
hi1000 16:f4277e9b8612 15 };
hi1000 16:f4277e9b8612 16
hi1000 16:f4277e9b8612 17 can_id_d can_id_s;
hi1000 16:f4277e9b8612 18 data_field_d can_rxdata_frame;
hi1000 17:faa4d4976d22 19 data_field_d can_txdata_frame;
hi1000 9:486f65124378 20 CANMessage tx_message;
hi1000 9:486f65124378 21 extern MemoryPool<CANMessage, 16> can_mpool;
hi1000 9:486f65124378 22 extern Queue<CANMessage, 16> can_queue;
hi1000 17:faa4d4976d22 23 extern void can_sendData(int can_id, uint8_t *tx_data, int length);
hi1000 18:e6ed582f7022 24 extern Device_Type_d device_type_v;
hi1000 18:e6ed582f7022 25 extern int device_address; // last 8 bits of address
hi1000 18:e6ed582f7022 26 extern int device_type; // first 3 bits of adddress
hi1000 18:e6ed582f7022 27
hi1000 19:0356e54240cc 28
hi1000 18:e6ed582f7022 29 void handleCupTrackCommand(data_field_d data_pack)
hi1000 18:e6ed582f7022 30 {
hi1000 18:e6ed582f7022 31 }
hi1000 18:e6ed582f7022 32 void handleJamTrackCommand(data_field_d data_pack)
hi1000 18:e6ed582f7022 33 {
hi1000 18:e6ed582f7022 34 }
hi1000 18:e6ed582f7022 35 void handleTeaTrackCommand(data_field_d data_pack)
hi1000 18:e6ed582f7022 36 {
hi1000 18:e6ed582f7022 37 }
hi1000 18:e6ed582f7022 38 void handleJamCommand(data_field_d data_pack)
hi1000 18:e6ed582f7022 39 {
hi1000 18:e6ed582f7022 40 }
hi1000 18:e6ed582f7022 41 void handleTeaCommand(data_field_d data_pack)
hi1000 18:e6ed582f7022 42 {
hi1000 18:e6ed582f7022 43 int can_id;
hi1000 19:0356e54240cc 44 int init_weight;
hi1000 19:0356e54240cc 45 int needed_weight;
hi1000 19:0356e54240cc 46 int loop;
hi1000 18:e6ed582f7022 47 switch (data_pack.cmd)
hi1000 18:e6ed582f7022 48 {
hi1000 18:e6ed582f7022 49 case COMMAND_INIT:
hi1000 18:e6ed582f7022 50 can_txdata_frame.cmd = COMMAND_INIT;
hi1000 18:e6ed582f7022 51 can_txdata_frame.value1 = current_weight;
hi1000 18:e6ed582f7022 52 can_txdata_frame.value2 = 0;
hi1000 18:e6ed582f7022 53 can_txdata_frame.value3 = 0;
hi1000 19:0356e54240cc 54 memcpy(can_tx_data, (unsigned char *)&can_txdata_frame, sizeof(can_tx_data));
hi1000 19:0356e54240cc 55 // 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 56 // 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 18:e6ed582f7022 57 can_id = (can_id_s.source_id << 18) | (can_id_s.dest_id << 7) | can_id_s.message_id | 0x80000000;
hi1000 18:e6ed582f7022 58 can_sendData(can_id, can_tx_data, 8);
hi1000 18:e6ed582f7022 59 break;
hi1000 19:0356e54240cc 60 case COMMAND_PLUS:
hi1000 19:0356e54240cc 61 init_weight = current_weight;
hi1000 19:0356e54240cc 62 needed_weight = data_pack.value1;
hi1000 19:0356e54240cc 63 printf("init_weight:%dg needed_weight:%dg\r\n", init_weight, needed_weight);
hi1000 19:0356e54240cc 64 while ((current_weight - init_weight) <= needed_weight)
hi1000 19:0356e54240cc 65 {
hi1000 19:0356e54240cc 66 printf("still need to plus weight %dg \r\n", (current_weight - init_weight));
hi1000 19:0356e54240cc 67 }
hi1000 19:0356e54240cc 68 break;
hi1000 18:e6ed582f7022 69 default:
hi1000 18:e6ed582f7022 70 break;
hi1000 18:e6ed582f7022 71 }
hi1000 18:e6ed582f7022 72 }
hi1000 18:e6ed582f7022 73 void handleShakerCommand(data_field_d command)
hi1000 18:e6ed582f7022 74 {
hi1000 18:e6ed582f7022 75 }
hi1000 19:0356e54240cc 76 void handleIceMakerCommand(data_field_d command)
hi1000 19:0356e54240cc 77 {
hi1000 19:0356e54240cc 78 }
hi1000 7:e0c7e624c5fa 79 void analyzePayload()
hi1000 7:e0c7e624c5fa 80 {
hi1000 17:faa4d4976d22 81 int can_id;
hi1000 9:486f65124378 82 int loop;
hi1000 8:6105ffbaf237 83
hi1000 8:6105ffbaf237 84 printf("analyzePayload thread\r\n");
hi1000 8:6105ffbaf237 85 while (true) {
hi1000 8:6105ffbaf237 86 osEvent evt = can_queue.get();
hi1000 8:6105ffbaf237 87 if (evt.status == osEventMessage) {
hi1000 8:6105ffbaf237 88 CANMessage *message = (CANMessage*)evt.value.p;
hi1000 9:486f65124378 89 memcpy((void *)&tx_message, (void *)message, sizeof(tx_message));
hi1000 8:6105ffbaf237 90
hi1000 9:486f65124378 91 printf("analyzePayload got message id=%d 0x%08x\r\n", tx_message.id, tx_message.id);
hi1000 16:f4277e9b8612 92
hi1000 9:486f65124378 93 for (loop = 0; loop < tx_message.len; loop++)
hi1000 9:486f65124378 94 {
hi1000 9:486f65124378 95 can_rx_data[loop] = tx_message.data[loop];
hi1000 9:486f65124378 96 }
hi1000 9:486f65124378 97
hi1000 9:486f65124378 98 printf("analyzePayload got data: length:%d\r\n", tx_message.len);
hi1000 9:486f65124378 99 for (loop = 0; loop < tx_message.len; loop++)
hi1000 9:486f65124378 100 {
hi1000 9:486f65124378 101 printf("data[%d]=%d\r\n", loop, can_rx_data[loop]);
hi1000 9:486f65124378 102 }
hi1000 16:f4277e9b8612 103 can_id_s.dest_id = (tx_message.id & 0x1FFC0000) >> 18;
hi1000 16:f4277e9b8612 104 can_id_s.source_id = (tx_message.id & 0x0003FF80) >> 7;
hi1000 16:f4277e9b8612 105 can_id_s.message_id = (tx_message.id & 0x0000007F) >> 0;
hi1000 19:0356e54240cc 106 memcpy((void*)&can_rxdata_frame, (void*)can_rx_data, sizeof(can_rx_data));
hi1000 16:f4277e9b8612 107 can_mpool.free(message);
hi1000 16:f4277e9b8612 108 printf("dest_id=%d source_id=%d message_id=%d \r\n", can_id_s.dest_id, can_id_s.source_id, can_id_s.message_id);
hi1000 19:0356e54240cc 109 // can_rxdata_frame.cmd = (can_rx_data[1] << 8 ) | can_rx_data[0];
hi1000 19:0356e54240cc 110 // can_rxdata_frame.value1 = (can_rx_data[3] << 8 ) | can_rx_data[2];
hi1000 19:0356e54240cc 111 // can_rxdata_frame.value2 = (can_rx_data[5] << 8 ) | can_rx_data[4];
hi1000 19:0356e54240cc 112 // can_rxdata_frame.value3 = (can_rx_data[7] << 8 ) | can_rx_data[6];
hi1000 19:0356e54240cc 113 printf("cmd=%d, value1=0x%04x, value2=0x%04x, value3=0x%04x\r\n", can_rxdata_frame.cmd, can_rxdata_frame.value1, can_rxdata_frame.value2, can_rxdata_frame.value3);
hi1000 18:e6ed582f7022 114 device_type_v = (Device_Type_d)device_type;
hi1000 19:0356e54240cc 115 if (can_rxdata_frame.cmd == COMMAND_REGISTER)
hi1000 19:0356e54240cc 116 {
hi1000 19:0356e54240cc 117 can_register_success = true;
hi1000 19:0356e54240cc 118 printf("device register SUCCESS\r\n");
hi1000 19:0356e54240cc 119 }
hi1000 19:0356e54240cc 120 else
hi1000 17:faa4d4976d22 121 {
hi1000 19:0356e54240cc 122 switch (device_type_v)
hi1000 19:0356e54240cc 123 {
hi1000 19:0356e54240cc 124 case CupTrack:
hi1000 19:0356e54240cc 125 printf("CupTrack command: %d \r\n", can_rxdata_frame.cmd);
hi1000 19:0356e54240cc 126 handleCupTrackCommand(can_rxdata_frame);
hi1000 19:0356e54240cc 127 break;
hi1000 19:0356e54240cc 128 case JamTrack:
hi1000 19:0356e54240cc 129 printf("JamTrack command: %d \r\n", can_rxdata_frame.cmd);
hi1000 19:0356e54240cc 130 handleJamTrackCommand(can_rxdata_frame);
hi1000 19:0356e54240cc 131 break;
hi1000 19:0356e54240cc 132 case TeaTrack:
hi1000 19:0356e54240cc 133 printf("TeaTrack command: %d \r\n", can_rxdata_frame.cmd);
hi1000 19:0356e54240cc 134 handleTeaTrackCommand(can_rxdata_frame);
hi1000 19:0356e54240cc 135 break;
hi1000 19:0356e54240cc 136 case Tea:
hi1000 19:0356e54240cc 137 printf("Tea command: %d \r\n", can_rxdata_frame.cmd);
hi1000 19:0356e54240cc 138 handleTeaCommand(can_rxdata_frame);
hi1000 19:0356e54240cc 139 break;
hi1000 19:0356e54240cc 140 case Jam:
hi1000 19:0356e54240cc 141 printf("Jam command: %d \r\n", can_rxdata_frame.cmd);
hi1000 19:0356e54240cc 142 handleJamCommand(can_rxdata_frame);
hi1000 19:0356e54240cc 143 break;
hi1000 19:0356e54240cc 144 case Shaker:
hi1000 19:0356e54240cc 145 printf("Shaker command: %d \r\n", can_rxdata_frame.cmd);
hi1000 19:0356e54240cc 146 handleShakerCommand(can_rxdata_frame);
hi1000 19:0356e54240cc 147 break;
hi1000 19:0356e54240cc 148 case IceMaker:
hi1000 19:0356e54240cc 149 printf("IceMaker command: %d \r\n", can_rxdata_frame.cmd);
hi1000 19:0356e54240cc 150 handleIceMakerCommand(can_rxdata_frame);
hi1000 19:0356e54240cc 151 break;
hi1000 19:0356e54240cc 152 }
hi1000 17:faa4d4976d22 153 }
hi1000 8:6105ffbaf237 154 }
hi1000 8:6105ffbaf237 155 }
hi1000 8:6105ffbaf237 156
hi1000 7:e0c7e624c5fa 157
hi1000 19:0356e54240cc 158 }
hi1000 19:0356e54240cc 159 #endif