-
Notifications
You must be signed in to change notification settings - Fork 0
/
DarkTower.ino
219 lines (193 loc) · 4.13 KB
/
DarkTower.ino
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
#include <Arduino.h>
#include "epd.h"
#include "darkTower.h"
#include "MsgHandlers.h"
#define DEBOUNCE 500
#define BTN01 474
#define BTN02 498
#define BTN03 524
#define BTN04 552
#define BTN05 584
#define BTN06 620
#define BTN07 660
#define BTN08 706
#define BTN09 759
#define BTN10 820
#define BTN11 892
#define BTN12 977
Queue_Msgs g_nLastButton=0;
int currentAction = 0;
//darkTower g_game;
Queue g_qMsg(sizeof(Queue_Msgs), 10, FIFO); // Instantiate queue
void (*msgHandlers[eHandlerCount])() =
{ //order must match Queue_Msgs in MsgHandlers.h
&OneBtn //0
,&TwoBtn //1
,&ThreeBtn //2
,&FourBtn //3
// ,&HaggleBtn //4
,&BazaarBtn //4
,&TombBtn //5
,&MoveBtn //6
,&CitadelBtn //7
,&DarkTwrBtn //8
,&FrontierBtn //9
,&AbortBtn //10
,&DisplaySetup1 //11
,&DisplaySetup2 //12
,&ReadyPlayer //13
,&Idle //14
,&EndTurn //15
,&BazaarItem //16
,&BazaarInitBuy //17
,&BazaarBuy //18
,&BattleMsg //19
};
//**************************************
void PostMsg(Queue_Msgs nMsg)
{
g_qMsg.push(&nMsg);
}
void HandleMsg()
{
Queue_Msgs nMsg;
if(!g_qMsg.isEmpty())
{
g_qMsg.pop(&nMsg);
msgHandlers[nMsg]();
if(nMsg != 19)
{
SERIALLOG("HandleMsg - ");
SERIALLOG_LNN(nMsg, DEC);
}
}
}
Queue_Msgs decodeAnalog(int level)
{
int buttonIdx =0;
if(level > BTN12)
{
buttonIdx = eBTN_FOUR;
}
else if(level > BTN11)
{
//TODO split ruin off buttonIdx = eBTN_HAGGLE ;
}
else if(level > BTN10)
{
buttonIdx = eBTN_ABORT ;
}
else if(level > BTN09)
{
buttonIdx = eBTN_THREE ;
}
else if(level > BTN08)
{
buttonIdx = eBTN_TOMB;
}
else if(level > BTN07)
{
buttonIdx = eBTN_DARKTWR ;
}
else if(level > BTN06)
{
buttonIdx = eBTN_TWO ;
}
else if(level > BTN05)
{
buttonIdx = eBTN_MOVE;
}
else if(level > BTN04)
{
buttonIdx = eBTN_CITADEL;
}
else if(level > BTN03)
{
buttonIdx = eBTN_ONE ;
}
else if(level > BTN02)
{
buttonIdx = eBTN_BAZAAR ;
}
else if(level > BTN01)
{
buttonIdx = eBTN_FRONTR ;
}
else
{
buttonIdx = 0;
}
return buttonIdx;
}
//**********************************************************
void setup() {
#ifdef DISABLE_EPD
Serial.begin(115200);
#else
/**/
epd_init();
epd_wakeup();
epd_set_memory(MEM_TF);
epd_set_color(BLACK, WHITE);
/**/
#endif
SERIALLOG_LN("DISABLE_EPD");
int seed = analogRead(1) +analogRead(2)+analogRead(3)+ analogRead(4)+analogRead(5);
randomSeed(seed);
/*g_game.*/Init();
PostMsg(eMSG_SETUP1);
}
void loop() {
// put your main code here, to run repeatedly:
int analogLevel=0;
Queue_Msgs currentButton=0;
static unsigned long nBtnTime =0;
analogLevel = analogRead(0);/*returns (0 to 1023)*/
currentButton = decodeAnalog(analogLevel);
if(analogLevel >0)
{
if(currentButton != g_nLastButton && millis() - nBtnTime > DEBOUNCE)
{
nBtnTime = millis();
int seed = (analogRead(1) +analogRead(5))* nBtnTime + analogRead(2) -analogRead(4);
randomSeed(seed);
g_nLastButton = currentButton;
PostMsg(currentButton);
}
}
else
{
g_nLastButton = 511;
}
HandleMsg();
delay(50);
}
// **********************************************************
// Message Handlers
/*
void OneBtn()
{
// MYERROR
g_game.OneBtn();
}
void TwoBtn(){g_game.TwoBtn();}
void ThreeBtn(){g_game.ThreeBtn();}
void FourBtn(){g_game.FourBtn();}
void HaggleBtn(){}
void BazaarBtn(){g_game.BazaarBtn();}
void BazaarItem(){g_game.BazaarItem();}
void BazaarInitBuy(){g_game.BazaarInitBuy();}
void BazaarBuy(){g_game.BazaarBuy();}
void TombBtn(){g_game.TombBtn();}
void MoveBtn(){g_game.MoveBtn();}
void CitadelBtn(){g_game.CitadelBtn();}
void DarkTwrBtn(){}
void FrontierBtn(){g_game.FrontierBtn();}
void AbortBtn(){g_game.AbortBtn();}
void DisplaySetup1(){g_game.DisplaySetup1();}
void DisplaySetup2(){g_game.DisplaySetup2();}
void ReadyPlayer(){g_game.ReadyPlayer();}
void Idle(){g_game.Idle();}
void EndTurn(){g_game.EndTurn();}
void BattleMsg(){}
*/