2 ************************************************************************************************
3 ** Program pre ariadenie emulujuce nastrazny system pozostavajuce z :
8 ** Umoznuje hrat scenar pre 2 timy, kazdy z timov sa snazi o zapnutie resp. vypnutie zariadenia.
9 ************************************************************************************************
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";
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
);
58 // LCD init and printf to LCD
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;
70 StateAction
* actions
= malloc(sizeof(StateAction
)*10);
71 actions
[0].action
= welcomeAction
;
72 actions
[1].action
= mainAction
;
73 actions
[3].action
= startGame
;
77 actions
[(*device
).stateId
].action(device
,screens
[(*device
).stateId
],screensSizes
[(*device
).stateId
]);
83 void welcomeAction(DeviceState
* d
, PGM_P
* s
, int scrSize
){
86 if((*d
).stateChanged
){
88 drawScreen(s
,2,(*d
).menuOffset
,scrSize
);
89 (*d
).stateChanged
= 0;
93 (*d
).stateChanged
= 1;
99 void mainAction(DeviceState
* d
, PGM_P
* s
, int scrSize
){
102 if((*d
).stateChanged
){
103 lcdClear(); lcdGotoXY(0,0);
105 drawScreen(s
,2,(*d
).menuOffset
,scrSize
);
106 (*d
).stateChanged
= 0;
117 if((*d
).menuOffset
== 1) {
118 (*d
).stateChanged
= 1;
127 void startGame(DeviceState
* d
, PGM_P
* s
, int scrSize
){
130 if((*d
).stateChanged
){
131 lcdClear(); lcdGotoXY(0,0);
133 drawScreen(s
,2,(*d
).menuOffset
,scrSize
);
134 (*d
).stateChanged
= 0;
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
){
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;}}