SAE Teensy ECU
IIT SAE Microcontroller programming
Loading...
Searching...
No Matches
Mirror.cpp
Go to the documentation of this file.
1
11//@cond
12
13#include "Mirror.h"
14#include "CanBus.h"
15#include "ECUGlobalConfig.h"
16#include "Heartbeat.h"
17#include "Log.h"
18#include "Pins.h"
19#include "SerialCommand.h"
20
21namespace Mirror {
22
23#define TIMER_RECEIVE_MICROS 2000
24
25static LOG_TAG ID = "Mirror";
26static bool cont = false;
27static bool init = false;
28static bool receive = true;
29
30static void toggleMirrorMode(void) {
31 cont = !cont;
32 if (cont) {
34 } else {
36 }
37}
38
39void timerReceive() {
40 if (receive)
42}
43
44void setup(void) {
45 if (!init) {
46#if CONF_ECU_POSITION == BACK_ECU
47 receive = true;
48 Heartbeat::addCallback(timerReceive);
49#endif
51 }
52 init = true;
53}
54
55static int getSerialByte() {
56 int serialData = 0;
57 while (!Serial.available()) {
58 }
59 serialData = Serial.read();
60 Log.d(ID, "Data received:", serialData);
61 return serialData;
62}
63
64static int getSerialInt() {
65 int serialData = 0;
66 while (!Serial.available()) {
67 }
68 size_t count = Serial.readBytes((char *)(&serialData), 4);
69 if (count != 4) {
70 Log.w(ID, "Did not receive 4 bytes for integer", count);
71 }
72
73 Log.d(ID, "Data received:", serialData);
74 return serialData;
75}
76
77void enterMirrorMode(void) {
78#if CONF_ECU_POSITION == BACK_ECU
79 receive = false;
80#endif
81 cont = true;
82 Log(ID, "Mirror Mode Enabled");
83 while (cont) { // NOTE: Low priority heartbeat will freeze because of this
84 Log(ID, "Waiting for data");
85 int serialData = getSerialByte();
86 if (serialData == COMMAND_TOGGLE_MIRROR_MODE) {
87 toggleMirrorMode();
88 } else if (serialData == COMMAND_ENTER_MIRROR_SET) {
89 Log(ID, "Waiting for a pin to set");
90 int pin = getSerialByte();
91 Log(ID, "Waiting for the value to set it to");
92 int value = getSerialInt();
93 Log(ID, "Setting pin:", pin);
94 Log(ID, "To value:", value);
95 Pins::setPinValue(pin, value);
96 } else {
97 Log(ID, "Requested pin", Pins::getPinValue(serialData));
98 }
99 // delay(50);
100 }
101 Log(ID, "Mirror Mode Disabled");
102#if CONF_ECU_POSITION == BACK_ECU
103 receive = true;
104#endif
105}
106
107void exitMirrorMode(void) {
108 cont = false;
109}
110
111} // namespace Mirror
112 //@endcond
FlexCAN_T4 wrapper.
Configure global build properties.
Make one ECU tell the other it is alive.
Special logging functionality.
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.
This module allows for the monitoring and modification of each GPIO pin on an ECU.
Update, set, and get predefined pin values.
#define COMMAND_TOGGLE_MIRROR_MODE
Command to enable mirror mode, for Mirror.h.
#define COMMAND_ENTER_MIRROR_SET
Command to listen for a pin to set, for Mirror.h.
SerialCommand functionality.
int receiveCommand(void)
receive any command from serial by matching incoming bytes to a callback
void setCommand(uint8_t command, CommandCallback callback)
attach a single callback to a byte value that will be received over serial
void addCallback(beatFunc func)
Add a callback to be run at each haertbeat.
Check and modify pin values over USB serial How to use - connect with a terminal that can both send a...
Definition Mirror.h:40
void setup(void)
Setup a listener for going into mirror mode, dependent on which ECU is compiled.
void enterMirrorMode(void)
Manually enter mirror mode, only returning when a set command is received.
void exitMirrorMode(void)
Manually exit mirror mode.
void setPinValue(uint8_t GPIO_Pin, int value)
Set the pin value of a predefined pin.
int getPinValue(uint8_t GPIO_Pin)
Get the pin value of a predefined pin.
void d(LOG_TAG TAG, LOG_MSG message)
Log a string using a debug tag.
void w(LOG_TAG TAG, LOG_MSG message)
Log a string using a warning tag.