SAE Teensy ECU
IIT SAE Microcontroller programming
Loading...
Searching...
No Matches
SerialVar.cpp
Go to the documentation of this file.
1
12// @cond
13
14#include "SerialVar.h"
15#include "ECUGlobalConfig.h"
16#include "PPHelp.h"
17
18namespace SerialVar {
19
20LOG_TAG LOG_ID = "SerialVar";
21
22// The largest ID number defined
23static const size_t MAX_ID = (
24#define X(type, ID) max(ID,
26#undef X
27 - 1
28#define X(type, ID) )
30#undef X
31);
32
33static uint8_t variables[MAX_ID + 1][8];
34
35uint8_t *getVariable(size_t ID) {
36 return variables[ID];
37}
38
39void updateVariable(size_t varID, const uint8_t *dataArr) {
40 switch (varID) {
41#define X(type, ID) \
42 case ID: \
43 memcpy(variables[ID], dataArr, 8); \
44 Log.p("serial_var_response", "Data received for varID:", ID); \
45 Log.i(LOG_ID, "Int value:", *(int *)(variables + ID)); \
46 Log.i(LOG_ID, "Approximate Float value:", (int)*(float *)(variables + ID)); \
47 break;
49#undef X
50 default:
51 Log.i(LOG_ID, "Invalid varID:", varID);
52 }
53}
54
55void receiveSerialVar() {
56 static elapsedMillis timeElapsed;
57 static uint8_t buffer[9];
58
59 timeElapsed = 0;
60 int c = -1;
61
62 Log.i(LOG_ID, "Waiting for data");
63
64 while (c < 8) {
65 if (Serial.available()) {
66 buffer[++c] = Serial.read();
67 } else if (timeElapsed > 1500) { // This process should be quick
68 Log.w(LOG_ID, "Timeout waiting for data");
69 return;
70 }
71 }
72
73 uint8_t varID = buffer[0];
74
75 updateVariable(varID, buffer + 1);
76}
77
78} // namespace SerialVar
79 // @endcond
Configure global build properties.
const char * LOG_TAG
Type definition of logging tags This typedef is necessary to allow for easier manipulation of code by...
Definition Log.h:48
Logging::Log_t Log
The global logging object.
Compilation of various helpful preprocessor macros.
Serial Variable Module.
#define SERIALVARS
X Macro of all the serial vars and their data type.
SerialVar is used for variables that can be modified over usb serial while still being used during ru...
Definition SerialVar.h:22
void receiveSerialVar()
Receive a variable over serial.
uint8_t * getVariable(size_t ID)
Get the Serial Variable for a given ID.
void w(LOG_TAG TAG, LOG_MSG message)
Log a string using a warning tag.
void i(LOG_TAG TAG, LOG_MSG message)
Log a string using an info tag.