menu and screen texts
[asdevice.git] / asdevice / device.c
blob84369a80cf0e89da6ef7f1f5a3d84146a460c784
1 /**
2 ************************************************************************************************
3 ** Program pre ariadenie emulujuce nastrazny system pozostavajuce z :
4 ** - 2x majak
5 ** - 1x sirena
6 ** - 1x LCD
7 ** - 1x keypad 4x4
8 ** Umoznuje hrat scenar pre 2 timy, kazdy z timov sa snazi o zapnutie resp. vypnutie zariadenia.
9 ************************************************************************************************
10 **/
11 #include "device.h"
13 // menu strings
14 //---------------------0123456789ABCDEF----
15 char s000[] PROGMEM = "\0";
16 char s001[] PROGMEM = "Made by OP-F\0";
17 char s002[] PROGMEM = "Start game\0";
18 char s003[] PROGMEM = "Game Setup\0";
19 char s004[] PROGMEM = "Device state\0";
20 char s005[] PROGMEM = "Act/Def codes:\0";
21 char s006[] PROGMEM = "Game begin in:\0";
22 char s007[] PROGMEM = "Enter CODE:\0";
23 char s008[] PROGMEM = "ACTIVE IN:\0";
24 char s009[] PROGMEM = "DEFUSED IN:\0";
25 char s010[] PROGMEM = "LAST ACTION\0";
26 char s011[] PROGMEM = "!CANCELLED!\0";
27 char s012[] PROGMEM = "Device\0";
28 char s013[] PROGMEM = "DEFUSED!\0";
29 char s014[] PROGMEM = "ACTIVATED!\0";
30 char s015[] PROGMEM = "!! GAME OVER !!\0";
31 char s016[] PROGMEM = "!! BLUE WINS !!\0";
32 char s017[] PROGMEM = "!! RED WINS !!\0";
35 //set screens
36 PGM_P welcomeScreen[] PROGMEM = {s000,s001};
37 #define welcomeScreenSize (sizeof(welcomeScreen)/sizeof(&welcomeScreen))
38 PGM_P mainScreen[] PROGMEM = {s002,s003,s004};
39 #define mainScreenSize (sizeof(mainScreen)/sizeof(&mainScreen))
40 PGM_P codeScreen[] PROGMEM = {s005};
41 #define codeScreenSize (sizeof(codeScreen)/sizeof(&codeScreen))
44 PGM_P* screens[] PROGMEM = {welcomeScreen,mainScreen,codeScreen};
45 int screensSizes[] = {welcomeScreenSize,mainScreenSize,codeScreenSize};
47 void welcomeAction(DeviceState*, PGM_P*, int scrSize);
48 void mainAction(DeviceState*, PGM_P*, int scrSize);
49 void startGame(DeviceState*, PGM_P*, int scrSize);
50 void setupDevice(DeviceState*, PGM_P*, int scrSize);
51 void showCodes(DeviceState*, PGM_P*, int scrSize);
53 void delay_us(unsigned short time_us);
54 void generateCodes(DeviceState*);
55 long getUniqueRandom(DeviceState* d);
57 int main(void){
58 // LCD init and printf to LCD
59 lcdInit();
61 // set initial device state and menu
62 DeviceState* device = malloc(sizeof(DeviceState));
64 (*device).stateChanged = 1;
65 (*device).screenChanged = 0;
66 (*device).stateId = 0;
67 (*device).menuOffset = 0;
69 //set function array
70 StateAction* actions = malloc(sizeof(StateAction)*10);
71 actions[0].action = welcomeAction;
72 actions[1].action = mainAction;
73 actions[3].action = startGame;
76 while(1){
77 actions[(*device).stateId].action(device,screens[(*device).stateId],screensSizes[(*device).stateId]);
80 return 0;
83 void welcomeAction(DeviceState* d, PGM_P* s, int scrSize){
84 char key = getKey();
86 if((*d).stateChanged){
87 (*d).menuOffset = 0;
88 drawScreen(s,2,(*d).menuOffset,scrSize);
89 (*d).stateChanged = 0;
90 } else {
91 if(key != 0xff){
92 (*d).stateId = 1;
93 (*d).stateChanged = 1;
94 (*d).menuOffset = 0;
99 void mainAction(DeviceState* d, PGM_P* s, int scrSize){
100 char key = getKey();
102 if((*d).stateChanged){
103 lcdClear(); lcdGotoXY(0,0);
104 (*d).menuOffset = 0;
105 drawScreen(s,2,(*d).menuOffset,scrSize);
106 (*d).stateChanged = 0;
107 } else {
108 if(key != 0xff){
109 switch (key) {
110 case '*': {
111 next(s,d,scrSize);
112 }; break;
113 case '#' : {
114 prev(s,d,scrSize);
115 }; break;
116 case '0' : {
117 if((*d).menuOffset == 1) {
118 (*d).stateChanged = 1;
119 (*d).stateId = 2;
121 }; break;
127 void startGame(DeviceState* d, PGM_P* s, int scrSize){
128 char key = getKey();
130 if((*d).stateChanged){
131 lcdClear(); lcdGotoXY(0,0);
132 (*d).menuOffset = 0;
133 drawScreen(s,2,(*d).menuOffset,scrSize);
134 (*d).stateChanged = 0;
135 } else {
136 switch (key) {
137 case '*': {
138 next(s,d,scrSize);
139 }; break;
140 case '#' : {
141 prev(s,d,scrSize);
142 }; break;
147 void delay_us(unsigned short time_us){
148 unsigned short delay_loops;
149 register unsigned short i;
151 delay_loops = (time_us+3)/5*CYCLES_PER_US; // +3 for rounding up (dirty)
152 // one loop takes 5 cpu cycles
153 for (i=0; i < delay_loops; i++) {};
156 void generateCodes(DeviceState* d){
157 for(int i=0; i<5; i++){(*d).aCodes[i] = getUniqueRandom(d);}
158 for(int i=0; i<5; i++){(*d).dCodes[i] = getUniqueRandom(d);}
161 long getUniqueRandom(DeviceState* d){
162 int run = 1;
163 long rnd = 0;
165 while(run){
166 run = 0;
167 rnd = random()%100000;
168 for(int i=0; i<5; i++){ if(rnd == (*d).aCodes[i]){run = 1;}}
169 for(int i=0; i<5; i++){ if(rnd == (*d).dCodes[i]){run = 1;}}
171 return rnd;