-
Notifications
You must be signed in to change notification settings - Fork 0
/
DisplayControl.cpp
114 lines (103 loc) · 3.5 KB
/
DisplayControl.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/**
* @file DisplayControl.cpp
* @author Ryan Johnson ([email protected])
* @brief Display view models and controllers
* @version 0.1
* @date 2020-06-16
*
* @copyright Copyright (c) 2020
*
*/
#include "DisplayControl.h"
#include <Arduino.h>
#include <cstdio>
#include <cstring>
#define OK_OR_HALT_OR_OFF(corner, enable) \
((enable)) ? (((corner)) ? "HALT" : "OK") : "OFF"
using namespace Display;
void AbstractDisplayView::BlankDisplayableText(DisplayableText *text)
{
memset(text, " ", sizeof(DisplayableText));
}
DisplayableText DisplayViewFault::Render(const SystemDisplayState &dispState)
{
DisplayableText text;
BlankDisplayableText(&text);
strncpy(text.line_struct.line1, "!! Fault Detected !!", 21);
strncpy(text.line_struct.line2, Fault::ecodeName[dispState.faultType], 21);
strncpy(text.line_struct.line3, Fault::ecodeHelpText[dispState.faultType],
21);
if (dispState.faultType > Fault::FATAL_END_SENTINEL) {
strncpy(text.line_struct.line4, " (please try again) ", 21);
}
else {
strncpy(text.line_struct.line4, " (re-boot required) ", 21);
}
return text;
}
DisplayableText DisplayViewNormal::Render(const SystemDisplayState &dispState)
{
DisplayableText text;
BlankDisplayableText(&text);
char pitch[8];
char roll[8];
dtostrf(dispState.pitch, 7, 2, pitch);
dtostrf(dispState.roll, 7, 2, roll);
snprintf(text.line_struct.line1, 21, "%-10s P:%7s",
Motion::k_motionStateNames[dispState.motionState], pitch);
if (dispState.motionState == Motion::MotionStateMachine::STATE_MOVING) {
if (dispState.dirn == Motion::MovementDirection::RAISE) {
snprintf(text.line_struct.line2, 21, "RAISING R:%7s", roll);
}
else if (dispState.dirn == Motion::MovementDirection::LOWER) {
snprintf(text.line_struct.line2, 21, "LOWERING R:%7s", roll);
}
else {
snprintf(text.line_struct.line2, 21, "OFF R:%7s", roll);
}
}
else if (dispState.motionState ==
Motion::MotionStateMachine::STATE_FAULTED) {
snprintf(text.line_struct.line2, 21, "CAN RESUME R:%7s", roll);
}
else {
snprintf(text.line_struct.line2, 21, "SYSTEM OK R:%7s", roll);
}
strncpy(text.line_struct.line3, "-1-- -2-- -3-- -4-- ", 21);
snprintf(text.line_struct.line4, 21, "%-4s %-4s %-4s %-4s ",
OK_OR_HALT_OR_OFF(dispState.ram1, dispState.enable),
OK_OR_HALT_OR_OFF(dispState.ram2, dispState.enable),
OK_OR_HALT_OR_OFF(dispState.ram3, dispState.enable),
OK_OR_HALT_OR_OFF(dispState.ram4, dispState.enable));
return text;
}
bool Controller::begin()
{
lcd.begin(20, 4);
lcd.setBacklight(HIGH);
lcd.setCursor(0, 0);
}
void Controller::update(SystemDisplayState &state)
{
DisplayableText t;
if (state.faultType != Fault::ALL_OK) {
DisplayViewFault faultDisp;
t = faultDisp.Render(state);
}
else {
DisplayViewNormal normalDisp;
t = normalDisp.Render(state);
}
writeRaw(t);
}
void Controller::writeRaw(DisplayableText &t)
{
lcd.setCursor(0, 0);
lcd.print(t.line_struct.line1);
lcd.setCursor(0, 1);
lcd.print(t.line_struct.line2);
lcd.setCursor(0, 2);
lcd.print(t.line_struct.line3);
lcd.setCursor(0, 3);
lcd.print(t.line_struct.line4);
}