-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEditor.cpp
624 lines (600 loc) · 17.6 KB
/
Editor.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
#include <Arduino.h>
#include <Adafruit_NeoPixel.h>
#include "ReadWrite.h"
#include "BTN.h"
#include "Wheel.h"
#include "PixelRing.h"
#include "Config.h"
#include "Tape.h"
#include "Machine.h"
#include "Examples.h"
#include "Programs.h"
#include "Editor.h"
// The main editor loop does EVERYTHING
Editor editor;
void Editor::Init()
{
}
void Editor::Edit()
{
// runs the edit loop which includes running the machine
_mode = Labels::Tape;
while (true)
{
// wait for a click:
if (_mode == Labels::Tape)
{
EditTape();
}
else if (_mode == Labels::Machine)
{
EditMachine();
}
else if (_mode == Labels::Menu)
{
EditMenu();
}
else if (_mode == Labels::Run)
{
SetTapeCursor(machine.Run());
break;
}
}
}
void Editor::SetTapeCursor(int idx)
{
// Set location of cursor on tape
_tapeCursor = idx;
}
void Editor::EditTape()
{
// the default editing mode, show the tape
// selecting a pixel changes to a menu to set it's symbol and offer other actions
tape.Show();
pixelRing.SetCursor(_tapeCursor);
// show that they're symbols and what else they can choose, eg Run
uint32_t context = LABEL_BIT(Labels::Tape) + LABEL_BIT(Labels::Symbol);
uint32_t allowed = LABEL_BIT(Labels::Run) + LABEL_BIT(Labels::Machine) + LABEL_BIT(Labels::Clear) + LABEL_BIT(Labels::Menu);
bool longPress = true;
while (true)
{
Labels pick = Pick(&longPress);
if (longPress)
{
if (pick == Labels::Tape)
{
tape.Pop();
_tapeCursor = 0;
break;
}
else if (pick == Labels::Clear)
{
tape.Clear();
_tapeCursor = 0;
break;
}
else
{
Default(context, allowed, pick);
break;
}
}
int cell = pixelRing.LabelToDisplayPixel(pick); // clicked on a cell to edit
// pick a symbol (or a menu):
pick = PickSymbol(tape.Get(cell), context, allowed);
if (Machine::IsASymbol(pick))
{
// symbol chosen, update the tape
tape.Set(cell, pick);
tape.Show();
// advance the cursor
pixelRing.SetCursor(cell);
pixelRing.MoveCursor(+1);
}
else if (pick == Labels::Clear)
{
tape.Clear();
break;
}
else
{
Default(context, allowed, pick);
break;
}
}
}
void Editor::EditMachine()
{
// edit the machine
// offer a list of states, picking one edits it
// show that they're symbols and what else they can choose, eg Run
uint32_t context = LABEL_BIT(Labels::Machine) + LABEL_BIT(Labels::State);
uint32_t allowed = LABEL_BIT(Labels::Run) + LABEL_BIT(Labels::Tape) + LABEL_BIT(Labels::Clear) + LABEL_BIT(Labels::Menu);
while (_mode == Labels::Machine)
{
// pick a state (or a menu):
Labels pick = PickState(Labels::A, context, allowed);
if (Machine::IsAState(pick, false)) // NOT Halt
{
// state chosen, edit it
EditState(pick);
}
else if (pick == Labels::Clear)
{
machine.Clear();
}
else
{
if (!Default(context, allowed, pick))
{
_mode = Labels::Tape;
}
break;
}
}
}
void Editor::EditState(Labels state)
{
// edit the specific state; the table of instructions
// show that they're symbols and what else they can choose, eg Run
uint32_t context = LABEL_BIT(Labels::Machine) + LABEL_BIT(Labels::Symbol);
uint32_t allowed = LABEL_BIT(Labels::Run) + LABEL_BIT(Labels::Tape) + LABEL_BIT(Labels::Clear) + LABEL_BIT(Labels::Menu) + LABEL_BIT(Labels::State);
while (_mode == Labels::Machine)
{
// pick a symbol (i.e an instruction) (or a menu):
Labels pick = PickInstruction(state, context, allowed);
if (Machine::IsASymbol(pick))
{
EditInstruction(state, pick);
}
else if (pick == Labels::State)
{
// go up
break;
}
else if (pick == Labels::Clear)
{
machine.Clear(state);
}
else
{
Default(context, allowed, pick);
break;
}
}
}
void Editor::EditInstruction(Labels state, Labels symbol)
{
// edit a specific instruction
uint32_t context = LABEL_BIT(Labels::Instruction);
uint32_t allowed = LABEL_BIT(Labels::Run) + LABEL_BIT(Labels::Tape) + LABEL_BIT(Labels::Machine) + LABEL_BIT(Labels::Symbol);
Machine::Instruction instruction;
machine.GetInstruction(state, symbol, instruction);
pixelRing.Clear();
SetCursor(Labels::WriteSymbol);
while (true)
{
ShowMenu(context, allowed);
ShowInstruction(symbol, instruction);
ShowSymbol(symbol);
Labels pick = Pick();
if (pick == Labels::WriteSymbol)
{
pick = PickSymbol(instruction._writeSymbol, LABEL_BIT(Labels::Instruction) + LABEL_BIT(Labels::Symbol), 0);
if (Machine::IsASymbol(pick))
instruction._writeSymbol = pick;
SetCursor(Labels::WriteSymbol);
}
else if (pick == Labels::NewState)
{
pick = PickState(instruction._newState, LABEL_BIT(Labels::Instruction) + LABEL_BIT(Labels::State), 0);
if (Machine::IsAState(pick, true))
instruction._newState = pick;
SetCursor(Labels::NewState);
}
else if (pick == Labels::MoveRight)
{
instruction._moveHead = +1;
}
else if (pick == Labels::MoveLeft)
{
instruction._moveHead = -1;
}
else if (pick == Labels::MoveNone)
{
instruction._moveHead = 0;
}
else if (pick == Labels::Symbol)
{
// go up
machine.PutInstruction(state, symbol, instruction);
break;
}
else
{
machine.PutInstruction(state, symbol, instruction);
Default(context, allowed, pick);
break;
}
}
}
void Editor::EditMenu()
{
// Offer extras
// Menu.A.S load example #S from PROGMEM
// Menu.B.S load from EEPROM slot #S
// Menu.C.S save to EEPROM slot #S
// Menu.D read from Serial
// Menu.E write to Serial
// Menu.F Clock
// Menu.G Set time
// Menu.H Brightness
// Menu.I Off
uint32_t context = LABEL_BIT(Labels::Menu) + LABEL_BIT(Labels::State);
uint32_t allowed = 0;
pixelRing.Clear();
ShowMenu(context, allowed);
SetCursor(Labels::Menu);
uint32_t secondLevelPickColour = pixelRing.Colour(ColourPalette::Malachite);
while (true)
{
Labels pick = PickN(Index(Labels::Menu_Last) - Index(Labels::Menu_First) + 1, context, allowed);
if (pick == Labels::Menu)
{
break;
}
else if (pick == Labels::Menu_LoadPROGMEM)
{
// load example N from PROGMEM
Labels pick = PickN(Examples::Count, LABEL_BIT(Labels::Menu) + LABEL_BIT(Labels::Symbol), 0, secondLevelPickColour);
if (machine.IsASymbol(pick))
{
Examples::Load(static_cast<int>(pick) - 1);
}
break;
}
else if (pick == Labels::Menu_LoadEEPROM)
{
// load machine from EEPROM slot N
Labels pick = PickN(config._NumberOfMachineSlotsInEEPROM, LABEL_BIT(Labels::Menu) + LABEL_BIT(Labels::Symbol), 0, secondLevelPickColour);
if (machine.IsASymbol(pick))
{
config.LoadSlot(Index(pick) - 1);
}
break;
}
else if (pick == Labels::Menu_SaveEEPROM)
{
// save machine to EEPROM slot N
Labels pick = PickN(config._NumberOfMachineSlotsInEEPROM, LABEL_BIT(Labels::Menu) + LABEL_BIT(Labels::Symbol), 0, secondLevelPickColour);
if (machine.IsASymbol(pick))
{
config.SaveSlot(Index(pick) - 1);
}
break;
}
else if (pick == Labels::Menu_ReadSerial)
{
// read from Serial
SerialReader reader;
// show we're waiting
pixelRing.Clear();
ShowMenu(LABEL_BIT(Labels::Menu), 0);
SetCursor(Labels::Null);
pixelRing.Show();
machine.DeSerialise(reader);
break;
}
else if (pick == Labels::Menu_WriteSerial)
{
// write to Serial
SerialWriter writer;
machine.Serialise(writer);
break;
}
else if (pick == Labels::Menu_Brightness)
{
// set brightness
SetBrightness();
break;
}
else if (pick == Labels::Menu_Speed)
{
// set speed
SetSpeed();
break;
}
else if (pick == Labels::Menu_Off)
{
// blank the ring and wait
SetCursor(Labels::Null);
pixelRing.Clear();
pixelRing.Show();
while (true)
{
wheel.Update();
if (wheel.GetRotation() != 0)
break;
if (btn.CheckButtonPress())
break;
}
break;
}
else
{
// run a program Clock, Set
Program(pick);
break;
}
}
_mode = Labels::Tape;
}
Labels Editor::Pick(bool* longPress)
{
// updates the wheel, pixels and the cursor waiting for a button press, returns the label selected
// if longPress is non-NULL, will set to true if button was held
while (true)
{
wheel.Update();
pixelRing.Update();
pixelRing.MoveCursor(wheel.GetRotation());
if (btn.CheckButtonPress())
{
if (longPress)
{
// interested in a long press
int ctr = LongPressDurationMS;
*longPress = false;
while (ctr--)
{
if (!btn.IsDown()) // released early, not a long press
break;
delay(1);
}
if (btn.IsDown())
{
*longPress = true; // held down long enough
}
}
return pixelRing.LabelAtCursor();
}
}
return Labels::Null;
}
Labels Editor::PickSymbol(Labels defaultLabel, uint32_t context, uint32_t allowed)
{
// return a symbol (or a menu item)
pixelRing.Clear();
ShowSymbols();
ShowMenu(context, allowed);
SetCursor((defaultLabel != Labels::Blank)?defaultLabel:Labels::A);
return Pick();
}
Labels Editor::PickN(int Count, uint32_t context, uint32_t allowed, uint32_t colour)
{
// pick from the first N symbols, coloured green
pixelRing.Clear();
if (!colour)
colour = pixelRing.Colour(MenuAllowedColour);
int n = 0;
for (int lbl = Index(Labels::A); lbl <= Index(Labels::LastLetter); lbl++, n++)
{
Labels label = Label(lbl);
if (n < Count)
pixelRing.SetPixel(pixelRing.LabelToDisplayPixel(label), colour);
}
ShowMenu(context, allowed);
SetCursor(Labels::A);
Labels pick = Pick();
if (machine.IsASymbol(pick))
{
n = Index(pick) - Index(Labels::A);
if (n < 0 || n >= Count)
pick = Labels::Null;
}
return pick;
}
Labels Editor::PickState(Labels defaultLabel, uint32_t context, uint32_t allowed)
{
// return a state (or a menu item)
pixelRing.Clear();
ShowSymbols();
ShowMenu(context, allowed);
SetCursor(defaultLabel);
return Pick();
}
Labels Editor::PickInstruction(Labels state, uint32_t context, uint32_t allowed)
{
// return a symbol (or a menu item)
pixelRing.Clear();
ShowSymbols();
ShowMenu(context, allowed);
SetCursor(Labels::A);
ShowInstruction(state, pixelRing.LabelAtCursor());
while (true) // live update of the instruction
{
wheel.Update();
pixelRing.Update();
if (pixelRing.MoveCursor(wheel.GetRotation()))
{
// blink
ShowInstruction(state, Labels::Null);
pixelRing.Update();
delay(25);
ShowInstruction(state, pixelRing.LabelAtCursor());
}
if (btn.CheckButtonPress())
return pixelRing.LabelAtCursor();
}
return Labels::Null;
}
void Editor::ShowSymbols()
{
// show the available symbols
for (int lbl = Index(Labels::A); lbl <= Index(Labels::LastLetter); lbl++)
{
Labels label = Label(lbl);
pixelRing.SetPixel(pixelRing.LabelToDisplayPixel(label), tape.SymbolColour(label));
}
pixelRing.SetPixel(pixelRing.LabelToDisplayPixel(Labels::Blank), tape.SymbolColour(Labels::Blank));
}
void Editor::ShowSymbol(Labels sym)
{
// show just one symbol/state
for (int lbl = Index(Labels::A); lbl <= Index(Labels::LastLetter); lbl++)
{
Labels label = Label(lbl);
pixelRing.SetPixel(pixelRing.LabelToDisplayPixel(label), pixelRing.Colour(ColourPalette::Black));
}
pixelRing.SetPixel(pixelRing.LabelToDisplayPixel(sym), tape.SymbolColour(sym));
}
void Editor::ShowMenu(uint32_t context, uint32_t allowed)
{
// show the context (current) menu items white, allowed (available) green. the rest black
uint32_t menuBitMask = LABEL_BIT(Labels::Clear) + LABEL_BIT(Labels::Tape) + LABEL_BIT(Labels::Instruction) + LABEL_BIT(Labels::State) + LABEL_BIT(Labels::Machine) + LABEL_BIT(Labels::Symbol) + LABEL_BIT(Labels::Run) + LABEL_BIT(Labels::Menu);
for (int lbl = 0; lbl < 32; lbl++)
{
if (context & (1UL << lbl))
pixelRing.SetPixel(pixelRing.LabelToDisplayPixel(Label(lbl)), pixelRing.Colour(MenuContextColour));
else
if (menuBitMask & (1UL << lbl))
pixelRing.SetPixel(pixelRing.LabelToDisplayPixel(Label(lbl)), pixelRing.Colour(ColourPalette::Black));
if (allowed & (1UL << lbl))
pixelRing.SetPixel(pixelRing.LabelToDisplayPixel(Label(lbl)), pixelRing.Colour(MenuAllowedColour));
}
}
void Editor::ShowInstruction(Labels state, Labels sym)
{
// show the Instruction (write, move, new state) when in the given state and reading the given symbol
Machine::Instruction instruction;
if (Machine::IsASymbol(sym) && machine.GetInstruction(state, sym, instruction))
{
ShowInstruction(sym, instruction);
}
else
{
ShowInstruction(Labels::Null, instruction); // blank
}
}
void Editor::ShowInstruction(Labels sym, Machine::Instruction& instruction)
{
// show the given Instruction (write, move, new state) when the given symbol
uint32_t Write = tape.SymbolColour(Labels::Blank);
uint32_t Left = pixelRing.Colour(ColourPalette::Black);
uint32_t None = pixelRing.Colour(ColourPalette::Black);
uint32_t Right = pixelRing.Colour(ColourPalette::Black);
uint32_t NewState = tape.SymbolColour(Labels::Blank);
if (Machine::IsASymbol(sym))
{
Write = tape.SymbolColour(instruction._writeSymbol);
if (instruction._moveHead == 0)
{
None = pixelRing.Colour(ColourPalette::White);
// make an undefined instruction more obvious, make no move grey
if (instruction._writeSymbol == Labels::Blank && instruction._newState == Labels::Halt)
{
None = pixelRing.Colour(ColourPalette::Grey);
}
}
else if (instruction._moveHead == -1)
Left = pixelRing.Colour(ColourPalette::White);
else
Right = pixelRing.Colour(ColourPalette::White);
NewState = tape.SymbolColour(instruction._newState);
}
pixelRing.SetPixel(pixelRing.LabelToDisplayPixel(Labels::WriteSymbol), Write);
pixelRing.SetPixel(pixelRing.LabelToDisplayPixel(Labels::MoveLeft), Left);
pixelRing.SetPixel(pixelRing.LabelToDisplayPixel(Labels::MoveNone), None);
pixelRing.SetPixel(pixelRing.LabelToDisplayPixel(Labels::MoveRight), Right);
pixelRing.SetPixel(pixelRing.LabelToDisplayPixel(Labels::NewState), NewState);
}
void Editor::SetCursor(Labels label)
{
// set the cursor at the label
pixelRing.SetCursor((label != Labels::Null)?pixelRing.LabelToDisplayPixel(label):-1);
}
bool Editor::Default(uint32_t /*context*/, uint32_t allowed, Labels pick)
{
// default handling of a pick at label
if (allowed & LABEL_BIT(pick))
{
// menu chosen, change mode
_mode = pick;
return true;
}
return false;
}
void Editor::SetBrightness()
{
int brite = config._PixelBrightness;
uint16_t dHue = 0xFFFF/PixelRing::Count;
for (int idx = 0; idx < PixelRing::Count; idx++)
{
pixelRing.SetPixel(idx, Adafruit_NeoPixel::ColorHSV(idx*dHue));
}
pixelRing.SetCursor(brite/Config::StepBrightness - 1);
while (true)
{
pixelRing.Brightness(brite);
pixelRing.Update();
wheel.Update();
int rot = wheel.GetRotation();
if (rot)
{
brite += rot*Config::StepBrightness;
if (brite < Config::MinBrightness)
brite = Config::MinBrightness;
if (brite > Config::MaxBrightness)
brite = Config::MaxBrightness;
pixelRing.SetCursor(brite/Config::StepBrightness - 1);
}
else if (btn.CheckButtonPress())
break;
}
config._PixelBrightness = brite;
config.Save();
}
void Editor::SetSpeed()
{
// speed (AND animation mode)
int DelayMS = config._MachineStepDelayMS;
int steps = (Config::MaxStepDelayMS - Config::MinStepDelayMS)/Config::StepDelayIncrementMS;
uint16_t dHueBlue = 0xAAAA; // blue
uint16_t dHue = (0xFFFF - dHueBlue)/(steps - 1); // blue-red
pixelRing.Clear();
for (int idx = 0; idx < steps; idx++)
{
pixelRing.SetPixel(idx, Adafruit_NeoPixel::ColorHSV(dHueBlue + idx*dHue));
}
pixelRing.SetPixel(pixelRing.LabelToDisplayPixel(Labels::State), pixelRing.Colour(MenuAllowedColour)); // animate state
int csr = steps - DelayMS/Config::StepDelayIncrementMS - 1;
pixelRing.SetCursor(csr);
while (true)
{
pixelRing.Update();
wheel.Update();
int rot = wheel.GetRotation();
if (rot)
{
csr += rot;
if (csr < 0) csr = PixelRing::Count - 1;
if (csr >= PixelRing::Count) csr = 0;
pixelRing.SetCursor(csr);
}
else if (btn.CheckButtonPress())
break;
}
DelayMS = Config::MinStepDelayMS + (steps - csr - 1)*Config::StepDelayIncrementMS;
if (csr == pixelRing.LabelToDisplayPixel(Labels::State))
{
// toggle animate state
config._AmimateState = !config._AmimateState;
config.Save();
}
else if (Config::MinStepDelayMS <= DelayMS && DelayMS <= Config::MaxStepDelayMS)
{
// save speed
config._MachineStepDelayMS = DelayMS;
config.Save();
}
}