-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinear.cpp
232 lines (209 loc) · 5.24 KB
/
linear.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#include <LiquidCrystal.h>
#include "Bounce2.h"
#include "linear.h"
#include "sensing.h"
#include "chars.h"
//Macros
#define BLANK(num) ({unsigned int i;for(i=0; i<num; i++){lcd->print(" ");}})
//Private prototypes
//differently from vanilla Arduino projects, those are needed, as the makefile
//used does not auto-generate the prototypes (thank god)
//also, the setup() and loop() functions *does not* need to be prototyped
void pinSetup();
void lcdSetup();
void updateReadings(Stats *stats); //update readings on screen
void bias(boolean run); //enable/disable amplification bias
void warning(boolean run); //enable/disable warning on screen and bias cutoff
void lock(boolean run); //enable/disable bias lock
void changeBand(); //change to the next band in the cycle
//LCD readings and positions, considering 20x4 screen
#define TX_POSX 0
#define TX_POSY 3
#define RX_POSX 0
#define RX_POSY 3
#define WARNING_POSX 12
#define WARNING_POSY 2
#define LOCKED_POSX 19
#define LOCKED_POSY 2
#define BAND_POSX 6
#define BAND_POSY 0
#define TEMP_POSX 16
#define TEMP_POSY 0
#define BANDSNUM 4 //number of different bands
#define BANDSIZE 7 //how many chars the largest band msg occupies
const char *txMsg = "TX=";
const char *rxMsg = "RX";
const char *warningMsg = "ATENCAO";
const char *bandMsg[4] = {
"80m",
"40m",
"20m/15m",
"12m/10m"
};
//Globals
LiquidCrystal *lcd;
boolean locked=false;
Stats stats;
Bounce bandsw=Bounce();
char currentBand=0;
State lastState=RX;
Warn lastWarn=OK;
//Arduino framework functions
//main() is auto-generated by the makefile and calls setup() once and loop()
//repeteadly, just as vanilla Arduino projects
void setup() {
pinSetup();
lcdSetup();
return;
}
void loop() {
readSensors(&stats);
if(statsOk(&stats)){
warning(false);
if(!locked && digitalRead(YTX_PIN))
bias(true);
else
bias(false);
}else{
warning(true);
lock(true);;
}
if(locked)
lock(digitalRead(REARM_PIN));
updateReadings(&stats);
bandsw.update();
if(bandsw.fell())
changeBand();
return;
}
void pinSetup(){
int i;
for(i=0;i<BANDSNUM;i++)
pinMode(band[i], OUTPUT);
pinMode(BIAS_PIN, OUTPUT);
bandsw.attach(BANDSW_PIN, INPUT_PULLUP);
bandsw.interval(DEBOUNCETIME);
pinMode(REARM_PIN, INPUT_PULLUP);
for(i=0;i<YD_PINS;i++)
pinMode(yd[i], INPUT);
return;
}
void lcdSetup(){
int i;
lcd = new LiquidCrystal(LCDR_PIN, LCDE_PIN, LCD4_PIN, LCD5_PIN, LCD6_PIN, LCD7_PIN);
lcd->begin(ROWS, COLUMNS); //AGM-2004D-201 has 20 rows and 4 lines
//black 1 to 4 aren't contained into LCD default charset, so we need to map
for(i=0;i<=3;i++)
lcd->createChar(char_black[i+1], map_black[i]);
lcd->createChar(char_key, map_key); //key symbol
lcd->setCursor(0, 0);
lcd->print("BANDA= T= ");
lcd->write(char_degree);
lcd->print("C");
lcd->setCursor(0,1);
lcd->print("POT.E= W ROE= .");
lcd->setCursor(0, 2);
lcd->print("POT.S= W");
lcd->setCursor(RX_POSX, RX_POSY);
lcd->print(rxMsg);
lcd->setCursor(BAND_POSX, BAND_POSY);
lcd->print(bandMsg[0]);
//Interface demo
/*
lcd->setCursor(6, 0);
lcd->print("12m/10m");
lcd->setCursor(16, 0);
lcd->print("99");
lcd->setCursor(6, 1);
lcd->print(" 90");
lcd->setCursor(16, 1);
lcd->print("1.50");
lcd->setCursor(6, 2);
lcd->print("990");
lcd->setCursor(WARNING_POSX, WARNING_POSY);
lcd->print(WARNING_MSG);
lcd->write(char_key);
lcd->setCursor(0, 3);
lcd->print("TX=");
for(i=0;i<=5;i++)
lcd->write(char_black[5]);
lcd->write(char_black[3]);
*/
return;
}
void changeBand(){
int i;
currentBand<BANDSNUM-1 ? currentBand++ : currentBand=0;
for(i=0;i<BANDSNUM;i++)
if(i==currentBand)
digitalWrite(band[i], HIGH);
else
digitalWrite(band[i], LOW);
lcd->setCursor(BAND_POSX, BAND_POSY);
BLANK(BANDSIZE); //largest band name
lcd->setCursor(BAND_POSX, BAND_POSY);
lcd->print(bandMsg[int(currentBand)]);
}
void bias(boolean run){
if(run){
if(lastState==RX){
digitalWrite(BIAS_PIN, HIGH);
lcd->setCursor(TX_POSX, TX_POSY);
lcd->print(txMsg);
lastState=TX;
}
}else{
if(lastState==TX){
digitalWrite(BIAS_PIN, LOW);
lcd->setCursor(TX_POSX, TX_POSY);
BLANK(COLUMNS); //blank whole line, since there could be a bargraph
lastState=RX;
lcd->setCursor(RX_POSX, RX_POSY);
lcd->print(rxMsg);
}
}
return;
}
void warning(boolean run){
if(run){
if(lastWarn==OK){
bias(false);
lcd->setCursor(WARNING_POSX, WARNING_POSY);
lcd->print(warningMsg);
lastWarn=WARN;
}
}else{
if(lastWarn==WARN){
lcd->setCursor(WARNING_POSX, WARNING_POSY);
BLANK(strlen(warningMsg));
lastWarn=OK;
}
}
return;
}
void lock(boolean run){
if(run){
if(!locked){
lcd->setCursor(LOCKED_POSX, LOCKED_POSY);
lcd->write(char_key);
locked=true;
}
} else {
if(locked){
lcd->setCursor(LOCKED_POSX, LOCKED_POSY);
BLANK(1);
locked=false;
}
}
}
void updateReadings(Stats *stats){
lcd->setCursor(TEMP_POSX, TEMP_POSY);
if(stats->temp > MAXTEMPREAD) //we only have space for 2 digits
lcd->print("!!");
else {
if (stats->temp <= 9) //liquidcrystal print doesn't support formatting
lcd->print("0");
lcd->print(stats->temp);
}
return;
}