initial commit
[asdevice.git] / asdevice / device.c
blob8613c755ebe7456eb0c37a62afa12caf1802d0c7
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 = "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";
22 //set screens
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);
44 int main(void){
45 // LCD init and printf to LCD
46 lcdInit();
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;
56 //set function array
57 StateAction* actions = malloc(sizeof(StateAction)*10);
58 actions[0].action = welcomeAction;
59 actions[1].action = mainAction;
60 actions[3].action = startGame;
63 while(1){
64 actions[(*device).stateId].action(device,screens[(*device).stateId],screensSizes[(*device).stateId]);
67 return 0;
70 void welcomeAction(DeviceState* d, PGM_P* s, int scrSize){
71 char key = getKey();
73 if((*d).stateChanged){
74 (*d).menuOffset = 0;
75 drawScreen(s,2,(*d).menuOffset,scrSize);
76 (*d).stateChanged = 0;
77 } else {
78 if(key != 0xff){
79 (*d).stateId = 1;
80 (*d).stateChanged = 1;
81 (*d).menuOffset = 0;
86 void mainAction(DeviceState* d, PGM_P* s, int scrSize){
87 char key = getKey();
89 if((*d).stateChanged){
90 lcdClear(); lcdGotoXY(0,0);
91 (*d).menuOffset = 0;
92 drawScreen(s,2,(*d).menuOffset,scrSize);
93 (*d).stateChanged = 0;
94 } else {
95 if(key != 0xff){
96 switch (key) {
97 case '*': {
98 next(s,d,scrSize);
99 }; break;
100 case '#' : {
101 prev(s,d,scrSize);
102 }; break;
103 case '0' : {
104 if((*d).menuOffset == 1) {
105 (*d).stateChanged = 1;
106 (*d).stateId = 2;
108 }; break;
114 void startGame(DeviceState* d, PGM_P* s, int scrSize){
115 char key = getKey();
117 if((*d).stateChanged){
118 lcdClear(); lcdGotoXY(0,0);
119 (*d).menuOffset = 0;
120 drawScreen(s,2,(*d).menuOffset,scrSize);
121 (*d).stateChanged = 0;
122 } else {
123 switch (key) {
124 case '*': {
125 next(s,d,scrSize);
126 }; break;
127 case '#' : {
128 prev(s,d,scrSize);
129 }; break;
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){
149 int run = 1;
150 long rnd = 0;
152 while(run){
153 run = 0;
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;}}
158 return rnd;