The Code Repository for the REV0 Steering Wheel.
Dependencies: CANBuffer KS0108_fork mbed-rtos mbed CAN Addresses
Fork of REVO_Updated_Steering by
Steering.cpp@35:b42afc973902, 2014-11-22 (annotated)
- Committer:
- palimar
- Date:
- Sat Nov 22 22:24:53 2014 +0000
- Revision:
- 35:b42afc973902
- Parent:
- 32:535acb159709
- Child:
- 36:8544a8900884
Added linked list, also graphics updates only when variables change.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kiran_mbed | 0:313541d8f8be | 1 | #include "Steering.h" |
kiran_mbed | 0:313541d8f8be | 2 | |
palimar | 23:6681a38918c6 | 3 | bool NOT_biSWBL_HELD; |
tmccurbin | 32:535acb159709 | 4 | bool NOT_biSWTR_HELD; |
kiran_mbed | 0:313541d8f8be | 5 | |
tmccurbin | 32:535acb159709 | 6 | void update_display(void const *args){ |
palimar | 35:b42afc973902 | 7 | |
tmccurbin | 32:535acb159709 | 8 | while(true){ |
tmccurbin | 32:535acb159709 | 9 | |
palimar | 35:b42afc973902 | 10 | if(screen_flags[curr_screen] || should_redraw){ |
palimar | 35:b42afc973902 | 11 | screen_flags[curr_screen] = 0; |
palimar | 35:b42afc973902 | 12 | should_redraw = false; |
tmccurbin | 32:535acb159709 | 13 | switch(curr_screen){ |
palimar | 26:e0256fd314eb | 14 | |
palimar | 35:b42afc973902 | 15 | case HOME_SCREEN:{ |
palimar | 35:b42afc973902 | 16 | |
tmccurbin | 32:535acb159709 | 17 | display.ClearScreen(); |
tmccurbin | 32:535acb159709 | 18 | display.SelectFont(Arial10,BLACK,ReadData); |
tmccurbin | 32:535acb159709 | 19 | display.GotoXY(37,0); |
palimar | 35:b42afc973902 | 20 | display.PrintString(" Home Screen"); |
palimar | 35:b42afc973902 | 21 | |
palimar | 35:b42afc973902 | 22 | display.GotoXY(22,30); |
palimar | 35:b42afc973902 | 23 | if(vars_list->get_value(PCM_STATE_ID) == 0){ |
palimar | 35:b42afc973902 | 24 | display.PutString(17, 30, "Drive Status: OFF"); |
palimar | 35:b42afc973902 | 25 | } |
palimar | 35:b42afc973902 | 26 | else{ |
palimar | 35:b42afc973902 | 27 | display.PrintString("Drive Status: ON"); |
palimar | 35:b42afc973902 | 28 | } |
tmccurbin | 32:535acb159709 | 29 | break; |
palimar | 35:b42afc973902 | 30 | } |
palimar | 26:e0256fd314eb | 31 | |
tmccurbin | 32:535acb159709 | 32 | case BATTERY_SCREEN: |
tmccurbin | 32:535acb159709 | 33 | |
tmccurbin | 32:535acb159709 | 34 | display.ClearScreen(); |
tmccurbin | 32:535acb159709 | 35 | display.SelectFont(Arial10,BLACK,ReadData); |
tmccurbin | 32:535acb159709 | 36 | display.GotoXY(33,0); |
palimar | 35:b42afc973902 | 37 | display.PrintString(" Battery Screen"); |
tmccurbin | 32:535acb159709 | 38 | break; |
palimar | 26:e0256fd314eb | 39 | |
tmccurbin | 32:535acb159709 | 40 | default: |
tmccurbin | 32:535acb159709 | 41 | break; |
palimar | 26:e0256fd314eb | 42 | |
palimar | 35:b42afc973902 | 43 | } |
tmccurbin | 32:535acb159709 | 44 | } |
tmccurbin | 32:535acb159709 | 45 | wait(2); |
tmccurbin | 32:535acb159709 | 46 | } |
tmccurbin | 32:535acb159709 | 47 | } |
tmccurbin | 32:535acb159709 | 48 | |
tmccurbin | 32:535acb159709 | 49 | void toggle_screen(){ |
palimar | 35:b42afc973902 | 50 | should_redraw = true; |
palimar | 35:b42afc973902 | 51 | curr_screen = (curr_screen+1) % NUM_SCREEN; |
palimar | 26:e0256fd314eb | 52 | } |
palimar | 26:e0256fd314eb | 53 | |
palimar | 23:6681a38918c6 | 54 | void request_status_change(){ |
palimar | 23:6681a38918c6 | 55 | |
palimar | 23:6681a38918c6 | 56 | char drive_status_request; |
palimar | 23:6681a38918c6 | 57 | ds_mutex.lock(); |
palimar | 35:b42afc973902 | 58 | drive_status_request = !(vars_list->get_value(PCM_STATE_ID)); |
palimar | 35:b42afc973902 | 59 | |
palimar | 23:6681a38918c6 | 60 | ds_mutex.unlock(); |
palimar | 23:6681a38918c6 | 61 | char * status_string; |
kiran_mbed | 0:313541d8f8be | 62 | |
palimar | 13:1f05dcd9ae0e | 63 | if(drive_status_request){ |
palimar | 13:1f05dcd9ae0e | 64 | status_string = "ON"; |
palimar | 13:1f05dcd9ae0e | 65 | } |
palimar | 13:1f05dcd9ae0e | 66 | else{ |
palimar | 13:1f05dcd9ae0e | 67 | status_string = "OFF"; |
palimar | 13:1f05dcd9ae0e | 68 | } |
palimar | 23:6681a38918c6 | 69 | |
palimar | 23:6681a38918c6 | 70 | CANMessage Txmsg_drive_status_request(0x501,&drive_status_request,1); |
palimar | 13:1f05dcd9ae0e | 71 | for(int i = 0; i < 10; i++){ |
palimar | 16:d2953809fb31 | 72 | CAN_Steering_Buffer.txWrite(Txmsg_drive_status_request); |
palimar | 13:1f05dcd9ae0e | 73 | } |
palimar | 6:99e754ac888f | 74 | |
palimar | 21:56890c41ecf1 | 75 | printf("%s\n\r", status_string); |
kiran_mbed | 0:313541d8f8be | 76 | return; |
kiran_mbed | 0:313541d8f8be | 77 | } |
kiran_mbed | 0:313541d8f8be | 78 | |
palimar | 9:e946cafa3cae | 79 | void reset() |
kiran_mbed | 0:313541d8f8be | 80 | { |
palimar | 23:6681a38918c6 | 81 | reset_body = 1; |
palimar | 23:6681a38918c6 | 82 | CANMessage Txmsg_reset(0x502,&reset_body,1); |
palimar | 23:6681a38918c6 | 83 | for(int i = 0; i < 10; i++){ |
palimar | 23:6681a38918c6 | 84 | CAN_Steering_Buffer.txWriteDirect(Txmsg_reset); |
tmccurbin | 22:ab33195882e3 | 85 | } |
palimar | 23:6681a38918c6 | 86 | NVIC_SystemReset(); |
kiran_mbed | 0:313541d8f8be | 87 | display.ClearScreen(); |
kiran_mbed | 0:313541d8f8be | 88 | display.SelectFont(Arial12,BLACK,ReadData); |
kiran_mbed | 0:313541d8f8be | 89 | display.GotoXY(16,16); |
kiran_mbed | 0:313541d8f8be | 90 | printf("Reset Initiated\n\r"); |
kiran_mbed | 0:313541d8f8be | 91 | |
kiran_mbed | 0:313541d8f8be | 92 | return; |
kiran_mbed | 0:313541d8f8be | 93 | } |
kiran_mbed | 0:313541d8f8be | 94 | |
kiran_mbed | 0:313541d8f8be | 95 | void Init() |
kiran_mbed | 0:313541d8f8be | 96 | { |
palimar | 35:b42afc973902 | 97 | should_redraw = true; |
palimar | 23:6681a38918c6 | 98 | pc.baud(921600); |
palimar | 26:e0256fd314eb | 99 | curr_screen = HOME_SCREEN; |
palimar | 9:e946cafa3cae | 100 | drive_status = 0; |
palimar | 13:1f05dcd9ae0e | 101 | drive_status_request = 1; |
palimar | 9:e946cafa3cae | 102 | reset_body = 0; |
kiran_mbed | 0:313541d8f8be | 103 | ledstream.write(0); |
palimar | 23:6681a38918c6 | 104 | NOT_biSWBL_HELD = true; |
tmccurbin | 32:535acb159709 | 105 | NOT_biSWTR_HELD = true; |
palimar | 35:b42afc973902 | 106 | |
palimar | 35:b42afc973902 | 107 | vars_list = new variables(); |
palimar | 35:b42afc973902 | 108 | |
palimar | 35:b42afc973902 | 109 | vars_list->add(PCM_STATE_ID, HOME_SCREEN); |
palimar | 35:b42afc973902 | 110 | vars_list->add(BATTERY_VOLTAGE_ID, BATTERY_SCREEN); |
palimar | 35:b42afc973902 | 111 | vars_list->add(BATTERY_POWER_ID, BATTERY_SCREEN); |
palimar | 35:b42afc973902 | 112 | vars_list->add(BATTERY_CURRENT_ID, BATTERY_SCREEN); |
palimar | 35:b42afc973902 | 113 | |
kiran_mbed | 0:313541d8f8be | 114 | } |
kiran_mbed | 0:313541d8f8be | 115 | |
palimar | 23:6681a38918c6 | 116 | void read_messages(void const *args) { |
palimar | 24:ba3428215006 | 117 | |
palimar | 21:56890c41ecf1 | 118 | while (true) { |
palimar | 35:b42afc973902 | 119 | |
palimar | 21:56890c41ecf1 | 120 | CANMessage Rxmsg; |
palimar | 35:b42afc973902 | 121 | if(CAN_Steering_Buffer.rxRead(Rxmsg)){ |
palimar | 35:b42afc973902 | 122 | id_node = vars_list->get_node(Rxmsg.id); |
palimar | 35:b42afc973902 | 123 | if(id_node != NULL){ |
palimar | 35:b42afc973902 | 124 | if(id_node->value != Rxmsg.data[0]){ |
palimar | 35:b42afc973902 | 125 | screen_flags[id_node->screen] = 1; |
palimar | 21:56890c41ecf1 | 126 | } |
palimar | 35:b42afc973902 | 127 | id_node->value = Rxmsg.data[0]; |
palimar | 23:6681a38918c6 | 128 | } |
palimar | 21:56890c41ecf1 | 129 | } |
palimar | 21:56890c41ecf1 | 130 | } |
palimar | 35:b42afc973902 | 131 | } |
palimar | 21:56890c41ecf1 | 132 | |
palimar | 23:6681a38918c6 | 133 | int main(){ |
palimar | 23:6681a38918c6 | 134 | // Initialize, set all variables. |
kiran_mbed | 0:313541d8f8be | 135 | Init(); |
kiran_mbed | 0:313541d8f8be | 136 | wait(0.1); |
palimar | 4:a4fb060e7840 | 137 | |
palimar | 4:a4fb060e7840 | 138 | //Init Display |
kiran_mbed | 0:313541d8f8be | 139 | display.GotoXY(10,16); |
kiran_mbed | 0:313541d8f8be | 140 | display.SelectFont(Arial_14,BLACK,ReadData); |
kiran_mbed | 0:313541d8f8be | 141 | display.PrintString("Penn Electric Racing"); |
palimar | 23:6681a38918c6 | 142 | CAN_Steering_Buffer.mode(NoAck); |
palimar | 6:99e754ac888f | 143 | |
tmccurbin | 32:535acb159709 | 144 | wait(2); |
palimar | 26:e0256fd314eb | 145 | |
palimar | 23:6681a38918c6 | 146 | //New thread to read messages. |
tmccurbin | 32:535acb159709 | 147 | Thread update_thread(read_messages); |
palimar | 26:e0256fd314eb | 148 | |
palimar | 26:e0256fd314eb | 149 | // display the screen. |
tmccurbin | 32:535acb159709 | 150 | Thread display_thread(update_display); |
palimar | 26:e0256fd314eb | 151 | |
palimar | 4:a4fb060e7840 | 152 | |
palimar | 4:a4fb060e7840 | 153 | // Start to read buttons on main thread |
kiran_mbed | 0:313541d8f8be | 154 | while(1) |
kiran_mbed | 0:313541d8f8be | 155 | { |
palimar | 23:6681a38918c6 | 156 | if(biSWBL.read() && NOT_biSWBL_HELD){ |
palimar | 9:e946cafa3cae | 157 | request_status_change(); |
palimar | 23:6681a38918c6 | 158 | NOT_biSWBL_HELD = false; |
kiran_mbed | 0:313541d8f8be | 159 | } |
palimar | 23:6681a38918c6 | 160 | |
palimar | 23:6681a38918c6 | 161 | else if(!biSWBL.read()){ |
palimar | 23:6681a38918c6 | 162 | NOT_biSWBL_HELD = true; |
kiran_mbed | 0:313541d8f8be | 163 | } |
palimar | 23:6681a38918c6 | 164 | |
palimar | 23:6681a38918c6 | 165 | else{ |
palimar | 23:6681a38918c6 | 166 | // ignore BiSWBL.read() |
palimar | 23:6681a38918c6 | 167 | } |
palimar | 23:6681a38918c6 | 168 | |
tmccurbin | 32:535acb159709 | 169 | if(biSWTR.read() && NOT_biSWTR_HELD){ |
tmccurbin | 32:535acb159709 | 170 | toggle_screen(); |
tmccurbin | 32:535acb159709 | 171 | NOT_biSWTR_HELD = false; |
tmccurbin | 32:535acb159709 | 172 | } |
tmccurbin | 32:535acb159709 | 173 | |
tmccurbin | 32:535acb159709 | 174 | else if(!biSWTR.read()){ |
tmccurbin | 32:535acb159709 | 175 | NOT_biSWTR_HELD = true; |
tmccurbin | 32:535acb159709 | 176 | } |
tmccurbin | 32:535acb159709 | 177 | |
tmccurbin | 32:535acb159709 | 178 | else{ |
tmccurbin | 32:535acb159709 | 179 | // ignore BiSWTR.read() |
tmccurbin | 32:535acb159709 | 180 | } |
tmccurbin | 32:535acb159709 | 181 | |
palimar | 23:6681a38918c6 | 182 | if(biSWBR.read()){ |
palimar | 9:e946cafa3cae | 183 | reset(); |
kiran_mbed | 0:313541d8f8be | 184 | } |
kiran_mbed | 0:313541d8f8be | 185 | } |
kiran_mbed | 0:313541d8f8be | 186 | } |
palimar | 23:6681a38918c6 | 187 |