-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathweather_Oregon_12864.ino
2510 lines (2264 loc) · 82.7 KB
/
weather_Oregon_12864.ino
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
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// The Weather Station@arduinoMEGA2560
#include <DHT.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP085_U.h>
#include <U8g2lib.h>
#include <SPI.h>
#include <SdFat.h>
#include <Time.h>
#include <TimeLib.h>
#include <DS3232RTC.h>
#include <EEPROM.h>
#include <math.h>
#include <sunMoon.h>
#include <WlessOregonV2.h>
#define DHT_PIN 4
#define DHTTYPE DHT22
#define TINY_FONT u8g2_font_profont10_tf
#define RUS_FONT_S u8g2_font_crox1h_tf
#define RUS_FONT_L u8g2_font_crox3hb_tf
#define DIGIT_FONT u8g2_font_osb18_tn
// lcd 128x64 screen in software mode
#define EN_SCK_PIN 48
#define RW_MOSI_PIN 47
#define RS_CS_PIN 49
#define RST_PIN 8
#define LIGHT_SENSOR A0 // A0, Photoresistor
#define LCD_BLGHT_PIN 46
// Rotary encoder interface
const byte R_MAIN_PIN = 2; // Rotary Encoder main pin (right)
const byte R_SECD_PIN = 5; // Rotary Encoder second pin (left)
const byte R_BUTN_PIN = 3; // Rotary Encoder push button pin
#define SD_PIN 53 // SD card reader select pin
#define HB_PIN A2 // The pin for heartBeat reset
#define OUR_latitude 55.751244
#define OUR_longtitude 37.618423
#define OUR_timezone 180 // localtime with UTC difference in minutes
const uint16_t high_pressure = 767-8; // High pressure depends on altitude
const uint16_t low_pressure = 757-8; // Low pressure depends on altitude
const byte max_sensors = 5; // The maximum number of the sensors (remote + one internal) connected
const byte w433_INTR = 5; // The wireless 433 MHz sensor @ pin 18, interrupt #5
static const char* months = "\xDF\xed\xe2\xD4\xe5\xe2\xCC\xe0\xf0\xC0\xef\xf0\xCC\xe0\xe9\xC8\xfe\xed\xC8\xfe\xeb\xC0\xe2\xe3\xD1\xe5\xed\xCE\xea\xf2\xCD\xee\xff\xC4\xe5\xea";
static const char* wday = "\xC2\xf1\xCF\xed\xC2\xf2\xD1\xf0\xD7\xf2\xCF\xf2\xD1\xe1";
void printDate(time_t date) {
char buff[20];
sprintf(buff, "%2d-%02d-%4d %02d:%02d:%02d",
day(date), month(date), year(date), hour(date), minute(date), second(date));
Serial.print(buff);
}
//------------------------------------------ Configuration data ------------------------------------------------
/* Config record in the EEPROM has the following format:
uint32_t ID each time increment by 1
struct cfg config data
byte CRC the checksum
*/
struct cfg {
byte ext_sensor_ID; // external sensor ID (0 - random)
byte backlight_morning; // morning time in 10-minute intervals
byte backlight_evening; // evening time in 10-minute intervals
};
class CONFIG {
public:
CONFIG() {
can_write = is_valid = false;
buffRecords = 0;
rAddr = wAddr = 0;
eLength = 0;
nextRecID = 0;
byte rs = sizeof(struct cfg) + 5; // The total config record size
// Select appropriate record size; The record size should be power of 2, i.e. 8, 16, 32, 64, ... bytes
for (record_size = 8; record_size < rs; record_size <<= 1);
}
void init();
bool load(void);
bool isValid(void) { return is_valid; }
void getConfig(struct cfg &Cfg); // Copy config structure from this class
void updateConfig(struct cfg &Cfg); // Copy updated config into this class
bool saveConfig(struct cfg &Cfg); // write updated config into the EEPROM
private:
void defaultConfig(void);
struct cfg Config;
bool readRecord(uint16_t addr, uint32_t &recID);
bool save(void);
bool can_write; // The flag indicates that data can be saved
bool is_valid; // Whether tha data was loaded
byte buffRecords; // Number of the records in the outpt buffer
uint16_t rAddr; // Address of thecorrect record in EEPROM to be read
uint16_t wAddr; // Address in the EEPROM to start write new record
uint16_t eLength; // Length of the EEPROM, depends on arduino model
uint32_t nextRecID; // next record ID
byte record_size; // The size of one record in bytes
};
// Read the records until the last one, point wAddr (write address) after the last record
void CONFIG::init(void) {
defaultConfig();
eLength = EEPROM.length();
byte t, p ,h;
uint32_t recID;
uint32_t minRecID = 0xffffffff;
uint16_t minRecAddr = 0;
uint32_t maxRecID = 0;
uint16_t maxRecAddr = 0;
byte records = 0;
nextRecID = 0;
// read all the records in the EEPROM find min and max record ID
for (uint16_t addr = 0; addr < eLength; addr += record_size) {
if (readRecord(addr, recID)) {
++records;
if (minRecID > recID) {
minRecID = recID;
minRecAddr = addr;
}
if (maxRecID < recID) {
maxRecID = recID;
maxRecAddr = addr;
}
} else {
break;
}
}
if (records == 0) {
wAddr = rAddr = 0;
can_write = true;
return;
}
rAddr = maxRecAddr;
if (records < (eLength / record_size)) { // The EEPROM is not full
wAddr = rAddr + record_size;
if (wAddr > eLength) wAddr = 0;
} else {
wAddr = minRecAddr;
}
can_write = true;
}
void CONFIG::getConfig(struct cfg &Cfg) {
memcpy(&Cfg, &Config, sizeof(struct cfg));
}
void CONFIG::updateConfig(struct cfg &Cfg) {
memcpy(&Config, &Cfg, sizeof(struct cfg));
}
bool CONFIG::saveConfig(struct cfg &Cfg) {
updateConfig(Cfg);
return save(); // Save new data into the EEPROM
}
bool CONFIG::save(void) {
if (!can_write) return can_write;
if (nextRecID == 0) nextRecID = 1;
uint16_t startWrite = wAddr;
uint32_t nxt = nextRecID;
byte summ = 0;
for (byte i = 0; i < 4; ++i) {
EEPROM.write(startWrite++, nxt & 0xff);
summ <<=2; summ += nxt;
nxt >>= 8;
}
byte* p = (byte *)&Config;
for (byte i = 0; i < sizeof(struct cfg); ++i) {
summ <<= 2; summ += p[i];
EEPROM.write(startWrite++, p[i]);
}
summ ++; // To avoid empty records
EEPROM.write(wAddr+record_size-1, summ);
rAddr = wAddr;
wAddr += record_size;
if (wAddr > EEPROM.length()) wAddr = 0;
return true;
}
bool CONFIG::load(void) {
is_valid = readRecord(rAddr, nextRecID);
nextRecID ++;
if (!is_valid) defaultConfig();
return is_valid;
}
bool CONFIG::readRecord(uint16_t addr, uint32_t &recID) {
byte Buff[record_size];
for (byte i = 0; i < record_size; ++i)
Buff[i] = EEPROM.read(addr+i);
byte summ = 0;
for (byte i = 0; i < sizeof(struct cfg) + 4; ++i) {
summ <<= 2; summ += Buff[i];
}
summ ++; // To avoid empty fields
if (summ == Buff[record_size-1]) { // Checksumm is correct
uint32_t ts = 0;
for (char i = 3; i >= 0; --i) {
ts <<= 8;
ts |= Buff[i];
}
recID = ts;
byte i = 4;
memcpy(&Config, &Buff[4], sizeof(struct cfg));
return true;
}
return false;
}
void CONFIG::defaultConfig(void) {
Config.ext_sensor_ID = 0; // 0 means - any sensor
Config.backlight_morning = 48; // 8:00
Config.backlight_evening = 138; // 23:00
}
//------------------------------------------ Heart beat class (Watch dog timer based on ne555 IC) --------------
class HB {
public:
HB(byte hb) {
hb_pin = hb;
pinMode(hb_pin, INPUT);
period = 0; // auto reset is disabled
next_reset = 0;
}
void reset(void) {
pinMode(hb_pin, OUTPUT);
digitalWrite(hb_pin, LOW); // reset ne555 timer
delay(200);
pinMode(hb_pin, INPUT);
}
void setTimeout(uint16_t to = 0) { period = to; }
void autoReset(void) {
if (period == 0) { // Automatic reset is disabled, reset now
reset();
return;
}
if (millis() > next_reset) {
reset();
next_reset = millis() + (long)period * 1000;
}
}
private:
byte hb_pin; // ne555 reset pin (heart beat)
uint16_t period; // The period to reset the ne555 in seconds
uint32_t next_reset; // time in ms to automatically reset the ne555
};
//------------------------------------------ backlight of the LCD display (lite version) ----------------------
class BL {
public:
BL(byte sensorPIN, byte lightPIN, byte start_brightness = 128) {
sensor_pin = sensorPIN;
led_pin = lightPIN;
default_brightness = start_brightness;
b_night = 50;
daily_brightness = 150;
b_day = 500;
}
void init(void); // Initialize the data
void adjust(void); // Automatically adjust the brightness
int getSensorValue(void) { return analogRead(sensor_pin); }
void setBrightness(byte b);
void turnAuto(bool a);
void setLimits(uint16_t dark, uint16_t daylight, byte br_nightly, byte br_daily);
void setNightPeriod(byte Evening, byte Morning);
bool isDark(void); // Whether it is night time or it is dark here
private:
int empAverage(int v); // Exponential average value
byte sensor_pin; // Light sensor pin
byte led_pin; // Led PWM pin
uint32_t checkMS; // Time in ms when the sensor was checked
uint32_t ch_step; // The time in ms when the brighthess can be adjusted
bool automatic; // Whether the backlight should be adjusted automatically
bool use_local_time; // Whether to use local time to switch off the light nightly
byte brightness; // The backlight brightness
byte new_brightness; // The baclight brightness to set up
byte evening, morning; // The time of evening and morning (in 10-minutes interval)
long emp; // Exponential average value
byte default_brightness; // Default brightness of backlight
uint16_t b_night; // light sensor value of the night
uint16_t b_day; // light sensor value of the day light
byte daily_brightness; // The maximum brightness of backlight when light between b_night and b_day
byte nightly_brightness; // The brightness to use nightly
const byte emp_k = 8; // The exponential average coefficient
const uint16_t period = 200; // The period in ms to check the photeregister
const uint16_t ch_period = 5; // The period to adjust brightness
};
void BL::init(void) {
pinMode(led_pin, OUTPUT);
pinMode(sensor_pin, INPUT);
int light = analogRead(sensor_pin);
emp = 0;
brightness = new_brightness = default_brightness;
checkMS = ch_step = 0;
use_local_time = false;
automatic = true;
nightly_brightness = 50;
evening = morning = 0; // This value will be overwritten by config
adjust();
}
int BL::empAverage(int v) {
long nv = v *emp_k;
int round_v = emp_k >> 1;
emp += (nv - emp + round_v) / emp_k;
int r = (emp + round_v) / emp_k;
return r;
}
void BL::adjust(void) {
if (!automatic) return;
uint32_t ms = millis();
if ((ms > ch_step) && (new_brightness != brightness)) {
if (new_brightness > brightness) ++brightness; else --brightness;
analogWrite(led_pin, brightness);
ch_step = ms + ch_period;
}
if (ms < checkMS) return;
checkMS = ms + period;
// Turn off the backlight at night
if (isDark()) {
new_brightness = nightly_brightness;
return;
}
int light = analogRead(sensor_pin);
light = empAverage(light);
if (light < b_night) {
new_brightness = nightly_brightness;
return;
}
if (light > b_day) {
new_brightness = 0;
return;
}
new_brightness = map(light, b_night, b_day, nightly_brightness, daily_brightness);
}
void BL::setBrightness(byte b) {
brightness = b;
automatic = false;
analogWrite(led_pin, brightness);
}
void BL::turnAuto(bool a) {
automatic = a;
checkMS = 0;
if (a) adjust();
}
void BL::setLimits(uint16_t dark, uint16_t daylight, byte br_nightly, byte br_daily) {
b_night = dark;
b_day = daylight;
daily_brightness = br_daily;
nightly_brightness = br_nightly;
}
void BL::setNightPeriod(byte Evening, byte Morning) { // Time in 10-minute intervals from midnight
if (Evening <= Morning) return;
if (Evening > 144) return;
morning = Morning;
evening = Evening;
use_local_time = true;
}
bool BL::isDark(void) {
if (use_local_time) {
byte now_t = hour() * 6 + (minute() + 5) /10; // Time in 10-minute intervals from midnight
return ((now_t <= morning) || (now_t >= evening));
}
long light = 0;
for (byte i = 0; i < 4; ++i) {
light += analogRead(sensor_pin);
delay(20);
}
light >>= 2;
return (light < b_night);
}
//------------------------------------------ class BUTTON ------------------------------------------------------
class BUTTON {
public:
BUTTON(byte ButtonPIN, unsigned int timeout_ms = 3000) {
pt = tickTime = 0;
buttonPIN = ButtonPIN;
overPress = timeout_ms;
}
void init(void) { pinMode(buttonPIN, INPUT_PULLUP); }
void setTimeout(uint16_t timeout_ms = 3000) { overPress = timeout_ms; }
byte intButtonStatus(void) { byte m = mode; mode = 0; return m; }
void cnangeINTR(void);
byte buttonCheck(void);
bool buttonTick(void);
private:
volatile byte mode; // The button mode: 0 - not pressed, 1 - pressed, 2 - long pressed
uint16_t overPress; // Maxumum time in ms the button can be pressed
volatile uint32_t pt; // Time in ms when the button was pressed (press time)
uint32_t tickTime; // The time in ms when the button Tick was set
byte buttonPIN; // The pin number connected to the button
const uint16_t tickTimeout = 200; // Period of button tick, while tha button is pressed
const uint16_t shortPress = 900; // If the button was pressed less that this timeout, we assume the short button press
};
void BUTTON::cnangeINTR(void) { // Interrupt function, called when the button status changed
bool keyUp = digitalRead(buttonPIN);
unsigned long now_t = millis();
if (!keyUp) { // The button has been pressed
if ((pt == 0) || (now_t - pt > overPress)) pt = now_t;
} else {
if (pt > 0) {
if ((now_t - pt) < shortPress) mode = 1; // short press
else mode = 2; // long press
pt = 0;
}
}
}
byte BUTTON::buttonCheck(void) { // Check the button state, called each time in the main loop
mode = 0;
bool keyUp = digitalRead(buttonPIN); // Read the current state of the button
uint32_t now_t = millis();
if (!keyUp) { // The button is pressed
if ((pt == 0) || (now_t - pt > overPress)) pt = now_t;
} else {
if (pt == 0) return 0;
if ((now_t - pt) > shortPress) // Long press
mode = 2;
else
mode = 1;
pt = 0;
}
return mode;
}
bool BUTTON::buttonTick(void) { // When the button pressed for a while, generate periodical ticks
bool keyUp = digitalRead(buttonPIN); // Read the current state of the button
uint32_t now_t = millis();
if (!keyUp && (now_t - pt > shortPress)) { // The button have been pressed for a while
if (now_t - tickTime > tickTimeout) {
tickTime = now_t;
return (pt != 0);
}
} else {
if (pt == 0) return false;
tickTime = 0;
}
return false;
}
//------------------------------------------ class ENCODER ------------------------------------------------------
class ENCODER {
public:
ENCODER(byte aPIN, byte bPIN, int16_t initPos = 0) {
pt = 0; mPIN = aPIN; sPIN = bPIN; pos = initPos;
min_pos = -32767; max_pos = 32766; channelB = false; increment = 1;
changed = 0;
is_looped = false;
}
void init(void) {
pinMode(mPIN, INPUT_PULLUP);
pinMode(sPIN, INPUT_PULLUP);
}
void set_increment(byte inc) { increment = inc; }
byte get_increment(void) { return increment; }
int16_t read(void) { return pos; }
void reset(int16_t initPos, int16_t low, int16_t upp, byte inc = 1, byte fast_inc = 0, bool looped = false);
bool write(int16_t initPos);
void cnangeINTR(void);
private:
int32_t min_pos, max_pos;
volatile uint32_t pt; // Time in ms when the encoder was rotaded
volatile uint32_t changed; // Time in ms when the value was changed
volatile bool channelB;
volatile int16_t pos; // Encoder current position
byte mPIN, sPIN; // The pin numbers connected to the main channel and to the socondary channel
bool is_looped; // Whether the encoder is looped
byte increment; // The value to add or substract for each encoder tick
byte fast_increment; // The value to change encoder when in runs quickly
const uint16_t fast_timeout = 300; // Time in ms to change encodeq quickly
const uint16_t overPress = 1000;
};
bool ENCODER::write(int16_t initPos) {
if ((initPos >= min_pos) && (initPos <= max_pos)) {
pos = initPos;
return true;
}
return false;
}
void ENCODER::reset(int16_t initPos, int16_t low, int16_t upp, byte inc, byte fast_inc, bool looped) {
min_pos = low; max_pos = upp;
if (!write(initPos)) initPos = min_pos;
increment = fast_increment = inc;
if (fast_inc > increment) fast_increment = fast_inc;
is_looped = looped;
}
void ENCODER::cnangeINTR(void) { // Interrupt function, called when the channel A of encoder changed
bool rUp = digitalRead(mPIN);
unsigned long now_t = millis();
if (!rUp) { // The channel A has been "pressed"
if ((pt == 0) || (now_t - pt > overPress)) {
pt = now_t;
channelB = digitalRead(sPIN);
}
} else {
if (pt > 0) {
byte inc = increment;
if ((now_t - pt) < overPress) {
if ((now_t - changed) < fast_timeout) inc = fast_increment;
changed = now_t;
if (channelB) pos -= inc; else pos += inc;
if (pos > max_pos) {
if (is_looped)
pos = min_pos;
else
pos = max_pos;
}
if (pos < min_pos) {
if (is_looped)
pos = max_pos;
else
pos = min_pos;
}
}
pt = 0;
}
}
}
//------------------------------------------ class clockArm ------------------------------------------------------
class clockArm {
public:
clockArm(void) {}
void setHour(byte Hour, byte Minute);
void setMinute(byte Minute);
char vect(char X, char ti);
byte armX(byte cx, char thickIndex, bool HourArm) {
char dy = vect(dY, thickIndex);
if (HourArm) {
char dx = dX;
dx -= dx>>2;
return (char)cx + dx - dy;
} else {
return (char)cx + dX - dy;
}
}
byte armY(byte cy, char thickIndex, bool HourArm) {
char dx = vect(dX, thickIndex);
if (HourArm) {
char dy = dY;
dy -= dy>>2;
return (char)cy + dy + dx;
} else {
return (char)cy + dY + dx;
}
}
byte tailX(byte cx, char thickIndex) {
char dy = vect(dY, thickIndex);
char dx = dX;
dx /= 5;
return (char)cx - dx - dy;
}
byte tailY(byte cy, char thickIndex) {
char dx = vect(dX, thickIndex);
char dy = dY;
dy /= 5;
return (char)cy - dy + dx;
}
byte deltaX(void) { return dX; }
byte deltaY(void) { return dY; }
private:
byte quadrant;
char dX, dY; // Coordinates of Minute or Hour arms
const byte cArm[16][2] = {
{0, 20}, {2, 20}, {4, 19}, {6, 19}, {8, 18}, {10, 17}, {12, 16},
{13, 15}, {15, 14}, {16, 12}, {17, 10}, {18, 8}, {19, 6}, {19, 4}, {20, 2}, {20, 0}
};
};
char clockArm::vect(char X, char ti) {
char x = 0;
if (X < -14) x = -1;
if (X > 14) x = 1;
return x*ti;
}
void clockArm::setHour(byte Hour, byte Minute) {
Hour %= 12;
setMinute(Hour * 5 + Minute / 12);
}
void clockArm::setMinute(byte Minute) {
byte angle;
quadrant = Minute / 15;
angle = Minute % 15;
switch (quadrant) {
case 0:
dX = cArm[angle][0];
dY = cArm[angle][1];
dY *= -1;
break;
case 1:
dX = cArm[15 - angle][0];
dY = cArm[15 - angle][1];
break;
case 2:
dX = cArm[angle][0];
dX *= -1;
dY = cArm[angle][1];
break;
case 3:
dX = cArm[15 - angle][0];
dX *= -1;
dY = cArm[15 - angle][1];
dY *= -1;
break;
}
}
//------------------------------------------ class HISTORY ----------------------------------------------------
#define H_LENGTH 30
class HISTORY {
public:
HISTORY(void) { len = 0; }
void init(void) { len = 0; }
void put(int item);
bool isFull(void) { return len == H_LENGTH; }
int last(void) { return queue[len-1]; }
int top(void) { return queue[0]; }
int average(void);
float dispersion(void);
float gradient(byte last_n = H_LENGTH);
private:
int queue[H_LENGTH];
byte len;
};
void HISTORY::put(int item) {
if (len < H_LENGTH) {
queue[len++] = item;
} else {
for (byte i = 0; i < len-1; ++i) queue[i] = queue[i+1];
queue[H_LENGTH-1] = item;
}
}
int HISTORY::average(void) {
long sum = 0;
if (len == 0) return 0;
if (len == 1) return queue[0];
for (byte i = 0; i < len; ++i) sum += queue[i];
sum += len >> 1; // round the average
sum /= len;
return (int)sum;
}
float HISTORY::dispersion(void) {
if (len < 3) return 1000;
long sum = 0;
long avg = average();
for (byte i = 0; i < len; ++i) {
long q = queue[i];
q -= avg;
q *= q;
sum += q;
}
sum += len << 1;
float d = (float)sum / (float)len;
return d;
}
/* approfimating the last elements of the history with the line (y = Ax+B) using method of minimum squares.
* The gradient is parameter A
*/
float HISTORY::gradient(byte last_n) {
if (last_n > H_LENGTH) last_n = H_LENGTH;
if (len < 2) return 0;
long sx, sx_sq, sxy, sy;
sx = sx_sq = sxy = sy = 0;
byte si = 1;
byte a_len = len;
if (len > last_n) {
si = len - last_n + 1;
a_len = last_n;
}
for (byte i = si; i <= len; ++i) {
sx += i;
sx_sq += i*i;
sxy += i*queue[i-1];
sy += queue[i-1];
}
long numerator = a_len * sxy - sx * sy;
long denominator = a_len * sx_sq - sx * sx;
float a = (float)numerator / (float)denominator;
return a;
}
//------------------------------------------ class lcd DSPLay for weayther station ------------------------------------
class DSPL : public U8G2_ST7920_128X64_F_SW_SPI {
public:
DSPL(byte sck, byte rw, byte rs, byte rst) : U8G2_ST7920_128X64_F_SW_SPI(U8G2_R2, sck, rw, rs, rst) {
}
void showForecast(byte f); // Show the bitmap for the weather forecast
void showPressure(int pressure); // Show the symbol pressure + pressure value
void showTempSign(byte x, byte y); // Show the temperature symbol
void showHummSign(byte x, byte y); // Show the humidity symbol
void showPressureSign(byte x, byte y); // Show the humidity symbol
void showCentigrade(byte x, byte y); // Show the sentigrade sign in the position (x; y)
void showPercent(byte x, byte y); // Show the percentage sign in the position (x; y)
void showBattery(byte x, byte y); // Show the empty battery sign in the position (x; y)
void showVarrow(byte x, byte y); // Show the vertical arrow
void showClockSetup(byte x, byte y); // Show the icon indicating the clock setup process
void showMenuMark(byte x, byte y); // Show the menu mark
enum fcast {SUNNY = 0, CLOUDLY = 1, RAIN = 2, MOONY = 3, SNOW = 4};
private:
int myLrint(float a) { return int(a+0.5); }
const uint8_t bmPressure[16]= {
0b00011000,
0b00100100,
0b00100100,
0b01100100,
0b00100100,
0b00100100,
0b00100100,
0b01100100,
0b00100100,
0b00100100,
0b00100100,
0b11111111,
0b11111111,
0b11111111,
0b11111111,
0b01111110
};
const uint8_t bmCentigrade[8] = {
0b01100000,
0b10010000,
0b10010000,
0b01100000,
0b00000000,
0b00000000,
0b00000000,
0b00000000
};
const uint8_t bmPercent[8] = {
0b01100001,
0b10010010,
0b10010100,
0b01101000,
0b00010110,
0b00101001,
0b01001001,
0b10000110
};
const uint8_t bmTemperature[16] = {
0b00010000,
0b00101000,
0b01101000,
0b00101000,
0b01101000,
0b00111000,
0b01111000,
0b00111000,
0b01111000,
0b00111000,
0b01111100,
0b11111110,
0b11111110,
0b11111110,
0b01111100,
0b00111000
};
const uint8_t bmHumidity[16] = {
0b00000000,
0b00100000,
0b01100000,
0b11110000,
0b11110000,
0b01100010,
0b00000110,
0b00001111,
0b00001111,
0b00000110,
0b00000000,
0b00010000,
0b00110000,
0b01111000,
0b01111000,
0b00110000
};
const uint8_t bmVarrow[4] = {
0b01110000,
0b01110000,
0b11111000,
0b10101000
};
const uint8_t bmClock[8] = {
0b00111100,
0b01000110,
0b10001001,
0b10010001,
0b10011101,
0b10000001,
0b01000010,
0b00111100
};
const uint8_t bmMenuMark[5] = {
0b00010000,
0b00011000,
0b00111100,
0b00111100,
0b00011000
};
const uint8_t bmBattery[14] = {
0b01111111, 0b11111000,
0b10000000, 0b00000100,
0b10001000, 0b01000111,
0b10001000, 0b01000101,
0b10001000, 0b01000111,
0b10000000, 0b00000100,
0b01111111, 0b11111000,
};
const uint8_t bmSunny[64] = {
0b10000000, 0b01100001, 0b00000001, 0b00000100,
0b11000000, 0b00100001, 0b00000010, 0b00001000,
0b00110000, 0b00010001, 0b00000010, 0b00011000,
0b00001100, 0b00001000, 0b10000010, 0b00010000,
0b00000110, 0b00001100, 0b10110100, 0b00100000,
0b00000001, 0b10000100, 0b11111110, 0b01000000,
0b00000000, 0b01110011, 0b11111111, 0b10000000,
0b10000000, 0b00001111, 0b11111111, 0b11000011,
0b01111000, 0b00001111, 0b11111111, 0b11001100,
0b00000111, 0b11111111, 0b11111111, 0b11110000,
0b00000000, 0b00111111, 0b11111111, 0b11100000,
0b00000000, 0b00011111, 0b11111111, 0b11100000,
0b00000000, 0b01111111, 0b11111111, 0b11111000,
0b00000011, 0b11001111, 0b11111111, 0b11100111,
0b00111100, 0b00001111, 0b11111111, 0b11100000,
0b11000000, 0b00000111, 0b11111111, 0b11000000
};
const uint8_t bmCloudly[64] = {
0b00001000, 0b00000001, 0b00000000, 0b00001000,
0b00000100, 0b00000001, 0b10000000, 0b00010000,
0b00000011, 0b00000000, 0b10000000, 0b00100000,
0b00000001, 0b10000000, 0b10000000, 0b01000000,
0b00000000, 0b11000000, 0b10111000, 0b10000000,
0b00000000, 0b00100011, 0b11111111, 0b00000000,
0b11100000, 0b00011111, 0b11111110, 0b00000000,
0b00111000, 0b00011111, 0b10011110, 0b11000000,
0b00000110, 0b00111111, 0b01100001, 0b11011100,
0b00000001, 0b11111110, 0b01111111, 0b11101110,
0b00111100, 0b01111000, 0b11111111, 0b11101110,
0b01111110, 0b01101011, 0b11111111, 0b01110111,
0b11111111, 0b10000110, 0b11111111, 0b10101011,
0b11111111, 0b11111111, 0b11111111, 0b11011101,
0b11111111, 0b11001111, 0b11111111, 0b11101110,
0b11111111, 0b11111111, 0b11111111, 0b11101111
};
const uint8_t bmRain[64] = {
0b00000000, 0b00000000, 0b00010000, 0b00000000,
0b00000000, 0b00000000, 0b00110000, 0b00000000,
0b00000000, 0b00000000, 0b01111000, 0b01000000,
0b00000000, 0b00001000, 0b01111000, 0b11000000,
0b00010000, 0b00011000, 0b00110001, 0b11100000,
0b00110000, 0b00111100, 0b00000001, 0b11100000,
0b01111000, 0b00111100, 0b00000000, 0b11000000,
0b01111000, 0b00011000, 0b00000000, 0b00000000,
0b00110000, 0b00000000, 0b00000000, 0b00010000,
0b00000000, 0b00000000, 0b00000010, 0b00110000,
0b00000000, 0b00100000, 0b00000110, 0b01111000,
0b00100000, 0b01100000, 0b00001111, 0b01111000,
0b01100000, 0b11110000, 0b00001111, 0b00110000,
0b11110000, 0b11110000, 0b00000110, 0b00000000,
0b11110000, 0b01100000, 0b00000000, 0b00000000,
0b01100000, 0b00000000, 0b00000000, 0b00000000
};
const uint8_t bmSnow[64] = {
0b00000000, 0b00100100, 0b10000101, 0b00000000,
0b00001000, 0b00110101, 0b10011010, 0b11000000,
0b01001001, 0b00001010, 0b00010010, 0b01000000,
0b01101011, 0b00110101, 0b10000010, 0b00000000,
0b00010100, 0b00100100, 0b10000000, 0b00000000,
0b01101011, 0b00000100, 0b00000000, 0b01000000,
0b01001001, 0b00000000, 0b00000010, 0b01001000,
0b00001000, 0b00000000, 0b10000011, 0b01011000,
0b00000000, 0b00000100, 0b10010000, 0b10100000,
0b00000001, 0b00000110, 0b10110011, 0b01011000,
0b00001001, 0b00100001, 0b01000010, 0b01001000,
0b00001101, 0b01100110, 0b10110000, 0b01000000,
0b00000010, 0b10000100, 0b10010000, 0b00000000,
0b00001101, 0b01100000, 0b10000100, 0b00000000,
0b00001001, 0b00100000, 0b00100100, 0b10000000,
0b00000001, 0b00000000, 0b00110101, 0b10000000
};
const uint8_t bmMoony[64] = {
0b00000000, 0b10000000, 0b00000000, 0b00000000,
0b00000000, 0b10000000, 0b11111000, 0b00000000,
0b00000000, 0b10000011, 0b11110000, 0b00000000,
0b00000111, 0b01100011, 0b11100000, 0b00010000,
0b00000000, 0b10000111, 0b11100000, 0b00010000,
0b00000000, 0b10001111, 0b11000000, 0b00010000,
0b00000100, 0b00001111, 0b11000000, 0b11101100,
0b00000100, 0b00001111, 0b11000000, 0b00010000,
0b00000100, 0b00001111, 0b11000000, 0b00010000,
0b00111011, 0b00001111, 0b11000000, 0b00000000,
0b00000100, 0b00001111, 0b11100000, 0b00000100,
0b00000100, 0b00000111, 0b11100000, 0b00000100,
0b00000000, 0b00000011, 0b11110000, 0b00000100,
0b00000000, 0b00000000, 0b11111000, 0b00011011,
0b00000000, 0b00000000, 0b00000000, 0b00000100,
0b00000000, 0b00000000, 0b00000000, 0b00000100,
};
};
void DSPL::showForecast(byte f) {
const byte x = 95;
const byte y = 0;
const byte* bmp = bmSunny;
switch (f) {
case CLOUDLY:
bmp = bmCloudly;
break;
case RAIN:
bmp = bmRain;
break;
case MOONY:
bmp = bmMoony;
break;
case SNOW:
bmp = bmSnow;
break;
}
drawBitmap(x, y, 4, 16, bmp);
}
void DSPL::showPressure(int pressure) {
const byte x = 0;
const byte y = 0;
showPressureSign(x, y);
byte pr = 6;
if (pressure > high_pressure)
pr = 10;
else
if (pressure < low_pressure) pr = 3;
drawBox(x+3, y+13-pr, x+2, y+pr);
char buff[4];
sprintf(buff, "%3d", pressure);
setFont(RUS_FONT_S);
drawStr(x+10, y+16-2, buff);
}
void DSPL::showTempSign(byte x, byte y) {
drawBitmap(x, y, 1, 16, bmTemperature);
}
void DSPL::showHummSign(byte x, byte y) {
drawBitmap(x, y, 1, 16, bmHumidity);
}
void DSPL::showPressureSign(byte x, byte y) {