-
Notifications
You must be signed in to change notification settings - Fork 0
/
master_lcd.cpp
295 lines (250 loc) · 6.33 KB
/
master_lcd.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#include <csignal>
#include <cstdlib>
#include <cstdio>
#include <string>
#include <vector>
#include <map>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <time.h>
#include <sys/mman.h> // for settuing up no page swapping
#include <bcm2835.h>
#include "rt_utils.hpp"
#include "nrf24l01.hpp"
#include "lcd_plate.hpp"
#include "messages.hpp"
#include "ensemble.hpp"
#include "Lock.hpp"
void nrf_tx(unsigned slave, uint8_t *buff, size_t len);
void slider(unsigned slave, uint8_t ch, uint16_t &v, int dir);
void hexdump(uint8_t* buff, size_t len);
void heartbeat(int slave);
void shutdown(int param);
void print_slave(int slave);
void process_ui();
using namespace std;
// Two global objects. sorry
RunTime runtime;
ofstream logfile;
bool time_to_die=false;
int main(int argc, char **argv)
{
Lock lock;
string log_fn="master_lcd.log";
signal(SIGTERM, shutdown);
signal(SIGINT, shutdown);
if (true)
{
struct sched_param sp;
memset(&sp, 0, sizeof(sp));
sp.sched_priority = sched_get_priority_max(SCHED_FIFO);
sched_setscheduler(0, SCHED_FIFO, &sp);
mlockall(MCL_CURRENT | MCL_FUTURE);
}
time_t rawtime;
struct tm * timeinfo;
char now [80];
time(&rawtime);
timeinfo = localtime (&rawtime);
strftime (now, 80, "%b %d %02H:%02M:%02S", timeinfo);
lcd_plate::setup(0x20);
lcd_plate::clear();
lcd_plate::set_cursor(0,0);
lcd_plate::set_backlight(lcd_plate::VIOLET);
nRF24L01::channel = 2;
memcpy(nRF24L01::master_addr, ensemble::master_addr, nRF24L01::addr_len);
memcpy(nRF24L01::broadcast_addr, ensemble::slave_addr[0], nRF24L01::addr_len);
memcpy(nRF24L01::slave_addr, ensemble::slave_addr[2], nRF24L01::addr_len);
nRF24L01::setup();
if (!nRF24L01::configure_base())
{
<< "Failed to find nRF24L01. Exiting." << endl;
return -1;
}
nRF24L01::configure_PTX();
nRF24L01::flush_tx();
while(!time_to_die)
{
heartbeat(0);
process_ui();
bcm2835_delayMicroseconds(5000);
}
nRF24L01::shutdown();
lcd_plate::clear();
lcd_plate::set_backlight(lcd_plate::OFF);
lcd_plate::shutdown();
bcm2835_close();
}
void shutdown(int param)
{
time_to_die=true;
}
void nrf_tx(unsigned slave, uint8_t *buff, size_t len)
{
using namespace nRF24L01;
static unsigned ack_err=0;
static unsigned tx_err=0;
bool ack = slave != 0;
write_tx_payload(buff, len, (const char*)ensemble::slave_addr[slave], ack);
uint8_t status;
int j;
for(j=0; j<100; j++)
{
status = read_reg(STATUS);
if (status & STATUS_TX_DS)
break;
delay_us(5);
}
if (status & STATUS_MAX_RT)
{
ack_err++;
write_reg(STATUS, STATUS_MAX_RT);
// data doesn't automatically removed...
flush_tx();
logfile << 1e-3*runtime.msec() << " ack_err " << ack_err << endl;
lcd_plate::set_cursor(0,12);
lcd_plate::puts(to_string(ack_err).c_str());
}
else if (status & STATUS_TX_DS)
write_reg(STATUS, STATUS_TX_DS); //Clear the data sent notice
else
logfile << 1e-3*runtime.msec() << " tx_err " << ++tx_err << endl;
}
// This is intended to mimic a slider control that can ramp up/down
// the intensity of an LED
void slider(unsigned slave, uint8_t ch, uint16_t &v, int dir)
{
if (dir>0)
{
if (v==0)
v=1;
else
v = (v << 1) + 1;
if (v >=4096)
v=4095;
}
else
{
if (v>0)
v >>= 1;
}
uint8_t buff[ensemble::message_size];
for (int i=0; i<sizeof(buff); i++) buff[i]=0;
::messages::encode_set_tlc_ch(buff, ch, v);
nrf_tx(slave, buff, sizeof(buff));
}
void print_slave(int slave)
{
lcd_plate::set_cursor(0,13);
lcd_plate::puts(to_string(slave).c_str());
}
void heartbeat(int slave)
{
static uint32_t last_hb=0;
if (runtime.msec() - last_hb < 1000)
return;
uint8_t buff[ensemble::message_size];
::messages::encode_heartbeat(buff, runtime.msec());
nrf_tx(slave, buff, sizeof(buff));
last_hb = runtime.msec();
}
class Effect
{
public:
Effect()
: state(idle)
{}
Effect(unsigned st, unsigned d, unsigned s)
: start_time(st), duration(d), slave(s),
state(pending)
{}
virtual void process(void) = 0;
virtual void stop(void) = 0;
enum State
{
idle, pending, started
} state;
unsigned start_time;
unsigned duration;
unsigned slave;
};
class Throb : public Effect
{
public:
virtual void process(void)
{
if (state==idle)
return;
uint32_t ms = runtime.msec();
if (ms < start_time)
return;
if (ms > start_time + duration)
{
state=idle;
return;
}
state=started;
unsigned sec = ms/1000;
ms -= sec*1000;
int v = ms;
if (!(sec & 1))
v = 1000-ms;
uint8_t buff[ensemble::message_size];
::messages::encode_set_tlc_ch(buff, 0, v);
nrf_tx(slave, buff, sizeof(buff));
}
};
void process_ui(void)
{
static unsigned slave=0;
static uint16_t red=0,green=0,blue=0;
typedef map<string, void *> MenuMap;
static MenuMap mm;
static MenuMap::const_iterator curr;
static uint8_t button=0;
static bool first_time = true;
if (first_time)
{
lcd_plate::puts("master");
print_slave(slave);
first_time=false;
}
uint8_t b = 0x1f & ~lcd_plate::read_buttons();
if (b!=button)
{
button = b;
logfile << 1e-3*runtime.msec() << " lcd keypress: " << hex << int(b) << endl;
if (b & lcd_plate::LEFT)
{
slave++;
if (slave>3)
slave=0;
print_slave(slave);
logfile << 1e-3*runtime.msec() << " slave " << slave << endl;
}
else if (b & lcd_plate::RIGHT)
{
//eff.start();
}
else if (b & lcd_plate::UP)
{
slider(slave, 0, red, 1);
slider(slave, 1, green, 1);
slider(slave, 2, blue, 1);
}
else if (b & lcd_plate::DOWN)
{
slider(slave, 0, red, -1);
slider(slave, 1, green, -1);
slider(slave, 2, blue, -1);
}
else if (b & lcd_plate::SELECT)
{
uint8_t buff[ensemble::message_size];
::messages::encode_start_effect(buff, 0, runtime.msec(), 1000);
nrf_tx(slave, buff, sizeof(buff));
logfile << 1e-3*runtime.msec() << " effect " << 0 << endl;
}
}
}