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.
Fork of OBD21121 by
main.cpp@2:d58ef72b4ab0, 2017-02-25 (annotated)
- Committer:
- takeuz
- Date:
- Sat Feb 25 07:01:14 2017 +0000
- Revision:
- 2:d58ef72b4ab0
- Parent:
- 1:1d9a04a21027
- Child:
- 4:3e1e2b161403
first Commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
takeuz | 0:ccd2474129f2 | 1 | #include "mbed.h" |
takeuz | 2:d58ef72b4ab0 | 2 | #include "functions.h" |
takeuz | 2:d58ef72b4ab0 | 3 | |
takeuz | 2:d58ef72b4ab0 | 4 | #define ENGINE_RPM 0x0C |
takeuz | 2:d58ef72b4ab0 | 5 | #define VEHICLE_SPEED 0x0D |
takeuz | 2:d58ef72b4ab0 | 6 | #define PID_REQUEST 0x7DF |
takeuz | 2:d58ef72b4ab0 | 7 | #define PID_REPLY 0x7E8 |
takeuz | 2:d58ef72b4ab0 | 8 | |
takeuz | 2:d58ef72b4ab0 | 9 | //??USE ARRAYLIST Of Int |
takeuz | 2:d58ef72b4ab0 | 10 | #define CANSPEED_500 500000 // CAN speed at 500 kbps |
takeuz | 2:d58ef72b4ab0 | 11 | #define CANSPEED_250 250000 // CAN speed at 250 kbps |
takeuz | 2:d58ef72b4ab0 | 12 | #define CANSPEED_125 125000 // CAN speed at 125 kbps |
takeuz | 2:d58ef72b4ab0 | 13 | #define sendDebugMessage 1 |
takeuz | 2:d58ef72b4ab0 | 14 | #define pushDebugData 1 |
takeuz | 2:d58ef72b4ab0 | 15 | |
takeuz | 2:d58ef72b4ab0 | 16 | const int canSpeeds[3]={CANSPEED_500,CANSPEED_250,CANSPEED_125}; |
takeuz | 2:d58ef72b4ab0 | 17 | |
takeuz | 2:d58ef72b4ab0 | 18 | |
takeuz | 2:d58ef72b4ab0 | 19 | DigitalOut myled(LED2); |
takeuz | 2:d58ef72b4ab0 | 20 | Serial serialPorts[3] = {Serial(P0_26,P0_6),Serial(P0_22,P0_23),Serial(USBTX,USBRX)}; |
takeuz | 2:d58ef72b4ab0 | 21 | //Serial pc(P0_9,P0_29); |
takeuz | 2:d58ef72b4ab0 | 22 | |
takeuz | 2:d58ef72b4ab0 | 23 | CAN canBus(P0_24, P0_25); |
takeuz | 2:d58ef72b4ab0 | 24 | //AnalogIn batteryVoltage(P0_20); |
takeuz | 2:d58ef72b4ab0 | 25 | //DigitalOut indicatorLEDs[3] = {P0_22,P0_23,P0_6}; |
takeuz | 2:d58ef72b4ab0 | 26 | |
takeuz | 2:d58ef72b4ab0 | 27 | |
takeuz | 2:d58ef72b4ab0 | 28 | //UART Counters |
takeuz | 2:d58ef72b4ab0 | 29 | int kLine=0; |
takeuz | 2:d58ef72b4ab0 | 30 | int interFace=1; |
takeuz | 2:d58ef72b4ab0 | 31 | int debug=2; |
takeuz | 2:d58ef72b4ab0 | 32 | |
takeuz | 2:d58ef72b4ab0 | 33 | |
takeuz | 2:d58ef72b4ab0 | 34 | int currentOBDProtocol=0; |
takeuz | 2:d58ef72b4ab0 | 35 | const int protkLine=2; |
takeuz | 2:d58ef72b4ab0 | 36 | const int protCan=1; |
takeuz | 2:d58ef72b4ab0 | 37 | int canFrequency; |
takeuz | 2:d58ef72b4ab0 | 38 | //LED Counters |
takeuz | 2:d58ef72b4ab0 | 39 | |
takeuz | 2:d58ef72b4ab0 | 40 | Ticker timeCounter; |
takeuz | 2:d58ef72b4ab0 | 41 | char can_msg[8]; |
takeuz | 2:d58ef72b4ab0 | 42 | Timer OBDTimer; |
takeuz | 2:d58ef72b4ab0 | 43 | |
takeuz | 2:d58ef72b4ab0 | 44 | //Data Storage for incoming data on Interface |
takeuz | 2:d58ef72b4ab0 | 45 | char *incomingDataInInterFace; |
takeuz | 2:d58ef72b4ab0 | 46 | const int incomingDataInInterFaceSize=256; |
takeuz | 2:d58ef72b4ab0 | 47 | int incomingDataInInterFaceCounter=0; |
takeuz | 2:d58ef72b4ab0 | 48 | |
takeuz | 2:d58ef72b4ab0 | 49 | //Data Storage for incoming data on OBD |
takeuz | 2:d58ef72b4ab0 | 50 | char *incomingDataInOBD; |
takeuz | 2:d58ef72b4ab0 | 51 | const int incomingDataInOBDSize=256; |
takeuz | 2:d58ef72b4ab0 | 52 | CANMessage can_MsgRx; |
takeuz | 2:d58ef72b4ab0 | 53 | int incomingDataInOBDCounter=0; |
takeuz | 2:d58ef72b4ab0 | 54 | |
takeuz | 2:d58ef72b4ab0 | 55 | |
takeuz | 2:d58ef72b4ab0 | 56 | //OBD Command data |
takeuz | 2:d58ef72b4ab0 | 57 | char* charPid; |
takeuz | 2:d58ef72b4ab0 | 58 | char* charID; |
takeuz | 2:d58ef72b4ab0 | 59 | int pid; |
takeuz | 2:d58ef72b4ab0 | 60 | int ID; |
takeuz | 2:d58ef72b4ab0 | 61 | |
takeuz | 2:d58ef72b4ab0 | 62 | |
takeuz | 0:ccd2474129f2 | 63 | |
takeuz | 2:d58ef72b4ab0 | 64 | //Wait Duration for OBD comm |
takeuz | 2:d58ef72b4ab0 | 65 | const int waitTimeForOBDComm=2000; |
takeuz | 2:d58ef72b4ab0 | 66 | //Sleep time between commands |
takeuz | 2:d58ef72b4ab0 | 67 | const int sleepTime=25000; |
takeuz | 2:d58ef72b4ab0 | 68 | //Time to send chip to power save mode |
takeuz | 2:d58ef72b4ab0 | 69 | const int powerSaveTime=120; |
takeuz | 2:d58ef72b4ab0 | 70 | |
takeuz | 2:d58ef72b4ab0 | 71 | //Global Variables |
takeuz | 2:d58ef72b4ab0 | 72 | //New Data receivd on OBD or Interface |
takeuz | 2:d58ef72b4ab0 | 73 | int OBDCmdReceived=0; |
takeuz | 2:d58ef72b4ab0 | 74 | int interfaceCmdReceived=0; |
takeuz | 2:d58ef72b4ab0 | 75 | |
takeuz | 2:d58ef72b4ab0 | 76 | //Last activity time on OBD Interface |
takeuz | 2:d58ef72b4ab0 | 77 | int lastOBDCmdRcvdTime=0; |
takeuz | 2:d58ef72b4ab0 | 78 | int lastInterfaceCmdRcvdTime=0; |
takeuz | 2:d58ef72b4ab0 | 79 | |
takeuz | 2:d58ef72b4ab0 | 80 | void readInterface() |
takeuz | 2:d58ef72b4ab0 | 81 | { |
takeuz | 2:d58ef72b4ab0 | 82 | char c; |
takeuz | 2:d58ef72b4ab0 | 83 | while(serialPorts[interFace].readable()) { |
takeuz | 2:d58ef72b4ab0 | 84 | lastInterfaceCmdRcvdTime=0; |
takeuz | 2:d58ef72b4ab0 | 85 | c = serialPorts[interFace].getc(); |
takeuz | 2:d58ef72b4ab0 | 86 | if(incomingDataInInterFaceCounter<incomingDataInInterFaceSize){ |
takeuz | 2:d58ef72b4ab0 | 87 | incomingDataInInterFace[incomingDataInInterFaceCounter] = c; |
takeuz | 2:d58ef72b4ab0 | 88 | } |
takeuz | 2:d58ef72b4ab0 | 89 | incomingDataInInterFaceCounter++; |
takeuz | 2:d58ef72b4ab0 | 90 | |
takeuz | 2:d58ef72b4ab0 | 91 | if(c=='\n') { |
takeuz | 2:d58ef72b4ab0 | 92 | interfaceCmdReceived=1; |
takeuz | 2:d58ef72b4ab0 | 93 | } |
takeuz | 0:ccd2474129f2 | 94 | |
takeuz | 2:d58ef72b4ab0 | 95 | } |
takeuz | 2:d58ef72b4ab0 | 96 | } |
takeuz | 2:d58ef72b4ab0 | 97 | void readKLine() |
takeuz | 2:d58ef72b4ab0 | 98 | { |
takeuz | 2:d58ef72b4ab0 | 99 | char c; |
takeuz | 2:d58ef72b4ab0 | 100 | while(serialPorts[kLine].readable()) { |
takeuz | 2:d58ef72b4ab0 | 101 | lastOBDCmdRcvdTime=0; |
takeuz | 2:d58ef72b4ab0 | 102 | c = serialPorts[kLine].getc(); |
takeuz | 2:d58ef72b4ab0 | 103 | if(incomingDataInOBDCounter<incomingDataInOBDSize){ |
takeuz | 2:d58ef72b4ab0 | 104 | incomingDataInOBD[incomingDataInOBDCounter]=c; |
takeuz | 2:d58ef72b4ab0 | 105 | } |
takeuz | 2:d58ef72b4ab0 | 106 | incomingDataInOBDCounter++; |
takeuz | 2:d58ef72b4ab0 | 107 | if(c=='\n') { |
takeuz | 2:d58ef72b4ab0 | 108 | OBDCmdReceived=1; |
takeuz | 2:d58ef72b4ab0 | 109 | } |
takeuz | 2:d58ef72b4ab0 | 110 | |
takeuz | 2:d58ef72b4ab0 | 111 | } |
takeuz | 2:d58ef72b4ab0 | 112 | } |
takeuz | 2:d58ef72b4ab0 | 113 | |
takeuz | 2:d58ef72b4ab0 | 114 | |
takeuz | 2:d58ef72b4ab0 | 115 | |
takeuz | 2:d58ef72b4ab0 | 116 | void canReader(){ |
takeuz | 2:d58ef72b4ab0 | 117 | if (canBus.read(can_MsgRx)) { |
takeuz | 2:d58ef72b4ab0 | 118 | //Setting |
takeuz | 2:d58ef72b4ab0 | 119 | lastOBDCmdRcvdTime=0; |
takeuz | 2:d58ef72b4ab0 | 120 | |
takeuz | 2:d58ef72b4ab0 | 121 | if ((can_MsgRx.id == PID_REPLY) && (can_MsgRx.data[2] == ENGINE_RPM)) { |
takeuz | 2:d58ef72b4ab0 | 122 | serialPorts[debug].puts("HRHK"); |
takeuz | 2:d58ef72b4ab0 | 123 | for (int i = 0; i < (int)can_MsgRx.len; i++) { |
takeuz | 2:d58ef72b4ab0 | 124 | serialPorts[debug].printf("%d:%x , ", i, can_MsgRx.data[i]); |
takeuz | 2:d58ef72b4ab0 | 125 | } |
takeuz | 2:d58ef72b4ab0 | 126 | } |
takeuz | 2:d58ef72b4ab0 | 127 | } |
takeuz | 2:d58ef72b4ab0 | 128 | } |
takeuz | 2:d58ef72b4ab0 | 129 | |
takeuz | 2:d58ef72b4ab0 | 130 | void timeCounterFunction(){ |
takeuz | 2:d58ef72b4ab0 | 131 | lastInterfaceCmdRcvdTime++; |
takeuz | 2:d58ef72b4ab0 | 132 | lastOBDCmdRcvdTime++; |
takeuz | 2:d58ef72b4ab0 | 133 | //IF timout is over certain amount just shutdown |
takeuz | 2:d58ef72b4ab0 | 134 | |
takeuz | 2:d58ef72b4ab0 | 135 | |
takeuz | 2:d58ef72b4ab0 | 136 | } |
takeuz | 0:ccd2474129f2 | 137 | |
takeuz | 0:ccd2474129f2 | 138 | int main() { |
takeuz | 2:d58ef72b4ab0 | 139 | |
takeuz | 2:d58ef72b4ab0 | 140 | serialPorts[debug].puts("f2114\r\n"); |
takeuz | 2:d58ef72b4ab0 | 141 | |
takeuz | 2:d58ef72b4ab0 | 142 | //Initialization |
takeuz | 2:d58ef72b4ab0 | 143 | incomingDataInInterFace=(char *)malloc(incomingDataInInterFaceSize*sizeof(char)); |
takeuz | 2:d58ef72b4ab0 | 144 | incomingDataInOBD=(char *)malloc(incomingDataInOBDSize*sizeof(char)); |
takeuz | 2:d58ef72b4ab0 | 145 | |
takeuz | 2:d58ef72b4ab0 | 146 | clearMemoryLocation(incomingDataInInterFace,incomingDataInInterFaceSize); |
takeuz | 2:d58ef72b4ab0 | 147 | clearMemoryLocation(incomingDataInOBD,incomingDataInOBDSize); |
takeuz | 2:d58ef72b4ab0 | 148 | |
takeuz | 2:d58ef72b4ab0 | 149 | timeCounter.attach(&timeCounterFunction, 1.0); |
takeuz | 2:d58ef72b4ab0 | 150 | serialPorts[interFace].attach(&readInterface); |
takeuz | 2:d58ef72b4ab0 | 151 | |
takeuz | 2:d58ef72b4ab0 | 152 | serialPorts[interFace].puts("ELM327 v1.0\r\n"); |
takeuz | 2:d58ef72b4ab0 | 153 | |
takeuz | 0:ccd2474129f2 | 154 | while(1) { |
takeuz | 2:d58ef72b4ab0 | 155 | if(!currentOBDProtocol){ |
takeuz | 2:d58ef72b4ab0 | 156 | #if sendDebugMessage |
takeuz | 2:d58ef72b4ab0 | 157 | serialPorts[debug].puts("Going to figure protocol\r\n"); |
takeuz | 2:d58ef72b4ab0 | 158 | #endif |
takeuz | 2:d58ef72b4ab0 | 159 | serialPorts[kLine].attach(NULL); |
takeuz | 2:d58ef72b4ab0 | 160 | canBus.attach(NULL); |
takeuz | 2:d58ef72b4ab0 | 161 | serialPorts[interFace].puts("Searching...\r\n"); |
takeuz | 2:d58ef72b4ab0 | 162 | serialPorts[debug].puts("Searching...\r\n"); |
takeuz | 2:d58ef72b4ab0 | 163 | checkCommunicationProtocol(); |
takeuz | 2:d58ef72b4ab0 | 164 | if(currentOBDProtocol==protkLine){ |
takeuz | 2:d58ef72b4ab0 | 165 | serialPorts[kLine].attach(&readKLine); |
takeuz | 2:d58ef72b4ab0 | 166 | } |
takeuz | 2:d58ef72b4ab0 | 167 | if(currentOBDProtocol==protCan){ |
takeuz | 2:d58ef72b4ab0 | 168 | canBus.attach(&canReader); |
takeuz | 2:d58ef72b4ab0 | 169 | } |
takeuz | 2:d58ef72b4ab0 | 170 | //??Shall we do somehitng when receive a specific commnad |
takeuz | 2:d58ef72b4ab0 | 171 | |
takeuz | 2:d58ef72b4ab0 | 172 | //?? Do this !Unknown only once. |
takeuz | 2:d58ef72b4ab0 | 173 | if(!currentOBDProtocol){ |
takeuz | 2:d58ef72b4ab0 | 174 | serialPorts[interFace].puts("!UnknownProtocol\r\n"); |
takeuz | 2:d58ef72b4ab0 | 175 | #if sendDebugMessage |
takeuz | 2:d58ef72b4ab0 | 176 | serialPorts[debug].puts("!Unknownprotocol\r\n"); |
takeuz | 2:d58ef72b4ab0 | 177 | #endif |
takeuz | 2:d58ef72b4ab0 | 178 | //Goto Sleep? |
takeuz | 2:d58ef72b4ab0 | 179 | } |
takeuz | 2:d58ef72b4ab0 | 180 | } |
takeuz | 2:d58ef72b4ab0 | 181 | |
takeuz | 2:d58ef72b4ab0 | 182 | |
takeuz | 2:d58ef72b4ab0 | 183 | if(interfaceCmdReceived){ |
takeuz | 2:d58ef72b4ab0 | 184 | checkForLocalInterfaceCommand(); |
takeuz | 2:d58ef72b4ab0 | 185 | } |
takeuz | 2:d58ef72b4ab0 | 186 | if(interfaceCmdReceived){ |
takeuz | 2:d58ef72b4ab0 | 187 | OBDTimer.reset(); |
takeuz | 2:d58ef72b4ab0 | 188 | OBDCmdReceived=0; |
takeuz | 2:d58ef72b4ab0 | 189 | sendCommand(); |
takeuz | 2:d58ef72b4ab0 | 190 | OBDTimer.start(); |
takeuz | 2:d58ef72b4ab0 | 191 | while(OBDTimer.read_ms() < waitTimeForOBDComm){ |
takeuz | 2:d58ef72b4ab0 | 192 | if(OBDCmdReceived){ |
takeuz | 2:d58ef72b4ab0 | 193 | sendResponseToInterface(); |
takeuz | 2:d58ef72b4ab0 | 194 | break; |
takeuz | 2:d58ef72b4ab0 | 195 | } |
takeuz | 2:d58ef72b4ab0 | 196 | } |
takeuz | 2:d58ef72b4ab0 | 197 | OBDTimer.stop(); |
takeuz | 2:d58ef72b4ab0 | 198 | if(!OBDCmdReceived){ |
takeuz | 2:d58ef72b4ab0 | 199 | serialPorts[interFace].puts("?\r\n"); |
takeuz | 2:d58ef72b4ab0 | 200 | } |
takeuz | 2:d58ef72b4ab0 | 201 | OBDCmdReceived=0; |
takeuz | 2:d58ef72b4ab0 | 202 | interfaceCmdReceived=0; |
takeuz | 2:d58ef72b4ab0 | 203 | |
takeuz | 2:d58ef72b4ab0 | 204 | //issueRegularCommands(); |
takeuz | 2:d58ef72b4ab0 | 205 | } |
takeuz | 2:d58ef72b4ab0 | 206 | myled=0; |
takeuz | 2:d58ef72b4ab0 | 207 | sleepProcess(); |
takeuz | 2:d58ef72b4ab0 | 208 | if(lastOBDCmdRcvdTime> powerSaveTime && lastInterfaceCmdRcvdTime> powerSaveTime ){ |
takeuz | 2:d58ef72b4ab0 | 209 | powerSaveMode(); |
takeuz | 2:d58ef72b4ab0 | 210 | } |
takeuz | 2:d58ef72b4ab0 | 211 | myled=1; |
takeuz | 2:d58ef72b4ab0 | 212 | |
takeuz | 2:d58ef72b4ab0 | 213 | |
takeuz | 0:ccd2474129f2 | 214 | } |
takeuz | 0:ccd2474129f2 | 215 | } |
takeuz | 2:d58ef72b4ab0 | 216 | |
takeuz | 2:d58ef72b4ab0 | 217 | void checkCommunicationProtocol(){ |
takeuz | 2:d58ef72b4ab0 | 218 | #if sendDebugMessage |
takeuz | 2:d58ef72b4ab0 | 219 | serialPorts[debug].puts("check Communication protocal ENT\r\n"); |
takeuz | 2:d58ef72b4ab0 | 220 | #endif |
takeuz | 2:d58ef72b4ab0 | 221 | int i=0; |
takeuz | 2:d58ef72b4ab0 | 222 | do{ |
takeuz | 2:d58ef72b4ab0 | 223 | #if sendDebugMessage |
takeuz | 2:d58ef72b4ab0 | 224 | serialPorts[debug].puts("CanBusCheck\r\n"); |
takeuz | 2:d58ef72b4ab0 | 225 | #endif |
takeuz | 2:d58ef72b4ab0 | 226 | canBus.reset(); |
takeuz | 2:d58ef72b4ab0 | 227 | clearCanMessage(); |
takeuz | 2:d58ef72b4ab0 | 228 | canBus.frequency(canSpeeds[i]); |
takeuz | 2:d58ef72b4ab0 | 229 | OBDTimer.reset(); |
takeuz | 2:d58ef72b4ab0 | 230 | OBDTimer.start(); |
takeuz | 2:d58ef72b4ab0 | 231 | OBDCmdReceived=0; |
takeuz | 2:d58ef72b4ab0 | 232 | if (canBus.write(CANMessage(PID_REQUEST, can_msg, 8))) { |
takeuz | 2:d58ef72b4ab0 | 233 | #if sendDebugMessage |
takeuz | 2:d58ef72b4ab0 | 234 | serialPorts[debug].printf("OBDTIMER:%d\r\n",OBDTimer.read_ms()); |
takeuz | 2:d58ef72b4ab0 | 235 | #endif |
takeuz | 2:d58ef72b4ab0 | 236 | while(OBDTimer.read_ms() < waitTimeForOBDComm){ |
takeuz | 2:d58ef72b4ab0 | 237 | if(OBDCmdReceived){ |
takeuz | 2:d58ef72b4ab0 | 238 | currentOBDProtocol=protCan; |
takeuz | 2:d58ef72b4ab0 | 239 | canFrequency=i; |
takeuz | 2:d58ef72b4ab0 | 240 | #if sendDebugMessage |
takeuz | 2:d58ef72b4ab0 | 241 | serialPorts[debug].puts("check Communication protocal SET\r\n"); |
takeuz | 2:d58ef72b4ab0 | 242 | #endif |
takeuz | 2:d58ef72b4ab0 | 243 | return; |
takeuz | 2:d58ef72b4ab0 | 244 | } |
takeuz | 2:d58ef72b4ab0 | 245 | } |
takeuz | 2:d58ef72b4ab0 | 246 | } |
takeuz | 2:d58ef72b4ab0 | 247 | wait_ms(200); |
takeuz | 2:d58ef72b4ab0 | 248 | i++; |
takeuz | 2:d58ef72b4ab0 | 249 | }while(i<3); |
takeuz | 2:d58ef72b4ab0 | 250 | OBDTimer.stop(); |
takeuz | 2:d58ef72b4ab0 | 251 | #if sendDebugMessage |
takeuz | 2:d58ef72b4ab0 | 252 | serialPorts[debug].puts("check Communication protocal EXT\r\n"); |
takeuz | 2:d58ef72b4ab0 | 253 | #endif |
takeuz | 2:d58ef72b4ab0 | 254 | } |
takeuz | 2:d58ef72b4ab0 | 255 | |
takeuz | 2:d58ef72b4ab0 | 256 | void clearMemoryLocation( char *base, int _size) |
takeuz | 2:d58ef72b4ab0 | 257 | { |
takeuz | 2:d58ef72b4ab0 | 258 | for(int forCount=0; forCount<_size; forCount++) { |
takeuz | 2:d58ef72b4ab0 | 259 | base[forCount]='\0'; |
takeuz | 2:d58ef72b4ab0 | 260 | } |
takeuz | 2:d58ef72b4ab0 | 261 | } |
takeuz | 2:d58ef72b4ab0 | 262 | void clearCanMessage(){ |
takeuz | 2:d58ef72b4ab0 | 263 | can_msg[0] = 0x02; |
takeuz | 2:d58ef72b4ab0 | 264 | can_msg[1] = 0x01; |
takeuz | 2:d58ef72b4ab0 | 265 | can_msg[2] = 0; |
takeuz | 2:d58ef72b4ab0 | 266 | can_msg[3] = 0; |
takeuz | 2:d58ef72b4ab0 | 267 | can_msg[4] = 0; |
takeuz | 2:d58ef72b4ab0 | 268 | can_msg[5] = 0; |
takeuz | 2:d58ef72b4ab0 | 269 | can_msg[6] = 0; |
takeuz | 2:d58ef72b4ab0 | 270 | can_msg[7] = 0; |
takeuz | 2:d58ef72b4ab0 | 271 | } |
takeuz | 2:d58ef72b4ab0 | 272 | |
takeuz | 2:d58ef72b4ab0 | 273 | void sendCommand(){ |
takeuz | 2:d58ef72b4ab0 | 274 | |
takeuz | 2:d58ef72b4ab0 | 275 | } |
takeuz | 2:d58ef72b4ab0 | 276 | |
takeuz | 2:d58ef72b4ab0 | 277 | |
takeuz | 2:d58ef72b4ab0 | 278 | void sleepProcess() { |
takeuz | 2:d58ef72b4ab0 | 279 | |
takeuz | 2:d58ef72b4ab0 | 280 | #if pushDebugData |
takeuz | 2:d58ef72b4ab0 | 281 | serialPorts[debug].puts("SPe\r\n"); |
takeuz | 2:d58ef72b4ab0 | 282 | #endif |
takeuz | 2:d58ef72b4ab0 | 283 | OBDTimer.reset(); |
takeuz | 2:d58ef72b4ab0 | 284 | OBDTimer.start(); |
takeuz | 2:d58ef72b4ab0 | 285 | while(OBDTimer.read_ms()<sleepTime && !interfaceCmdReceived){ |
takeuz | 2:d58ef72b4ab0 | 286 | wait_ms(50); |
takeuz | 2:d58ef72b4ab0 | 287 | } |
takeuz | 2:d58ef72b4ab0 | 288 | OBDTimer.stop(); |
takeuz | 2:d58ef72b4ab0 | 289 | |
takeuz | 2:d58ef72b4ab0 | 290 | } |
takeuz | 2:d58ef72b4ab0 | 291 | |
takeuz | 2:d58ef72b4ab0 | 292 | void powerSaveMode(){ |
takeuz | 2:d58ef72b4ab0 | 293 | #if pushDebugData |
takeuz | 2:d58ef72b4ab0 | 294 | serialPorts[debug].puts("PowerSaveOn\r\n"); |
takeuz | 2:d58ef72b4ab0 | 295 | #endif |
takeuz | 2:d58ef72b4ab0 | 296 | //Remove pin allocaitons |
takeuz | 2:d58ef72b4ab0 | 297 | |
takeuz | 2:d58ef72b4ab0 | 298 | wait(20); |
takeuz | 2:d58ef72b4ab0 | 299 | |
takeuz | 2:d58ef72b4ab0 | 300 | //Make pin allocaitons |
takeuz | 2:d58ef72b4ab0 | 301 | lastOBDCmdRcvdTime=0; |
takeuz | 2:d58ef72b4ab0 | 302 | lastInterfaceCmdRcvdTime=0; |
takeuz | 2:d58ef72b4ab0 | 303 | |
takeuz | 2:d58ef72b4ab0 | 304 | #if pushDebugData |
takeuz | 2:d58ef72b4ab0 | 305 | serialPorts[debug].puts("PowerSaveOff\r\n"); |
takeuz | 2:d58ef72b4ab0 | 306 | #endif |
takeuz | 2:d58ef72b4ab0 | 307 | |
takeuz | 2:d58ef72b4ab0 | 308 | } |
takeuz | 2:d58ef72b4ab0 | 309 | |
takeuz | 2:d58ef72b4ab0 | 310 | void tprintf( char* str, int num){ |
takeuz | 2:d58ef72b4ab0 | 311 | int i2aCounter=0; |
takeuz | 2:d58ef72b4ab0 | 312 | if (num == 0) |
takeuz | 2:d58ef72b4ab0 | 313 | { |
takeuz | 2:d58ef72b4ab0 | 314 | str[i2aCounter++] = '0'; |
takeuz | 2:d58ef72b4ab0 | 315 | str[i2aCounter] = '\0'; |
takeuz | 2:d58ef72b4ab0 | 316 | return; |
takeuz | 2:d58ef72b4ab0 | 317 | } |
takeuz | 2:d58ef72b4ab0 | 318 | int tempNum=num; |
takeuz | 2:d58ef72b4ab0 | 319 | while(tempNum!=0){ |
takeuz | 2:d58ef72b4ab0 | 320 | tempNum/=10; |
takeuz | 2:d58ef72b4ab0 | 321 | i2aCounter++; |
takeuz | 2:d58ef72b4ab0 | 322 | } |
takeuz | 2:d58ef72b4ab0 | 323 | str[i2aCounter]='\0'; |
takeuz | 2:d58ef72b4ab0 | 324 | i2aCounter--; |
takeuz | 2:d58ef72b4ab0 | 325 | while(i2aCounter>=0){ |
takeuz | 2:d58ef72b4ab0 | 326 | str[i2aCounter--] = (num % 10) + '0'; |
takeuz | 2:d58ef72b4ab0 | 327 | num = num/10; |
takeuz | 2:d58ef72b4ab0 | 328 | } |
takeuz | 2:d58ef72b4ab0 | 329 | return; |
takeuz | 2:d58ef72b4ab0 | 330 | } |
takeuz | 2:d58ef72b4ab0 | 331 | |
takeuz | 2:d58ef72b4ab0 | 332 | void checkForLocalInterfaceCommand(){ |
takeuz | 2:d58ef72b4ab0 | 333 | if(strstr(incomingDataInInterFace,"AT")==0){ |
takeuz | 2:d58ef72b4ab0 | 334 | return; |
takeuz | 2:d58ef72b4ab0 | 335 | } |
takeuz | 2:d58ef72b4ab0 | 336 | if(strstr(incomingDataInInterFace,"ATZ")>0){ |
takeuz | 2:d58ef72b4ab0 | 337 | NVIC_SystemReset(); |
takeuz | 2:d58ef72b4ab0 | 338 | } |
takeuz | 2:d58ef72b4ab0 | 339 | if(strstr(incomingDataInInterFace,"ATRV")>0){ |
takeuz | 2:d58ef72b4ab0 | 340 | serialPorts[interFace].puts("Gointoreport bat vol\r\n"); |
takeuz | 2:d58ef72b4ab0 | 341 | interfaceCmdReceived=0; |
takeuz | 2:d58ef72b4ab0 | 342 | return; |
takeuz | 2:d58ef72b4ab0 | 343 | } |
takeuz | 2:d58ef72b4ab0 | 344 | serialPorts[interFace].puts("?\r\n"); |
takeuz | 2:d58ef72b4ab0 | 345 | |
takeuz | 2:d58ef72b4ab0 | 346 | } |
takeuz | 2:d58ef72b4ab0 | 347 | void sendResponseToInterface(){ |
takeuz | 2:d58ef72b4ab0 | 348 | |
takeuz | 2:d58ef72b4ab0 | 349 | } |
takeuz | 2:d58ef72b4ab0 | 350 |