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
= "by OP-Force\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:\n";
23 PGM_P welcomeScreen
[] PROGMEM
= {s000
,s001
};
24 #define welcomeScreenSize (sizeof(welcomeScreen)/sizeof(&welcomeScreen))
25 PGM_P mainScreen
[] PROGMEM
= {s002
,s003
,s004
};
26 #define mainScreenSize (sizeof(mainScreen)/sizeof(&mainScreen))
27 PGM_P codeScreen
[] PROGMEM
= {s005
};
28 #define codeScreenSize (sizeof(codeScreen)/sizeof(&codeScreen))
31 PGM_P
* screens
[] PROGMEM
= {welcomeScreen
,mainScreen
,codeScreen
};
32 int screensSizes
[] = {welcomeScreenSize
,mainScreenSize
,codeScreenSize
};
34 void welcomeAction(DeviceState
*, PGM_P
*, int scrSize
);
35 void mainAction(DeviceState
*, PGM_P
*, int scrSize
);
36 void startGame(DeviceState
*, PGM_P
*, int scrSize
);
37 void setupDevice(DeviceState
*, PGM_P
*, int scrSize
);
38 void showCodes(DeviceState
*, PGM_P
*, int scrSize
);
40 void delay_us(unsigned short time_us
);
41 void generateCodes(DeviceState
*);
42 long getUniqueRandom(DeviceState
* d
);
45 // LCD init and printf to LCD
48 // set initial device state and menu
49 DeviceState
* device
= malloc(sizeof(DeviceState
));
51 (*device
).stateChanged
= 1;
52 (*device
).screenChanged
= 0;
53 (*device
).stateId
= 0;
54 (*device
).menuOffset
= 0;
57 StateAction
* actions
= malloc(sizeof(StateAction
)*10);
58 actions
[0].action
= welcomeAction
;
59 actions
[1].action
= mainAction
;
60 actions
[3].action
= startGame
;
64 actions
[(*device
).stateId
].action(device
,screens
[(*device
).stateId
],screensSizes
[(*device
).stateId
]);
70 void welcomeAction(DeviceState
* d
, PGM_P
* s
, int scrSize
){
73 if((*d
).stateChanged
){
75 drawScreen(s
,2,(*d
).menuOffset
,scrSize
);
76 (*d
).stateChanged
= 0;
80 (*d
).stateChanged
= 1;
86 void mainAction(DeviceState
* d
, PGM_P
* s
, int scrSize
){
89 if((*d
).stateChanged
){
90 lcdClear(); lcdGotoXY(0,0);
92 drawScreen(s
,2,(*d
).menuOffset
,scrSize
);
93 (*d
).stateChanged
= 0;
104 if((*d
).menuOffset
== 1) {
105 (*d
).stateChanged
= 1;
114 void startGame(DeviceState
* d
, PGM_P
* s
, int scrSize
){
117 if((*d
).stateChanged
){
118 lcdClear(); lcdGotoXY(0,0);
120 drawScreen(s
,2,(*d
).menuOffset
,scrSize
);
121 (*d
).stateChanged
= 0;
134 void delay_us(unsigned short time_us
){
135 unsigned short delay_loops
;
136 register unsigned short i
;
138 delay_loops
= (time_us
+3)/5*CYCLES_PER_US
; // +3 for rounding up (dirty)
139 // one loop takes 5 cpu cycles
140 for (i
=0; i
< delay_loops
; i
++) {};
143 void generateCodes(DeviceState
* d
){
144 for(int i
=0; i
<5; i
++){(*d
).aCodes
[i
] = getUniqueRandom(d
);}
145 for(int i
=0; i
<5; i
++){(*d
).dCodes
[i
] = getUniqueRandom(d
);}
148 long getUniqueRandom(DeviceState
* d
){
154 rnd
= random()%100000;
155 for(int i
=0; i
<5; i
++){ if(rnd
== (*d
).aCodes
[i
]){run
= 1;}}
156 for(int i
=0; i
<5; i
++){ if(rnd
== (*d
).dCodes
[i
]){run
= 1;}}