-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.ts
1717 lines (1548 loc) · 63.1 KB
/
main.ts
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
/*
Kitronik package for use with the Smart Greenhouse (www.kitronik.co.uk/5699)
This package pulls in other packages to deal with the lower level work for:
Bit banging the WS2182 protocol
Setting and reading a Real Time Clock chip
Reading from a BME280 Temperature, Humidity, Pressure sensor
*/
/**
* Well known colors for ZIP LEDs
*/
enum ZipLedColors {
//% block=red
Red = 0xFF0000,
//% block=orange
Orange = 0xFFA500,
//% block=yellow
Yellow = 0xFFFF00,
//% block=green
Green = 0x00FF00,
//% block=blue
Blue = 0x0000FF,
//% block=indigo
Indigo = 0x4b0082,
//% block=violet
Violet = 0x8a2be2,
//% block=purple
Purple = 0xFF00FF,
//% block=white
White = 0xFFFFFF,
//% block=black
Black = 0x000000
}
/**
* Different time options for the Real Time Clock
*/
enum TimeParameter {
//% block=hours
Hours,
//% block=minutes
Minutes,
//% block=seconds
Seconds
}
/**
* Different date options for the Real Time Clock
*/
enum DateParameter {
//% block=day
Day,
//% block=month
Month,
//% block=year
Year
}
//List of different temperature units
enum TemperatureUnitList {
//% block="°C"
C,
//% block="°F"
F
}
//List of different pressure units
enum PressureUnitList {
//% block="Pa"
Pa,
//% block="mBar"
mBar
}
/**
* Kitronik Smart Greenhouse MakeCode Package
*/
//% weight=100 color=#00A654 icon="\uf06c" block="Greenhouse"
//% groups='["Set Time", "Set Date", "Read Time", "Read Date", "Alarm", "General Inputs/Outputs", "High Power Outputs", "Servo", "Setup", "Entries", "Transfer", "Analyse"]'
namespace kitronik_smart_greenhouse {
////////////////////////////////
// MUSIC //
////////////////////////////////
/**
* Setup micro:bit to play music through on board buzzer
*/
//% blockId="kitronik_smart_greenhouse_buzzer_setup" block="set music pin for buzzer"
//% weight=100 blockGap=8
export function setBuzzerPin(): void {
pins.analogSetPitchPin(AnalogPin.P12)
}
////////////////////////////////
// ZIP LEDS //
////////////////////////////////
export class greenhouseZIPLEDs {
buf: Buffer;
pin: DigitalPin;
brightness: number;
start: number;
_length: number;
/**
* Shows a rainbow pattern on all LEDs.
* @param startHue the start hue value for the rainbow, eg: 1
* @param endHue the end hue value for the rainbow, eg: 360
*/
//% subcategory="ZIP LEDs"
//% blockId="kitronik_smart_greenhouse_rainbow" block="%zipLEDs|show rainbow from %startHue|to %endHue"
//% weight=94 blockGap=8
showRainbow(startHue: number = 1, endHue: number = 360) {
if (this._length <= 0) return;
startHue = startHue >> 0;
endHue = endHue >> 0;
const saturation = 100;
const luminance = 50;
const steps = this._length;
const direction = HueInterpolationDirection.Clockwise;
//hue
const h1 = startHue;
const h2 = endHue;
const hDistCW = ((h2 + 360) - h1) % 360;
const hStepCW = Math.idiv((hDistCW * 100), steps);
const hDistCCW = ((h1 + 360) - h2) % 360;
const hStepCCW = Math.idiv(-(hDistCCW * 100), steps);
let hStep: number;
if (direction === HueInterpolationDirection.Clockwise) {
hStep = hStepCW;
} else if (direction === HueInterpolationDirection.CounterClockwise) {
hStep = hStepCCW;
} else {
hStep = hDistCW < hDistCCW ? hStepCW : hStepCCW;
}
const h1_100 = h1 * 100; //we multiply by 100 so we keep more accurate results while doing interpolation
//sat
const s1 = saturation;
const s2 = saturation;
const sDist = s2 - s1;
const sStep = Math.idiv(sDist, steps);
const s1_100 = s1 * 100;
//lum
const l1 = luminance;
const l2 = luminance;
const lDist = l2 - l1;
const lStep = Math.idiv(lDist, steps);
const l1_100 = l1 * 100
//interpolate
if (steps === 1) {
this.setPixelRGB(0, hsl(h1 + hStep, s1 + sStep, l1 + lStep))
} else {
this.setPixelRGB(0, hsl(startHue, saturation, luminance));
for (let i = 1; i < steps - 1; i++) {
const h = Math.idiv((h1_100 + i * hStep), 100) + 360;
const s = Math.idiv((s1_100 + i * sStep), 100);
const l = Math.idiv((l1_100 + i * lStep), 100);
this.setPixelRGB(i, hsl(h, s, l));
}
this.setPixelRGB(steps - 1, hsl(endHue, saturation, luminance));
}
this.show();
}
/**
* Create a range of LEDs.
* @param start offset in the LED strip to start the range
* @param length number of LEDs in the range. eg: 2
*/
//% subcategory="ZIP LEDs"
//% weight=89 blockGap=8
//% blockId="kitronik_smart_greenhouse_range" block="%zipLEDs|range from %start|with %length|LEDs"
range(start: number, length: number): greenhouseZIPLEDs {
start = start >> 0;
length = length >> 0;
let zipLEDs = new greenhouseZIPLEDs();
zipLEDs.buf = this.buf;
zipLEDs.pin = this.pin;
zipLEDs.brightness = this.brightness;
zipLEDs.start = this.start + Math.clamp(0, this._length - 1, start);
zipLEDs._length = Math.clamp(0, this._length - (zipLEDs.start - this.start), length);
return zipLEDs;
}
/**
* Create a range for the on-board Status LEDs.
*/
//% subcategory="ZIP LEDs"
//% weight=99 blockGap=8
//% blockId="kitronik_smart_greenhouse_status_leds_range" block="%zipLEDs|range from 0 with 3 LEDs"
//% blockSetVariable=statusLEDs
statusLedsRange(): greenhouseZIPLEDs {
let statusLEDs = new greenhouseZIPLEDs();
statusLEDs.buf = this.buf;
statusLEDs.pin = this.pin;
statusLEDs.brightness = this.brightness;
statusLEDs.start = this.start + Math.clamp(0, this._length - 1, 0);
statusLEDs._length = Math.clamp(0, this._length - (statusLEDs.start - this.start), 3);
return statusLEDs;
}
/**
* Create a range for the external ZIP Stick LEDs.
*/
//% subcategory="ZIP LEDs"
//% weight=98 blockGap=8
//% blockId="kitronik_smart_greenhouse_zip_stick_range" block="%zipLEDs|range from 3 with 5 LEDs"
//% blockSetVariable=zipStick
zipStickRange(): greenhouseZIPLEDs {
let zipStick = new greenhouseZIPLEDs();
zipStick.buf = this.buf;
zipStick.pin = this.pin;
zipStick.brightness = this.brightness;
zipStick.start = this.start + Math.clamp(0, this._length - 1, 3);
zipStick._length = Math.clamp(0, this._length - (zipStick.start - this.start), 5);
return zipStick;
}
/**
* Rotate LEDs forward.
* You need to call ``show`` to make the changes visible.
* @param offset number of ZIP LEDs to rotate forward, eg: 1
*/
//% subcategory="ZIP LEDs"
//% blockId="kitronik_smart_greenhouse_display_rotate" block="%zipLEDs|rotate ZIP LEDs by %offset" blockGap=8
//% weight=92
rotate(offset: number = 1): void {
this.buf.rotate(-offset * 3, this.start * 3, this._length * 3)
}
/**
* Sets all the ZIP LEDs to a given color (range 0-255 for r, g, b). Call Show to make changes visible
* @param rgb RGB color of the LED
*/
//% subcategory="ZIP LEDs"
//% blockId="kitronik_smart_greenhouse_display_only_set_strip_color" block="%zipLEDs|set color %rgb=kitronik_smart_greenhouse_colors"
//% weight=96 blockGap=8
setColor(rgb: number) {
rgb = rgb >> 0;
this.setAllRGB(rgb);
}
/**
* Shows all the ZIP LEDs as a given color (range 0-255 for r, g, b).
* @param rgb RGB color of the LED
*/
//% subcategory="ZIP LEDs"
//% blockId="kitronik_smart_greenhouse_display_set_strip_color" block="%zipLEDs|show color %rgb=kitronik_smart_greenhouse_colors"
//% weight=97 blockGap=8
showColor(rgb: number) {
rgb = rgb >> 0;
this.setAllRGB(rgb);
this.show();
}
/**
* Set particular ZIP LED to a given color.
* You need to call ``show changes`` to make the changes visible.
* @param zipLedNum position of the ZIP LED in the string
* @param rgb RGB color of the ZIP LED
*/
//% subcategory="ZIP LEDs"
//% blockId="kitronik_smart_greenhouse_set_zip_color" block="%zipLEDs|set ZIP LED %zipLedNum|to %rgb=kitronik_smart_greenhouse_colors"
//% weight=95 blockGap=8
setZipLedColor(zipLedNum: number, rgb: number): void {
this.setPixelRGB(zipLedNum >> 0, rgb >> 0);
}
/**
* Send all the changes to the ZIP LEDs.
*/
//% subcategory="ZIP LEDs"
//% blockId="kitronik_smart_greenhouse_display_show" block="%zipLEDs|show" blockGap=8
//% weight=94
show() {
//use the Kitronik version which respects brightness for all
//ws2812b.sendBuffer(this.buf, this.pin, this.brightness);
// Use the pxt-microbit core version which now respects brightness (10/2020)
light.sendWS2812BufferWithBrightness(this.buf, this.pin, this.brightness);
control.waitMicros(100) // This looks messy, but it fixes the issue sometimes found when using multiple ZIP LED ranges, where the settings for the first range are clocked through to the next range. A short pause allows the ZIP LEDs to realise they need to stop pushing data.
}
/**
* Turn off all the ZIP LEDs.
* You need to call ``show`` to make the changes visible.
*/
//% subcategory="ZIP LEDs"
//% blockId="kitronik_smart_greenhouse_display_clear" block="%zipLEDs|clear"
//% weight=93 blockGap=8
clear(): void {
this.buf.fill(0, this.start * 3, this._length * 3);
}
/**
* Set the brightness of the ZIP LEDs. This flag only applies to future show operation.
* @param brightness a measure of LED brightness in 0-255. eg: 255
*/
//% subcategory="ZIP LEDs"
//% blockId="kitronik_smart_greenhouse_display_set_brightness" block="%zipLEDs|set brightness %brightness" blockGap=8
//% weight=91
//% brightness.min=0 brightness.max=255
setBrightness(brightness: number): void {
//Clamp incoming variable at 0-255 as values out of this range cause unexpected brightnesses as the lower level code only expects a byte.
if(brightness <0)
{
brightness = 0
}
else if (brightness > 255)
{
brightness = 255
}
this.brightness = brightness & 0xff;
basic.pause(1) //add a pause to stop wierdnesses
}
//Sets up the buffer for pushing LED control data out to LEDs
private setBufferRGB(offset: number, red: number, green: number, blue: number): void {
this.buf[offset + 0] = green;
this.buf[offset + 1] = red;
this.buf[offset + 2] = blue;
}
//Separates out Red, Green and Blue data and fills the LED control data buffer for all LEDs
private setAllRGB(rgb: number) {
let red = unpackR(rgb);
let green = unpackG(rgb);
let blue = unpackB(rgb);
const end = this.start + this._length;
for (let i = this.start; i < end; ++i) {
this.setBufferRGB(i * 3, red, green, blue)
}
}
//Separates out Red, Green and Blue data and fills the LED control data buffer for a single LED
private setPixelRGB(pixeloffset: number, rgb: number): void {
if (pixeloffset < 0
|| pixeloffset >= this._length)
return;
pixeloffset = (pixeloffset + this.start) * 3;
let red = unpackR(rgb);
let green = unpackG(rgb);
let blue = unpackB(rgb);
this.setBufferRGB(pixeloffset, red, green, blue)
}
}
/**
* Create a new ZIP LED driver for Smart Greenhouse (LEDs on and off board).
* @param zipNum is the total number of ZIP LEDs eg: 8
*/
//% subcategory="ZIP LEDs"
//% blockId="kitronik_smart_greenhouse_display_create" block="Smart Greenhouse with %zipNum|ZIP LEDs"
//% weight=100 blockGap=8
//% trackArgs=0,2
//% blockSetVariable=zipLEDs
export function createGreenhouseZIPDisplay(zipNum: number): greenhouseZIPLEDs {
let zipLEDs = new greenhouseZIPLEDs;
zipLEDs.buf = pins.createBuffer(zipNum * 3);
zipLEDs.start = 0;
zipLEDs._length = zipNum;
zipLEDs.setBrightness(128)
zipLEDs.pin = DigitalPin.P8;
pins.digitalWritePin(zipLEDs.pin, 0);
return zipLEDs;
}
/**
* Converts wavelength value to red, green, blue channels
* @param wavelength value between 470 and 625. eg: 500
*/
//% subcategory="ZIP LEDs"
//% weight=1 blockGap=8
//% blockId="kitronik_smart_greenhouse_wavelength" block="wavelength %wavelength|nm"
//% wavelength.min=470 wavelength.max=625
export function wavelength(wavelength: number): number {
/* The LEDs we are using have centre wavelengths of 470nm (Blue) 525nm(Green) and 625nm (Red)
* We blend these linearly to give the impression of the other wavelengths.
* as we cant wavelength shift an actual LED... (Ye canna change the laws of physics Capt)*/
let r = 0;
let g = 0;
let b = 0;
if ((wavelength >= 470) && (wavelength < 525)){
//We are between Blue and Green so mix those
g = pins.map(wavelength,470,525,0,255);
b = pins.map(wavelength,470,525,255,0);
}
else if ((wavelength >= 525) && (wavelength <= 625)){
//we are between Green and Red, so mix those
r = pins.map(wavelength,525,625,0,255);
g = pins.map(wavelength,525,625,255,0);
}
return packRGB(r, g, b);
}
/**
* Converts hue (0-360) to an RGB value.
* Does not attempt to modify luminosity or saturation.
* Colours end up fully saturated.
* @param hue value between 0 and 360
*/
//% subcategory="ZIP LEDs"
//% weight=1 blockGap=8
//% blockId="kitronik_smart_greenhouse_hue" block="hue %hue"
//% hue.min=0 hue.max=360
export function hueToRGB(hue: number): number {
let redVal = 0
let greenVal = 0
let blueVal = 0
let hueStep = 2.125
if ((hue >= 0) && (hue < 120)) { //RedGreen section
greenVal = Math.floor((hue) * hueStep)
redVal = 255 - greenVal
}
else if ((hue >= 120) && (hue < 240)) { //GreenBlueSection
blueVal = Math.floor((hue - 120) * hueStep)
greenVal = 255 - blueVal
}
else if ((hue >= 240) && (hue < 360)) { //BlueRedSection
redVal = Math.floor((hue - 240) * hueStep)
blueVal = 255 - redVal
}
return ((redVal & 0xFF) << 16) | ((greenVal & 0xFF) << 8) | (blueVal & 0xFF);
}
/* The LEDs we are using have centre wavelengths of 470nm (Blue) 525nm(Green) and 625nm (Red)
* We blend these linearly to give the impression of the other wavelengths.
* as we cant wavelength shift an actual LED... (Ye canna change the laws of physics Capt)*/
/**
* Converts value to red, green, blue channels
* @param red value of the red channel between 0 and 255. eg: 255
* @param green value of the green channel between 0 and 255. eg: 255
* @param blue value of the blue channel between 0 and 255. eg: 255
*/
//% subcategory="ZIP LEDs"
//% weight=1 blockGap=8
//% blockId="kitronik_smart_greenhouse_rgb" block="red %red|green %green|blue %blue"
export function rgb(red: number, green: number, blue: number): number {
return packRGB(red, green, blue);
}
/**
* Gets the RGB value of a known color
*/
//% subcategory="ZIP LEDs"
//% weight=2 blockGap=8
//% blockId="kitronik_smart_greenhouse_colors" block="%color"
export function colors(color: ZipLedColors): number {
return color;
}
//Combines individual RGB settings to be a single number
function packRGB(a: number, b: number, c: number): number {
return ((a & 0xFF) << 16) | ((b & 0xFF) << 8) | (c & 0xFF);
}
//Separates red value from combined number
function unpackR(rgb: number): number {
let r = (rgb >> 16) & 0xFF;
return r;
}
//Separates green value from combined number
function unpackG(rgb: number): number {
let g = (rgb >> 8) & 0xFF;
return g;
}
//Separates blue value from combined number
function unpackB(rgb: number): number {
let b = (rgb) & 0xFF;
return b;
}
/**
* Converts a hue saturation luminosity value into a RGB color
*/
function hsl(h: number, s: number, l: number): number {
h = Math.round(h);
s = Math.round(s);
l = Math.round(l);
h = h % 360;
s = Math.clamp(0, 99, s);
l = Math.clamp(0, 99, l);
let c = Math.idiv((((100 - Math.abs(2 * l - 100)) * s) << 8), 10000); //chroma, [0,255]
let h1 = Math.idiv(h, 60);//[0,6]
let h2 = Math.idiv((h - h1 * 60) * 256, 60);//[0,255]
let temp = Math.abs((((h1 % 2) << 8) + h2) - 256);
let x = (c * (256 - (temp))) >> 8;//[0,255], second largest component of this color
let r$: number;
let g$: number;
let b$: number;
if (h1 == 0) {
r$ = c; g$ = x; b$ = 0;
} else if (h1 == 1) {
r$ = x; g$ = c; b$ = 0;
} else if (h1 == 2) {
r$ = 0; g$ = c; b$ = x;
} else if (h1 == 3) {
r$ = 0; g$ = x; b$ = c;
} else if (h1 == 4) {
r$ = x; g$ = 0; b$ = c;
} else if (h1 == 5) {
r$ = c; g$ = 0; b$ = x;
}
let m = Math.idiv((Math.idiv((l * 2 << 8), 100) - c), 2);
let r = r$ + m;
let g = g$ + m;
let b = b$ + m;
return packRGB(r, g, b);
}
/**
* Options for direction hue changes, used by rainbow block (never visible to end user)
*/
export enum HueInterpolationDirection {
Clockwise,
CounterClockwise,
Shortest
}
////////////////////////////////
// RTC //
////////////////////////////////
/**
* Alarm repeat type
*/
export enum AlarmType {
//% block="Single"
Single = 0,
//% block="Daily Repeating"
Repeating = 1
}
/**
* Alarm silence type
*/
export enum AlarmSilence {
//% block="Auto Silence"
autoSilence = 1,
//% block="User Silence"
userSilence = 2
}
let alarmHour = 0 //The hour setting for the alarm
let alarmMin = 0 //The minute setting for the alarm
export let alarmSetFlag = 0 //Flag set to '1' when an alarm is set
let alarmRepeat = 0 //If '1' shows that the alarm should remain set so it triggers at the next time match
let alarmOff = 0 //If '1' shows that alarm should auto switch off, if '2' the user must switch off
let alarmTriggered = 0 //Flag to show if the alarm has been triggered ('1') or not ('0')
let alarmTriggerHandler: Action
let alarmHandler: Action
let simpleCheck = 0 //If '1' shows that the alarmHandler is not required as the check is inside an "if" statement
/**
* Set time on RTC, as three numbers
* @param setHours is to set the hours
* @param setMinutes is to set the minutes
* @param setSeconds is to set the seconds
*/
//% subcategory="Clock"
//% group="Set Time"
//% blockId=kitronik_smart_greenhouse_set_time
//% block="Set Time to %setHours|hrs %setMinutes|mins %setSeconds|secs"
//% setHours.min=0 setHours.max=23
//% setMinutes.min=0 setMinutes.max=59
//% setSeconds.min=0 setSeconds.max=59
//% weight=100 blockGap=8
export function setTime(setHours: number, setMinutes: number, setSeconds: number): void {
if (kitronik_RTC.initalised == false) {
kitronik_RTC.secretIncantation()
}
let bcdHours = kitronik_RTC.decToBcd(setHours) //Convert number to binary coded decimal
let bcdMinutes = kitronik_RTC.decToBcd(setMinutes) //Convert number to binary coded decimal
let bcdSeconds = kitronik_RTC.decToBcd(setSeconds) //Convert number to binary coded decimal
let writeBuf = pins.createBuffer(2)
writeBuf[0] = kitronik_RTC.RTC_SECONDS_REG
writeBuf[1] = kitronik_RTC.STOP_RTC //Disable Oscillator
pins.i2cWriteBuffer(kitronik_RTC.CHIP_ADDRESS, writeBuf, false)
writeBuf[0] = kitronik_RTC.RTC_HOURS_REG
writeBuf[1] = bcdHours //Send new Hours value
pins.i2cWriteBuffer(kitronik_RTC.CHIP_ADDRESS, writeBuf, false)
writeBuf[0] = kitronik_RTC.RTC_MINUTES_REG
writeBuf[1] = bcdMinutes //Send new Minutes value
pins.i2cWriteBuffer(kitronik_RTC.CHIP_ADDRESS, writeBuf, false)
writeBuf[0] = kitronik_RTC.RTC_SECONDS_REG
writeBuf[1] = kitronik_RTC.START_RTC | bcdSeconds //Send new seconds masked with the Enable Oscillator
pins.i2cWriteBuffer(kitronik_RTC.CHIP_ADDRESS, writeBuf, false)
}
/**
* Read time from RTC as a string
*/
//% subcategory="Clock"
//% group="Read Time"
//% blockId=kitronik_smart_greenhouse_read_time
//% block="Read Time as String"
//% weight=95 blockGap=8
export function readTime(): string {
if (kitronik_RTC.initalised == false) {
kitronik_RTC.secretIncantation()
}
//read Values
kitronik_RTC.readValue()
let decSeconds = kitronik_RTC.bcdToDec(kitronik_RTC.currentSeconds, kitronik_RTC.RTC_SECONDS_REG) //Convert number to Decimal
let decMinutes = kitronik_RTC.bcdToDec(kitronik_RTC.currentMinutes, kitronik_RTC.RTC_MINUTES_REG) //Convert number to Decimal
let decHours = kitronik_RTC.bcdToDec(kitronik_RTC.currentHours, kitronik_RTC.RTC_HOURS_REG) //Convert number to Decimal
//Combine hours,minutes and seconds in to one string
let strTime: string = "" + ((decHours / 10)>>0) + decHours % 10 + ":" + ((decMinutes / 10)>>0) + decMinutes % 10 + ":" + ((decSeconds / 10)>>0) + decSeconds % 10
return strTime
}
/**
* Set date on RTC as three numbers
* @param setDay is to set the day in terms of numbers 1 to 31
* @param setMonths is to set the month in terms of numbers 1 to 12
* @param setYears is to set the years in terms of numbers 0 to 99
*/
//% subcategory="Clock"
//% group="Set Date"
//% blockId=kitronik_smart_greenhouse_set_date
//% block="Set Date to %setDays|Day %setMonths|Month %setYear|Year"
//% setDay.min=1 setDay.max=31
//% setMonth.min=1 setMonth.max=12
//% setYear.min=0 setYear.max=99
//% weight=90 blockGap=8
export function setDate(setDay: number, setMonth: number, setYear: number): void {
if (kitronik_RTC.initalised == false) {
kitronik_RTC.secretIncantation()
}
let leapYearCheck = 0
let writeBuf = pins.createBuffer(2)
let readBuf = pins.createBuffer(1)
let bcdDay = 0
let bcdMonths = 0
let bcdYears = 0
let readCurrentSeconds = 0
//Check day entered does not exceed month that has 30 days in
if ((setMonth == 4) || (setMonth == 6) || (setMonth == 9) || (setMonth == 11)) {
if (setDay == 31) {
setDay = 30
}
}
//Leap year check and does not exceed 30 days
if ((setMonth == 2) && (setDay >= 29)) {
leapYearCheck = setYear % 4
if (leapYearCheck == 0)
setDay = 29
else
setDay = 28
}
let weekday = kitronik_RTC.calcWeekday(setDay, setMonth, (setYear+2000))
bcdDay = kitronik_RTC.decToBcd(setDay) //Convert number to binary coded decimal
bcdMonths = kitronik_RTC.decToBcd(setMonth) //Convert number to binary coded decimal
bcdYears = kitronik_RTC.decToBcd(setYear) //Convert number to binary coded decimal
writeBuf[0] = kitronik_RTC.RTC_SECONDS_REG
pins.i2cWriteBuffer(kitronik_RTC.CHIP_ADDRESS, writeBuf, false)
readBuf = pins.i2cReadBuffer(kitronik_RTC.CHIP_ADDRESS, 1, false)
readCurrentSeconds = readBuf[0]
writeBuf[0] = kitronik_RTC.RTC_SECONDS_REG
writeBuf[1] = kitronik_RTC.STOP_RTC //Disable Oscillator
pins.i2cWriteBuffer(kitronik_RTC.CHIP_ADDRESS, writeBuf, false)
writeBuf[0] = kitronik_RTC.RTC_WEEKDAY_REG
writeBuf[1] = weekday //Send new Weekday value
pins.i2cWriteBuffer(kitronik_RTC.CHIP_ADDRESS, writeBuf, false)
writeBuf[0] = kitronik_RTC.RTC_DAY_REG
writeBuf[1] = bcdDay //Send new Day value
pins.i2cWriteBuffer(kitronik_RTC.CHIP_ADDRESS, writeBuf, false)
writeBuf[0] = kitronik_RTC.RTC_MONTH_REG
writeBuf[1] = bcdMonths //Send new Months value
pins.i2cWriteBuffer(kitronik_RTC.CHIP_ADDRESS, writeBuf, false)
writeBuf[0] = kitronik_RTC.RTC_YEAR_REG
writeBuf[1] = bcdYears //Send new Year value
pins.i2cWriteBuffer(kitronik_RTC.CHIP_ADDRESS, writeBuf, false)
writeBuf[0] = kitronik_RTC.RTC_SECONDS_REG
writeBuf[1] = kitronik_RTC.START_RTC | readCurrentSeconds //Enable Oscillator
pins.i2cWriteBuffer(kitronik_RTC.CHIP_ADDRESS, writeBuf, false)
}
/**
* Read date from RTC as a string
*/
//% subcategory="Clock"
//% group="Read Date"
//% blockId=kitronik_smart_greenhouse_read_date
//% block="Read Date as String"
//% weight=85 blockGap=8
export function readDate(): string {
if (kitronik_RTC.initalised == false) {
kitronik_RTC.secretIncantation()
}
//read Values
kitronik_RTC.readValue()
let decDay = kitronik_RTC.bcdToDec(kitronik_RTC.currentDay, kitronik_RTC.RTC_DAY_REG) //Convert number to Decimal
let decMonths = kitronik_RTC.bcdToDec(kitronik_RTC.currentMonth, kitronik_RTC.RTC_MONTH_REG) //Convert number to Decimal
let decYears = kitronik_RTC.bcdToDec(kitronik_RTC.currentYear, kitronik_RTC.RTC_YEAR_REG) //Convert number to Decimal
//let strDate: string = decDay + "/" + decMonths + "/" + decYears
let strDate: string = "" + ((decDay / 10)>>0) + (decDay % 10) + "/" + ((decMonths / 10)>>0) + (decMonths % 10) + "/" + ((decYears / 10)>>0) + (decYears % 10)
return strDate
}
/**Read time parameter from RTC*/
//% subcategory="Clock"
//% group="Read Time"
//% blockId=kitronik_smart_greenhouse_read_time_parameter
//% block="Read %selectParameter| as Number"
//% weight=75 blockGap=8
export function readTimeParameter(selectParameter: TimeParameter): number {
if (kitronik_RTC.initalised == false) {
kitronik_RTC.secretIncantation()
}
let decParameter = 0
//read Values
kitronik_RTC.readValue()
//from enum convert the required time parameter and return
if (selectParameter == TimeParameter.Hours){
decParameter = kitronik_RTC.bcdToDec(kitronik_RTC.currentHours, kitronik_RTC.RTC_HOURS_REG) //Convert number to Decimal
}
else if (selectParameter == TimeParameter.Minutes){
decParameter = kitronik_RTC.bcdToDec(kitronik_RTC.currentMinutes, kitronik_RTC.RTC_MINUTES_REG) //Convert number to Decimal
}
else if (selectParameter == TimeParameter.Seconds){
decParameter = kitronik_RTC.bcdToDec(kitronik_RTC.currentSeconds, kitronik_RTC.RTC_SECONDS_REG) //Convert number to Decimal
}
return decParameter
}
/**
* Set the hours on the RTC in 24 hour format
* @param writeHours is to set the hours in terms of numbers 0 to 23
*/
//% subcategory="Clock"
//% group="Set Time"
//% blockId=kitronik_smart_greenhouse_write_hours
//% block="Set Hours to %hours|hrs"
//% hours.min=0 hours.max=23
//% weight=80 blockGap=8
export function writeHours(hours: number): void {
if (kitronik_RTC.initalised == false) {
kitronik_RTC.secretIncantation()
}
let bcdHours = kitronik_RTC.decToBcd(hours)
let writeBuf = pins.createBuffer(2)
writeBuf[0] = kitronik_RTC.RTC_SECONDS_REG
writeBuf[1] = kitronik_RTC.STOP_RTC //Disable Oscillator
pins.i2cWriteBuffer(kitronik_RTC.CHIP_ADDRESS, writeBuf, false)
writeBuf[0] = kitronik_RTC.RTC_HOURS_REG
writeBuf[1] = bcdHours //Send new value
pins.i2cWriteBuffer(kitronik_RTC.CHIP_ADDRESS, writeBuf, false)
writeBuf[0] = kitronik_RTC.RTC_SECONDS_REG
writeBuf[1] = kitronik_RTC.START_RTC //Enable Oscillator
pins.i2cWriteBuffer(kitronik_RTC.CHIP_ADDRESS, writeBuf, false)
}
/**
* Set the minutes on the RTC
* @param writeMinutes is to set the minutes in terms of numbers 0 to 59
*/
//% subcategory="Clock"
//% group="Set Time"
//% blockId=kitronik_smart_greenhouse_write_minutes
//% block="Set Minutes to %minutes|mins"
//% minutes.min=0 minutes.max=59
//% weight=70 blockGap=8
export function writeMinutes(minutes: number): void {
if (kitronik_RTC.initalised == false) {
kitronik_RTC.secretIncantation()
}
let bcdMinutes = kitronik_RTC.decToBcd(minutes)
let writeBuf = pins.createBuffer(2)
writeBuf[0] = kitronik_RTC.RTC_SECONDS_REG
writeBuf[1] = kitronik_RTC.STOP_RTC //Disable Oscillator
pins.i2cWriteBuffer(kitronik_RTC.CHIP_ADDRESS, writeBuf, false)
writeBuf[0] = kitronik_RTC.RTC_MINUTES_REG
writeBuf[1] = bcdMinutes //Send new value
pins.i2cWriteBuffer(kitronik_RTC.CHIP_ADDRESS, writeBuf, false)
writeBuf[0] = kitronik_RTC.RTC_SECONDS_REG
writeBuf[1] = kitronik_RTC.START_RTC //Enable Oscillator
pins.i2cWriteBuffer(kitronik_RTC.CHIP_ADDRESS, writeBuf, false)
}
/**
* Set the seconds on the RTC
* @param writeSeconds is to set the seconds in terms of numbers 0 to 59
*/
//% subcategory="Clock"
//% group="Set Time"
//% blockId=kitronik_smart_greenhouse_write_seconds
//% block="Set Seconds to %seconds|secs"
//% seconds.min=0 seconds.max=59
//% weight=60 blockGap=8
export function writeSeconds(seconds: number): void {
if (kitronik_RTC.initalised == false) {
kitronik_RTC.secretIncantation()
}
let bcdSeconds = kitronik_RTC.decToBcd(seconds)
let writeBuf = pins.createBuffer(2)
writeBuf[0] = kitronik_RTC.RTC_SECONDS_REG
writeBuf[1] = kitronik_RTC.STOP_RTC //Disable Oscillator
pins.i2cWriteBuffer(kitronik_RTC.CHIP_ADDRESS, writeBuf, false)
writeBuf[0] = kitronik_RTC.RTC_SECONDS_REG
writeBuf[1] = kitronik_RTC.START_RTC | bcdSeconds //Enable Oscillator and Send new value
pins.i2cWriteBuffer(kitronik_RTC.CHIP_ADDRESS, writeBuf, false)
}
/**Read time parameter from RTC*/
//% subcategory="Clock"
//% group="Read Date"
//% blockId=kitronik_smart_greenhouse_read_date_parameter
//% block="Read %selectParameter| as Number"
//% weight=65 blockGap=8
export function readDateParameter(selectParameter: DateParameter): number {
if (kitronik_RTC.initalised == false) {
kitronik_RTC.secretIncantation()
}
let decParameter = 0
//read Values
kitronik_RTC.readValue()
//from enum convert the required time parameter and return
if (selectParameter == DateParameter.Day){
decParameter = kitronik_RTC.bcdToDec(kitronik_RTC.currentDay, kitronik_RTC.RTC_DAY_REG) //Convert number to Decimal
}
else if (selectParameter == DateParameter.Month){
decParameter = kitronik_RTC.bcdToDec(kitronik_RTC.currentMonth, kitronik_RTC.RTC_MONTH_REG) //Convert number to Decimal
}
else if (selectParameter == DateParameter.Year){
decParameter = kitronik_RTC.bcdToDec(kitronik_RTC.currentYear, kitronik_RTC.RTC_YEAR_REG) //Convert number to Decimal
}
return decParameter
}
/**
* Set the day on the RTC
* @param writeDay is to set the day in terms of numbers 0 to 31
*/
//% subcategory="Clock"
//% group="Set Date"
//% blockId=kitronik_smart_greenhouse_write_day
//% block="Set Day to %day|day"
//% day.min=1 day.max=31
//% weight=50 blockGap=8
export function writeDay(day: number): void {
if (kitronik_RTC.initalised == false) {
kitronik_RTC.secretIncantation()
}
let bcdDay = kitronik_RTC.decToBcd(day)
let writeBuf = pins.createBuffer(2)
writeBuf[0] = kitronik_RTC.RTC_SECONDS_REG
writeBuf[1] = kitronik_RTC.STOP_RTC //Disable Oscillator
pins.i2cWriteBuffer(kitronik_RTC.CHIP_ADDRESS, writeBuf, false)
writeBuf[0] = kitronik_RTC.RTC_DAY_REG
writeBuf[1] = bcdDay //Send new value
pins.i2cWriteBuffer(kitronik_RTC.CHIP_ADDRESS, writeBuf, false)
writeBuf[0] = kitronik_RTC.RTC_SECONDS_REG
writeBuf[1] = kitronik_RTC.START_RTC //Enable Oscillator
pins.i2cWriteBuffer(kitronik_RTC.CHIP_ADDRESS, writeBuf, false)
}
/**
* set the month on the RTC
* @param writeMonth is to set the month in terms of numbers 1 to 12
*/
//% subcategory="Clock"
//% group="Set Date"
//% blockId=kitronik_smart_greenhouse_write_month
//% block="Set Month to %month|month"
//% month.min=1 month.max=12
//% weight=40 blockGap=8
export function writeMonth(month: number): void {
if (kitronik_RTC.initalised == false) {
kitronik_RTC.secretIncantation()
}
let bcdMonth = kitronik_RTC.decToBcd(month)
let writeBuf = pins.createBuffer(2)
writeBuf[0] = kitronik_RTC.RTC_SECONDS_REG
writeBuf[1] = kitronik_RTC.STOP_RTC //Disable Oscillator
pins.i2cWriteBuffer(kitronik_RTC.CHIP_ADDRESS, writeBuf, false)
writeBuf[0] = kitronik_RTC.RTC_MONTH_REG
writeBuf[1] = bcdMonth //Send new value
pins.i2cWriteBuffer(kitronik_RTC.CHIP_ADDRESS, writeBuf, false)
writeBuf[0] = kitronik_RTC.RTC_SECONDS_REG
writeBuf[1] = kitronik_RTC.START_RTC //Enable Oscillator
pins.i2cWriteBuffer(kitronik_RTC.CHIP_ADDRESS, writeBuf, false)
}
/**
* set the year on the RTC
* @param writeYear is to set the year in terms of numbers 0 to 99
*/
//% subcategory="Clock"
//% group="Set Date"
//% blockId=kitronik_smart_greenhouse_write_year
//% block="Set Year to %year|year"
//% year.min=0 year.max=99
//% weight=30 blockGap=8
export function writeYear(year: number): void {
if (kitronik_RTC.initalised == false) {
kitronik_RTC.secretIncantation()
}
let bcdYear = kitronik_RTC.decToBcd(year) //Convert number to BCD
let writeBuf = pins.createBuffer(2)
writeBuf[0] = kitronik_RTC.RTC_SECONDS_REG
writeBuf[1] = kitronik_RTC.STOP_RTC //Disable Oscillator
pins.i2cWriteBuffer(kitronik_RTC.CHIP_ADDRESS, writeBuf, false)
writeBuf[0] = kitronik_RTC.RTC_YEAR_REG
writeBuf[1] = bcdYear //Send new value
pins.i2cWriteBuffer(kitronik_RTC.CHIP_ADDRESS, writeBuf, false)
writeBuf[0] = kitronik_RTC.RTC_SECONDS_REG
writeBuf[1] = kitronik_RTC.START_RTC //Enable Oscillator
pins.i2cWriteBuffer(kitronik_RTC.CHIP_ADDRESS, writeBuf, false)
}
/**
* Set simple alarm
* @param alarmType determines whether the alarm repeats
* @param hour is the alarm hour setting (24 hour)
* @param min is the alarm minute setting
* @param alarmSilence determines whether the alarm turns off automatically or the user turns it off
*/
//% subcategory="Clock"
//% group=Alarm
//% blockId=kitronik_smart_greenhouse_simple_set_alarm
//% block="set %alarmType|alarm to %hour|:%min|with %alarmSilence"
//% hour.min=0 hour.max=23
//% min.min=0 min.max=59
//% sec.min=0 sec.max=59
//% inlineInputMode=inline
//% weight=26 blockGap=8
export function simpleAlarmSet(alarmType: AlarmType, hour: number, min: number, alarmSilence: AlarmSilence): void {
if (kitronik_RTC.initalised == false) {
kitronik_RTC.secretIncantation()
}
if (alarmType == 1) {
alarmRepeat = 1 //Daily Repeating Alarm
}
else {
alarmRepeat = 0 //Single Alarm
}
if (alarmSilence == 1) {
alarmOff = 1 //Auto Silence
}
else if (alarmSilence == 2) {
alarmOff = 2 //User Silence
}