-
Notifications
You must be signed in to change notification settings - Fork 2
/
cmd.c
700 lines (632 loc) · 14.9 KB
/
cmd.c
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
/*
* cmd.c
*
* Created on: Apr 15, 2013
* Author: popai
*/
#include <avr/pgmspace.h>
#include <avr/eeprom.h>
#include <util/delay.h>
#include <string.h>
#include <stdio.h>
#include <math.h>
#include "cmd.h"
#include "pinDef.h"
#include "lib/fbus/fbus.h"
#include "lib/usart/usart.h"
#include "lib/adc/adc.h"
static float Thermistor(int RawADC);
static void StareOUT(char *smsc_nr, char *nrtel);
static void StareTMP(char *smsc_nr, char *nrtel);
static void SetPort();
//"Flash store comands etc are strings to store
const prog_char IN1[] PROGMEM = "IN1"; //adresa 18*2
const prog_char IN2[] PROGMEM = "IN2"; //adresa 18*3
const prog_char IN3[] PROGMEM = "IN3"; //adresa 18*4
const prog_char IN4[] PROGMEM = "IN4"; //adresa 18*5
const prog_char OUT1H[] PROGMEM = "OUT1L"; //adresa 18*6
const prog_char OUT1L[] PROGMEM = "OUT1H"; //adresa 18*7
const prog_char OUT2H[] PROGMEM = "OUT2L"; //adresa 18*8
const prog_char OUT2L[] PROGMEM = "OUT2H"; //adresa 18*9
const prog_char OUT3H[] PROGMEM = "OUT3L"; //adresa 18*10
const prog_char OUT3L[] PROGMEM = "OUT3H"; //adresa 18*11
const prog_char OUT4H[] PROGMEM = "OUT4L"; //adresa 18*12
const prog_char OUT4L[] PROGMEM = "OUT4H"; //adresa 18*13
const prog_char OUT5H[] PROGMEM = "OUT5L"; //adresa 18*14
const prog_char OUT5L[] PROGMEM = "OUT5H"; //adresa 18*15
const prog_char TMP1[] PROGMEM = "TMP1"; //adresa 18*16
const prog_char TMP2[] PROGMEM = "TMP2"; //adresa 18*17
const prog_char TMP3[] PROGMEM = "TMP3"; //adresa 18*18
const prog_char LOGIN[] PROGMEM = "LOGIN"; //adresa 18*19
const prog_char SMSC[] PROGMEM = "SMSC"; //adresa 360
const prog_char STARE_OUT[]PROGMEM = "STARE OUT"; //adresa 18*21
const prog_char STARE_TMP[] PROGMEM = "STARE TMP"; //adresa 18*22
const prog_char STARE_ALL[]PROGMEM = "STARE ALL"; //adresa 18*23
const prog_char CMD_ERR[] PROGMEM = "Comanda ne scrisa";
// The table to refer to my strings.
PROGMEM const char *comenzi[] =
{ IN1, IN2, IN3, IN4, OUT1H, OUT1L, OUT2H, OUT2L, OUT3H, OUT3L, OUT4H, OUT4L,
OUT5H, OUT5L, TMP1, TMP2, TMP3, LOGIN, SMSC };
int8_t in1 = 1, in2 = 1, in3 = 1, in4 = 1;
/**
* @breaf initialize the pfon comunication
*
* @param no parametrs
*/
int8_t FON_INIT()
{
enum fbus_frametype ftype;
char buffer[24];
memset(buffer, 0, sizeof(buffer));
//write mesage in EEPPROM
/*
eeprom_read_block(buffer, (int*) 486, 18);
if (strlen(buffer) == 0)
{
strcpy_P(buffer, CMD_ERR);
eeprom_write_block(buffer, (int*) 486, 18);
//strcpy_P(buffer, 0x00);
}
*/
SetPort();
strcpy_P(buffer, CMD_ERR);
eeprom_update_block(buffer, (int*) 486, 18);
uint8_t init[] =
{ 0x00, 0x01, 0x64, 0x01, 0x01, 0x40 };
fbus_init();
sendframe(0x40, init, 6);
ftype = fbus_readack();
if (ftype == FRAME_UNKNOWN)
{
sendframe(0x40, init, 6);
ftype = fbus_readack();
if (ftype == FRAME_UNKNOWN)
{
sendframe(0x40, init, 6);
ftype = fbus_readack();
if (ftype == FRAME_UNKNOWN)
{
PORTB |= (1 << PB5); //eror
return 0;
}
}
}
//save smsc number if is not saved
char smsc_nr[18];
memset(smsc_nr, 0, sizeof(smsc_nr));
_delay_ms(1000);
fbus_getsmsc(smsc_nr);
/*
eeprom_read_block(buffer, (int*) 360, 18);
if ((strcmp(smsc_nr, buffer) != 0) && (strlen(smsc_nr) != 0))
eeprom_write_block(smsc_nr, (int*) 360, 18);
*/
if (strlen(smsc_nr) != 0)
eeprom_update_block(smsc_nr, (int*) 360, 18);
sendack(0x02, 0x04);
uint8_t init_sms[] =
{ 0x00, 0x01, 0x00, 0x07, 0x02, 0x01, 0x01, 0x64, 0x01, 0x47 };
sendframe(0x02, init_sms, 0x0a);
//fbus_readack();
sendack(0x14, 0x00);
//uart_sendsms("+40744946000", "+40745183841", smsc_nr);
return 1;
}
//write data on eeprom
int CfgCmd(char *inbuffer)
{
int adresa = 18;
//char *scriu;
char comanda[7];
for (int8_t i = 0; i < 19; ++i)
{
strcpy_P(comanda, (char*) pgm_read_word(&(comenzi[i]))); // Necessary casts and dereferencing, just copy.
if (strstr(inbuffer, comanda) != 0)
{
inbuffer += strlen(comanda) + 1;
adresa = 18 * (2 + i);
#if DEBUG
//Serial.print("Scriu la adresa ");
UWriteInt(adresa);
UWriteString(": ");
UWriteString(inbuffer);
UWriteData(0x0D);
UWriteData(0x0A);
#endif
eeprom_write_block(inbuffer, (int*) adresa, 18);
return adresa;
}
}
return 0;
}
//read data from eeprom on specified adres
void ReadEprom(char* str_citit, int const adresa)
{
//char str_citit[18];
eeprom_read_block(str_citit, (int*) adresa, 18);
/*
#if DEBUG
//Serial.print("citesc la adresa: ");
UWriteInt(adresa);
UWriteString(str_citit);
UWriteData(0x0D);
UWriteData(0x0A);
#endif
*/
}
void DellEprom()
{
for (int i = 0; i < 512; i++)
eeprom_write_byte((uint8_t*) i, 0);
}
/*
void DellPass()
{
for (int i = 18 * 19; i < 18 * 20; i++)
EEPROM.update(i, 0);
}
*/
/**
* @breaf set the port on startup
*
* @param none
*/
void SetPort()
{
int i;
int8_t byte;
for (i = 379; i < 384; i++)
{
byte = eeprom_read_byte((uint8_t*) i);
switch (i)
{
case 379:
if (byte)
PORTD |= (1 << PD2);
else
PORTD &= ~(1 << PD2);
break;
case 380:
if (byte)
PORTD |= (1 << PD3);
else
PORTD &= ~(1 << PD3);
break;
case 381:
if (byte)
PORTD |= (1 << PD4);
else
PORTD &= ~(1 << PD4);
break;
case 382:
if (byte)
PORTD |= (1 << PD5);
else
PORTD &= ~(1 << PD5);
break;
case 383:
if (byte)
PORTD |= (1 << PD6);
else
PORTD &= ~(1 << PD6);
break;
default: break;
}
}
}
/**
* @breaf execut sms commands
*
* @param nrtel: pfon number
* inmsg: sms command
*/
void Comand(char *nrtel, char *inmsg)
{
char buffer[18];
memset(buffer, 0, sizeof(buffer));
char OK[3] = "OK";
char smsc_num[18];
memset(smsc_num, 0, sizeof(smsc_num));
//bool cmdok = false;
ReadEprom(buffer, 18);
ReadEprom(smsc_num, 360);
if (strcmp(nrtel, buffer) == 0)
{
ReadEprom(buffer, 108); //18 * 6
if (strcasecmp(buffer, inmsg) == 0)
{
//digitalWrite(outD1, LOW);
PORTD &= ~(1 << PD2);
eeprom_update_byte((uint8_t*) 379, 0);
uart_sendsms(smsc_num, nrtel, OK);
return;
}
ReadEprom(buffer, 126); //18 * 7
if (strcasecmp(buffer, inmsg) == 0)
{
//digitalWrite(outD1, HIGH);
PORTD |= (1 << PD2);
eeprom_update_byte((uint8_t*) 379, 1);
uart_sendsms(smsc_num, nrtel, OK);
return;
}
ReadEprom(buffer, 144); //18 * 8
if (strcasecmp(buffer, inmsg) == 0)
{
//digitalWrite(outD2, LOW);
PORTD &= ~(1 << PD3);
eeprom_update_byte((uint8_t*) 380, 0);
uart_sendsms(smsc_num, nrtel, OK);
return;
}
ReadEprom(buffer, 162); //18 * 9
if (strcasecmp(buffer, inmsg) == 0)
{
//digitalWrite(outD2, HIGH);
PORTD |= (1 << PD3);
eeprom_update_byte((uint8_t*) 380, 1);
uart_sendsms(smsc_num, nrtel, OK);
return;
}
ReadEprom(buffer, 180); //18 * 10
if (strcasecmp(buffer, inmsg) == 0)
{
//digitalWrite(outD3, LOW);
PORTD &= ~(1 << PD4);
eeprom_update_byte((uint8_t*) 381, 0);
uart_sendsms(smsc_num, nrtel, OK);
return;
}
ReadEprom(buffer, 198); //18 * 11
if (strcasecmp(buffer, inmsg) == 0)
{
//digitalWrite(outD3, HIGH);
PORTD |= (1 << PD4);
eeprom_update_byte((uint8_t*) 381, 1);
uart_sendsms(smsc_num, nrtel, OK);
return;
}
ReadEprom(buffer, 216); //18 * 12
if (strcasecmp(buffer, inmsg) == 0)
{
//digitalWrite(outD4, LOW);
PORTD &= ~(1 << PD5);
eeprom_update_byte((uint8_t*) 382, 0);
uart_sendsms(smsc_num, nrtel, OK);
return;
}
ReadEprom(buffer, 234); //18 * 13
if (strcasecmp(buffer, inmsg) == 0)
{
//digitalWrite(outD4, HIGH);
PORTD |= (1 << PD5);
eeprom_update_byte((uint8_t*) 382, 1);
uart_sendsms(smsc_num, nrtel, OK);
return;
}
ReadEprom(buffer, 252); //18 * 14
if (strcasecmp(buffer, inmsg) == 0)
{
//digitalWrite(outD5, LOW);
PORTD &= ~(1 << PD6);
eeprom_update_byte((uint8_t*) 383, 0);
uart_sendsms(smsc_num, nrtel, OK);
return;
}
ReadEprom(buffer, 270); //18 * 15
if (strcasecmp(buffer, inmsg) == 0)
{
//digitalWrite(outD5, HIGH);
PORTD |= (1 << PD6);
eeprom_update_byte((uint8_t*) 383, 1);
uart_sendsms(smsc_num, nrtel, OK);
return;
}
//strcpy_P(buffer, (char*) pgm_read_word(&(comenzi[18])));
//if (strcasecmp(buffer, inmsg) == 0)
if (strcasecmp_P(inmsg, STARE_OUT) == 0)
{
StareOUT(smsc_num, nrtel);
return;
}
//strcpy_P(buffer, (char*) pgm_read_word(&(comenzi[19])));
//if (strcasecmp(buffer, inmsg) == 0)
if (strcasecmp_P(inmsg, STARE_TMP) == 0)
{
StareTMP(smsc_num, nrtel);
return;
}
//strcpy_P(buffer, (char*) pgm_read_word(&(comenzi[20])));
//if (strcasecmp(buffer, inmsg) == 0)
if (strcasecmp_P(inmsg, STARE_ALL) == 0)
{
StareTMP(smsc_num, nrtel);
StareOUT(smsc_num, nrtel);
return;
}
ReadEprom(buffer, 486);
uart_sendsms(smsc_num, nrtel, buffer);
return;
}
else
{
ReadEprom(buffer, 342); //18 * 19
if (strcmp(buffer, inmsg) == 0)
{
eeprom_write_block(nrtel, (int*) 18, 18);
strcpy_P(buffer, LOGIN);
uart_sendsms(smsc_num, nrtel, buffer);
}
}
}
//write the sms string for commands
void Config(char *nrtel, char *inmsg)
{
char buffer[18];
memset(buffer, 0, sizeof(buffer));
int adr = 18;
if ((strlen(nrtel) != 0) && (strlen(inmsg) != 0))
{
//strcpy_P(buffer, (char*) pgm_read_word(&(comenzi[17])));
if (strstr_P(inmsg, LOGIN) != 0)
{
eeprom_write_block(nrtel, (int*) adr, 18);
CfgCmd(inmsg);
}
else
{
ReadEprom(buffer, 18);
if (strcmp(nrtel, buffer) == 0)
CfgCmd(inmsg);
}
}
}
static void StareOUT(char *smsc_nr, char *nrtel)
{
char mesage1[60];
char mesage2[60];
char buffer[18];
memset(mesage1, 0, sizeof(mesage1));
memset(mesage2, 0, sizeof(mesage2));
memset(buffer, 0, sizeof(buffer));
//int i = 108;
//if (digitalRead(outD1) == LOW)
if ((PIND & (1 << PD2)) == 0)
{
ReadEprom(buffer, 108);
if (strlen(buffer) != 0)
{
strcat(mesage1, buffer);
strcat(mesage1, "\n\r");
}
//i += 18;
}
else
{
ReadEprom(buffer, 126);
if (strlen(buffer) != 0)
{
strcat(mesage1, buffer);
strcat(mesage1, "\n\r");
}
}
//if (digitalRead(outD2) == LOW)
if ((PIND & (1 << PD3)) == 0)
{
ReadEprom(buffer, 144);
if (strlen(buffer) != 0)
{
strcat(mesage1, buffer);
strcat(mesage1, "\n\r");
}
}
else
{
ReadEprom(buffer, 162);
if (strlen(buffer) != 0)
{
strcat(mesage1, buffer);
strcat(mesage1, "\n\r");
}
}
//if (digitalRead(outD3) == LOW)
if ((PIND & (1 << PD4)) == 0)
{
ReadEprom(buffer, 180);
if (strlen(buffer) != 0)
{
strcat(mesage1, buffer);
strcat(mesage1, "\n\r");
}
}
else
{
ReadEprom(buffer, 198);
if (strlen(buffer) != 0)
{
strcat(mesage1, buffer);
strcat(mesage1, "\n\r");
}
}
//if (digitalRead(outD4) == LOW)
if ((PIND & (1 << PD5)) == 0)
{
ReadEprom(buffer, 216);
if (strlen(buffer) != 0)
{
strcat(mesage2, buffer);
strcat(mesage2, "\n\r");
}
}
else
{
ReadEprom(buffer, 234);
if (strlen(buffer) != 0)
{
strcat(mesage2, buffer);
strcat(mesage2, "\n\r");
}
}
//if (digitalRead(outD5) == LOW)
if ((PIND & (1 << PD6)) == 0)
{
ReadEprom(buffer, 252);
if (strlen(buffer) != 0)
{
strcat(mesage2, buffer);
strcat(mesage2, "\n\r");
}
}
else
{
ReadEprom(buffer, 270);
if (strlen(buffer) != 0)
{
strcat(mesage2, buffer);
//strcat(mesage, "\n");
}
}
if (strlen(mesage1) != 0)
uart_sendsms(smsc_nr, nrtel, mesage1);
if (strlen(mesage2) != 0)
uart_sendsms(smsc_nr, nrtel, mesage2);
}
/**
* @breaf calculate the temperature in grads Celsius for a 10K termistor
*
* @param Tpin analog input pin wher temp is read
*/
static float Thermistor(int Tpin)
{
float Vo;
float logRt, Rt, T;
float R = 9870; // fixed resistance, measured with multimeter
// c1, c2, c3 are calibration coefficients for a particular thermistor
float c1 = 1.009249522e-03, c2 = 2.378405444e-04, c3 = 2.019202697e-07;
//Vo = analogRead(Tpin);
Vo = ReadADC(Tpin);
Rt = R * (1024.0 / Vo - 1.0);
logRt = log(Rt);
T = (1.0 / (c1 + c2 * logRt + c3 * logRt * logRt * logRt)) - 273.15;
return T;
}
/**
* @breaf read the temperature on analog pins (10K termistor) and send sms with
* teaded temperature
*
* @param smsc_nr: sms center number
* nrtel: number hu recive sms
*/
static void StareTMP(char *smsc_nr, char *nrtel)
{
char mesage[64];
char buffer[18];
char tmpe[32];
memset(mesage, 0, sizeof(mesage));
memset(buffer, 0, sizeof(buffer));
memset(tmpe, 0, sizeof(tmpe));
int tmp, tmp1, tmp2;
tmp = Thermistor(PC0);
_delay_ms(30);
tmp1 = Thermistor(PC0);
_delay_ms(30);
tmp2 = Thermistor(PC0);
tmp = (tmp + tmp1 + tmp2) / 3;
ReadEprom(buffer, 288); //18 * 16
if (strlen(buffer) != 0)
{
sprintf(tmpe, "%s: %d %s", buffer, tmp, "C\r\n");
strcat(mesage, tmpe);
}
tmp = Thermistor(PC1);
_delay_ms(30);
tmp1 = Thermistor(PC1);
_delay_ms(30);
tmp2 = Thermistor(PC1);
tmp = (tmp + tmp1 + tmp2) / 3;
ReadEprom(buffer, 306); //18 * 17
if (strlen(buffer) != 0)
{
sprintf(tmpe, "%s: %d %s", buffer, tmp, "C\r\n");
strcat(mesage, tmpe);
}
tmp = Thermistor(PC2);
_delay_ms(30);
tmp1 = Thermistor(PC2);
_delay_ms(30);
tmp2 = Thermistor(PC2);
tmp = (tmp + tmp1 + tmp2) / 3;
ReadEprom(buffer, 324); //18 * 18
if (strlen(buffer) != 0)
{
sprintf(tmpe, "%s: %d %c", buffer, tmp, 'C');
strcat(mesage, tmpe);
}
if (strlen(mesage) != 0)
uart_sendsms(smsc_nr, nrtel, mesage);
}
void VerificIN()
{
char smsc_nr[18];
char nrtel[18];
char buffer[18];
memset(nrtel, 0, sizeof(nrtel));
memset(buffer, 0, sizeof(buffer));
memset(smsc_nr, 0, sizeof(smsc_nr));
if ((PINB & (1 << PB0)) == 0)
{
if (in1)
{
ReadEprom(smsc_nr, 360);
ReadEprom(nrtel, 18);
in1 = 0;
ReadEprom(buffer, 36); //18*2
if (strlen(buffer) != 0)
uart_sendsms(smsc_nr, nrtel, buffer);
}
}
else
in1 = 1;
if ((PINB & (1 << PB1)) == 0)
{
if (in2)
{
ReadEprom(smsc_nr, 360);
ReadEprom(nrtel, 18);
in2 = 0;
ReadEprom(buffer, 54); //18*3
if (strlen(buffer) != 0)
uart_sendsms(smsc_nr, nrtel, buffer);
}
}
else
in2 = 1;
if ((PINB & (1 << PB2)) == 0)
{
if (in3)
{
ReadEprom(smsc_nr, 360);
ReadEprom(nrtel, 18);
in3 = 0;
ReadEprom(buffer, 72); //18 * 4
if (strlen(buffer) != 0)
uart_sendsms(smsc_nr, nrtel, buffer);
}
}
else
in3 = 1;
if ((PINB & (1 << PB3)) == 0)
{
if (in4)
{
ReadEprom(smsc_nr, 360);
ReadEprom(nrtel, 18);
in4 = 0;
ReadEprom(buffer, 90); //18*5
if (strlen(buffer) != 0)
uart_sendsms(smsc_nr, nrtel, buffer);
}
}
else
in4 = 1;
}