-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDS1921_Logger.ino
1839 lines (1605 loc) · 40.5 KB
/
DS1921_Logger.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
#include <avr/interrupt.h>
#include <avr/sleep.h>
#include <SPI.h>
#include <Wire.h>
/* OneWire DS18S20, DS18B20, DS1822 Temperature Example
*
* http://www.pjrc.com/teensy/td_libs_OneWire.html
*
* The DallasTemperature library can do all this work for you!
* http://milesburton.com/Dallas_Temperature_Control_Library
*/
//#define ARDUINO_UNO
#define DEBUG_ASSERT
#define DEBUG_SENSOR
//#define DEBUG_TIMING
#define USE_ZIGBEE_DEBUG
//#define USE_SOFT_XBEE
#define USE_ARDUINO_XBEE
#define ONE_WIRE_SENSORS
#define SELF_POWERED
#define LED_NOTIFICATION
#define PIEZO_NOTIFICATION
#include "DS1921_Logger.h"
#include "serial.h"
#include "xbee.h"
#include "sensor.h"
#ifdef USE_SOFT_XBEE
#include <SoftwareSerial.h>
#endif
#ifdef USE_ARDUINO_XBEE
#include <XBee.h>
#endif
#ifdef ONE_WIRE_SENSORS
#include <OneWire.h>
#endif
#define DS18B20_CONVERT_TEMP 0x44
#define DS18B20_COPY_SCRATCHPAD 0x48
#define DS18B20_READ_SCRATCHPAD 0xBE
#define DS18B20_WRITE_SCRATCHPAD 0x4E
#define DS18B20_RECALL_EE 0xB8
#define DS18B20_READ_POWER_SUPPLY 0xB4
#define DS1921_CONVERT_TEMP 0x44
#define DS1921_COPY_SCRATCHPAD 0x55
#define DS1921_READ_SCRATCHPAD 0xAA
#define DS1921_WRITE_SCRATCHPAD 0x0F
#define DS1921_CLEAR_MEMORY 0x3C
#define DS1921_READ_MEMORY 0xF0
#define DS1921_RTC_REGISTER 0x0200
#define DS1921_RTC_ALARM_REGISTER 0x0207
#define DS1921_CONTROL_REGISTER 0x020E
#define DS1921_STATUS_REGISTER 0x0214
#define DS1921_SAMPLE_REGISTER 0x020D
#define DS1921_DEVICE_SAMPLES_COUNTER 0x021D
#define DS1921_MISSION_SAMPLES_COUNTER 0x021A
#define DS1921_MISSION_TIMESTAMP 0x0215
#define DS1921_MISSION_START_DELAY 0x0212
#define DS1921_TEMPERATURE 0x0211
#define DS1921_TEMPERATURE_LOW_ALARM 0x020B
#define DS1921_TEMPERATURE_HIGH_ALARM 0x020C
#define DS1921_TEMP_HISTOGRAM 0x0800
#define DS1921_DATA_LOG 0x1000
/*
* Device Samples Counter: 24-bit unsigned little-endian
* Mission Samples Counter: 24-bit unsigned little-endian
* Mission Timestamp:
* BCD minutes
* BCD hours (24-hour)
* BCD day of month
* BCD year without century
*/
#define DS1921_CONTROL_nEOSC 0x80
#define DS1921_CONTROL_EMCLR 0x40
#define DS1921_CONTROL_nEM 0x10
#define DS1921_CONTROL_RO 0x08
#define DS1921_CONTROL_TLS 0x04
#define DS1921_CONTROL_THS 0x02
#define DS1921_CONTROL_TAS 0x01
#define DS1921_STATUS_nTCB 0x80
#define DS1921_STATUS_MEMCLR 0x40
#define DS1921_STATUS_MIP 0x20
#define DS1921_STATUS_SIP 0x10
#define DS1921_STATUS_TLF 0x04
#define DS1921_STATUS_THF 0x02
#define DS1921_STATUS_TAF 0x01
#ifdef LED_NOTIFICATION
#define LED_PIN LED_BUILTIN
#define ledOn() digitalWrite(LED_PIN, HIGH)
#define ledOff() digitalWrite(LED_PIN, LOW)
#endif
#ifdef PIEZO_NOTIFICATION
#define PIEZO_PIN 12
#define piezoOn() digitalWrite(PIEZO_PIN, LOW)
#define piezoOff() digitalWrite(PIEZO_PIN, HIGH)
#endif
#ifdef CORE_TEENSY
#define AREF_MV 5000.
#else
#ifdef ARDUINO_UNO
#define AREF_MV 5000.
#else
#define AREF_MV 3300.
#endif
#endif
#ifdef ARDUINO_UNO
//#define USE_SOFT_XBEE
#endif
#ifdef USE_SOFT_XBEE
#define XBEE_RX_PIN 2
#define XBEE_TX_PIN 3
SoftwareSerial xbeeSerial2 = SoftwareSerial(XBEE_RX_PIN, XBEE_TX_PIN);
#endif
class Dummy : public Stream {
public:
virtual int available(void) { return 0; }
virtual int read(void) { return -1; }
virtual int peek(void) { return -1; }
virtual void flush(void) {}
virtual size_t write(uint8_t val) { return 0; }
};
#ifdef USE_ARDUINO_XBEE
XBee xbee = XBee();
//XBeeResponse response = XBeeResponse();
ZBRxResponse rx = ZBRxResponse();
ZBTxStatusResponse txStatus = ZBTxStatusResponse();
ModemStatusResponse msr = ModemStatusResponse();
XBeeAddress64 coordinator = XBeeAddress64(0x0, 0x0);
#else
xbee_t xbeeDevice;
xbee_t *xbee = &xbeeDevice;
#endif
uint8_t timePayload[] = { 0x81, 0, 0, 0, 0 };
struct querySensorResponse sensorPayload;
uint32_t clock;
unsigned long clockTick;
class SensorDebug : public Stream {
private:
uint8_t debugPayload[110];
unsigned int offset;
#ifdef USE_ARDUINO_XBEE
ZBTxRequest zbTx;
#endif
public:
SensorDebug() : Stream() {
debugPayload[0] = 0x04;
offset = 1U;
#ifdef USE_ARDUINO_XBEE
zbTx = ZBTxRequest(coordinator, debugPayload, offset);
#endif
}
virtual int available(void) { return 0; }
virtual int read(void) { return -1; }
virtual int peek(void) { return -1; }
virtual void flush(void) {
#ifdef USE_ARDUINO_XBEE
zbTx.setPayloadLength(offset);
xbee.send(zbTx);
#endif
offset = 1U;
}
virtual size_t write(uint8_t val) {
return write(&val, sizeof(val));
}
virtual size_t write(const uint8_t *val, size_t len) {
unsigned int i;
for(i = 0U; offset < sizeof(debugPayload) && i < len; offset++, i++) {
if(val[i] == '\r') {
offset--;
continue;
} else if(val[i] == '\n') {
i++;
flush();
return len - i;
}
debugPayload[offset] = val[i];
}
if(offset > 60U)
flush();
return len - i;
}
};
#ifndef USE_ARDUINO_XBEE
HardwareSerial uart = HardwareSerial();
//#define debug Uart
#endif
/* localDebug uses serial port if available */
#if defined(CORE_TEENSY) || defined(USE_SOFT_XBEE)
#define localDebug Serial
#else
Dummy dummy = Dummy();
#define localDebug dummy
#endif
/* debug can send output over Zigbee or locally */
#ifdef USE_ZIGBEE_DEBUG
SensorDebug debug = SensorDebug();
#else
#define debug localDebug
#endif
//#define serial Serial
#define serial localDebug
uint8_t frameId = -1;
uint8_t getFrameId(void)
{
frameId++;
if(frameId == 0)
frameId = 1;
return frameId;
}
#ifdef ONE_WIRE_SENSORS
#define ONE_WIRE_PIN 9
OneWire ds(ONE_WIRE_PIN);
void writeDS18B20(byte *addr, byte high, byte low, byte config)
{
ds.reset();
ds.select(addr);
ds.write(DS18B20_WRITE_SCRATCHPAD);
ds.write(high);
ds.write(low);
ds.write(config);
ds.reset();
ds.select(addr);
ds.write(DS18B20_READ_SCRATCHPAD);
ds.read();
ds.read();
if(ds.read() != high ||
ds.read() != low ||
ds.read() != config) {
/* TODO: record failure to configure */
return;
}
ds.reset();
ds.select(addr);
ds.write(DS18B20_COPY_SCRATCHPAD);
#ifdef SELF_POWERED
/* Wait for finished writing */
while(!ds.read_bit())
;
#else
/* TODO: enable strong pull-up */
delay(10);
/* TODO: disable strong pull-up */
#endif
/* TODO: replace delay with better solution */
}
void writeDS1921(byte *addr, int target, byte *data, int len)
{
byte status;
ds.reset();
ds.select(addr);
ds.write(DS1921_WRITE_SCRATCHPAD);
ds.write(target & 0x00FF);
ds.write(target >> 8 & 0x00FF);
while(len-- > 0)
ds.write(*data++);
/* TODO: write til end of page for CRC-16 */
ds.reset();
ds.select(addr);
ds.write(DS1921_READ_SCRATCHPAD);
target = (int)ds.read();
target |= (int)ds.read() << 8;
status = ds.read();
/* TODO: check AA and PF flags */
/* TODO: read back and verify data */
/* TODO: read til end of page for CRC-16 */
ds.reset();
ds.select(addr);
ds.write(DS1921_COPY_SCRATCHPAD);
ds.write(target & 0x00FF);
ds.write(target >> 8 & 0x00FF);
ds.write(status);
delay(1);
/* TODO: replace delay with better solution */
}
void readDS1921(byte *addr, int target, byte *data, int len)
{
ds.reset();
ds.select(addr);
ds.write(DS1921_READ_MEMORY);
ds.write(target & 0x00FF);
ds.write(target >> 8 & 0x00FF);
while(len-- > 0)
*data++ = ds.read();
}
void clearDS1921(byte *addr)
{
byte control;
debug.println(" Clearing memory...");
readDS1921(addr, DS1921_STATUS_REGISTER, &control, sizeof(control));
if(control & DS1921_STATUS_MEMCLR)
debug.println(" Memory previously cleared.");
readDS1921(addr, DS1921_CONTROL_REGISTER, &control, sizeof(control));
debug.print(" Control = ");
debug.println(control, HEX);
control |= DS1921_CONTROL_EMCLR;
control |= DS1921_CONTROL_RO;
writeDS1921(addr, DS1921_CONTROL_REGISTER, &control, sizeof(control));
ds.reset();
ds.select(addr);
ds.write(DS1921_CLEAR_MEMORY);
delayMicroseconds(550);
readDS1921(addr, DS1921_STATUS_REGISTER, &control, sizeof(control));
debug.print(" Status = ");
debug.println(control, HEX);
if(control & DS1921_STATUS_MEMCLR)
debug.println(" Memory cleared successfully.");
else
debug.println(" Failed to clear memory!");
}
void stopMission(byte *addr)
{
byte zero = 0;
writeDS1921(addr, DS1921_TEMPERATURE, &zero, sizeof(zero));
}
#endif
enum {
HOME_STATE,
D_STATE,
C_STATE,
R_STATE,
RT_STATE,
RTC_STATE,
};
byte addr[8];
byte buf[11];
size_t bufCount = 0U;
long val;
#ifdef ONE_WIRE_SENSORS
void parseSerial(char c)
{
static int state = HOME_STATE;
byte rtcBuf[7];
int i;
serial.print("S=");
serial.print(state, DEC);
serial.print(": ");
serial.println(c);
switch(state) {
case HOME_STATE:
if(c == 'D') {
state = D_STATE;
break;
} else if(c == 'C') {
state = C_STATE;
bufCount = 0U;
val = 0;
break;
} else if(c == 'R') {
state = R_STATE;
break;
}
state = HOME_STATE;
break;
case D_STATE:
if(c == '\n') {
serial.println("STARTLOG");
readDS1921(addr, DS1921_MISSION_TIMESTAMP, rtcBuf, 5);
serial.print("Time=20");
serial.print(rtcBuf[4], HEX);
serial.print("-");
serial.print(rtcBuf[3], HEX);
serial.print("-");
serial.print(rtcBuf[2], HEX);
serial.print("T");
serial.print(rtcBuf[1], HEX);
serial.print(":");
serial.print(rtcBuf[0], HEX);
serial.println(":00Z");
long count = 0;
byte countBytes[3];
readDS1921(addr, DS1921_MISSION_SAMPLES_COUNTER, countBytes, sizeof(countBytes));
count = (long)countBytes[0] << 0 | (long)countBytes[1] << 8 | (long)countBytes[2] << 16;
serial.print("Count=");
serial.println(count, DEC);
readDS1921(addr, DS1921_DATA_LOG, NULL, 0);
serial.println("LOG");
if(count > 2048)
count = 2048;
while(count-- > 0) {
serial.print(" ");
serial.println(ds.read(), DEC);
}
serial.println("ENDLOG");
state = HOME_STATE;
break;
}
state = HOME_STATE;
break;
case C_STATE:
if(isdigit(c) && bufCount < sizeof(buf)-1) {
val = val*10 + c - '0';
bufCount++;
//buf[bufCount] = c;
break;
}
clock = val;
serial.print("Time set to ");
serial.println(clock, DEC);
state = HOME_STATE;
break;
case R_STATE:
if(c == 'T') {
state = RT_STATE;
break;
}
state = HOME_STATE;
break;
case RT_STATE:
if(c == 'C') {
state = RTC_STATE;
break;
}
state = HOME_STATE;
break;
case RTC_STATE:
if(c == '\n') {
serial.println("rtc time is ...");
readDS1921(addr, DS1921_RTC_REGISTER, rtcBuf, sizeof(rtcBuf));
serial.print("20");
serial.print((unsigned char)rtcBuf[6], HEX);
serial.print("-");
serial.print((unsigned char)rtcBuf[5] & ~0x80, HEX);
serial.print("-");
serial.print((unsigned char)rtcBuf[4], HEX);
serial.print("T");
serial.print((unsigned char)rtcBuf[2], HEX);
serial.print(":");
serial.print((unsigned char)rtcBuf[1], HEX);
serial.print(":");
serial.print((unsigned char)rtcBuf[0], HEX);
serial.println("Z");
state = HOME_STATE;
break;
} else {
memset(rtcBuf, 0, sizeof(rtcBuf));
/* discard 2 */
while(!serial.available())
;
c = serial.read();
/* discard 0 */
while(!serial.available())
;
c = serial.read();
rtcBuf[6] = (c & ~0x30) << 4;
while(!serial.available())
;
c = serial.read();
rtcBuf[6] |= (c & ~0x30);
while(!serial.available())
;
c = serial.read();
/* discard - */
while(!serial.available())
;
c = serial.read();
rtcBuf[5] = (c & ~0x30) << 4;
while(!serial.available())
;
c = serial.read();
rtcBuf[5] |= (c & ~0x30);
rtcBuf[5] |= 0x80;
while(!serial.available())
;
c = serial.read();
/* discard - */
while(!serial.available())
;
c = serial.read();
rtcBuf[4] = (c & ~0x30) << 4;
while(!serial.available())
;
c = serial.read();
rtcBuf[4] |= (c & ~0x30);
while(!serial.available())
;
c = serial.read();
/* discard T */
while(!serial.available())
;
c = serial.read();
rtcBuf[2] = (c & ~0x30) << 4;
while(!serial.available())
;
c = serial.read();
rtcBuf[2] |= (c & ~0x30);
while(!serial.available())
;
c = serial.read();
/* discard : */
while(!serial.available())
;
c = serial.read();
rtcBuf[1] = (c & ~0x30) << 4;
while(!serial.available())
;
c = serial.read();
rtcBuf[1] |= (c & ~0x30);
while(!serial.available())
;
c = serial.read();
/* discard : */
while(!serial.available())
;
c = serial.read();
rtcBuf[0] = (c & ~0x30) << 4;
while(!serial.available())
;
c = serial.read();
rtcBuf[0] |= (c & ~0x30);
while(!serial.available())
;
c = serial.read();
/* discard '\n' */
rtcBuf[3] = 1; /* day of week (1-7) */
serial.print(" Set RTC = 20");
for(i = 6; i >= 0; i--) {
serial.print(rtcBuf[i], HEX);
serial.print(" ");
}
serial.println();
serial.println(" Updating RTC...");
writeDS1921(addr, DS1921_RTC_REGISTER, rtcBuf, sizeof(rtcBuf));
}
state = HOME_STATE;
break;
default:
state = HOME_STATE;
break;
}
}
/* 2011-12-25T05:22:40 Sun */
byte rtc[] = {
0x40, /* BCD seconds */
0x22, /* BCD minutes */
0x05, /* BCD hours (24-hour) */
0x01, /* day of week (1-7) */
0x25, /* BCD day of month */
0x92, /* century bit + BCD month */
0x11, /* BCD year without century */
};
int writeRtc = 0;
#endif
void printTemp(temp_t celsius)
{
static int alarm = 0;
float fahrenheit;
if(celsius < 8. || celsius > 58.) {
debug.println("ALARM!");
alarm = 1;
#ifdef LED_NOTIFICATION
ledOn();
} else {
ledOff();
#endif
}
fahrenheit = celsius * 1.8 + 32.0;
debug.print(" T=");
debug.print(celsius);
debug.print("C, ");
debug.print(fahrenheit);
debug.println("F");
}
#define MAX_SENSORS 32U
sensor_t sensors[MAX_SENSORS];
void printSensor(sensor_t *sensor);
/* Analog sensor support */
#define ANALOG_BASE_IDX 0
sensorState_t readAnalogSensor(sensor_t *sensor)
{
if(sensor->type != ANALOG_SENSOR_TYPE &&
sensor->type != LIGHT_SENSOR_TYPE)
return ERROR_SENSOR_STATE;
long val = analogRead(sensor->addr);
if(sensor->type == LIGHT_SENSOR_TYPE)
sensor->data16[0] = val << 5 | (val >> 5);
else
sensor->data16[0] = val * AREF_MV * 16 / 1023 / 10 + (CELSIUS_TO_KELVIN << 4) - (50 << 4);
#ifdef DEBUG_SENSOR
printSensor(sensor);
temp_t temp = ((float)val / 1023. * AREF_MV - 500.)/10.;
debug.print("ADC Val: ");
debug.println(val, DEC);
if(sensor->type == LIGHT_SENSOR_TYPE) {
debug.print(" L=");
debug.print((double)val * 100. / 1023.);
debug.println("%");
} else {
printTemp(temp);
}
#endif
return COMPLETED_SENSOR_STATE;
}
void analogSensorInit(void)
{
const int sensorPin[] = {
A0,
A1,
//A2,
//A3,
#if defined(CORE_TEENSY) || defined(ARDUINO_UNO)
//A4,
//A5,
#else
A6,
A7,
#endif
};
for(int8_t i = sizeof(sensorPin)/sizeof(sensorPin[0])-1; i >= 0; i--) {
uint8_t addr = sensorPin[i];
pinMode(addr, INPUT);
digitalWrite(addr, LOW);
uint16_t val = analogRead(addr);
if(val < 5)
continue;
sensor_t *sensor = &sensors[i+ANALOG_BASE_IDX];
sensor->type = ANALOG_SENSOR_TYPE;
if(addr == A2)
sensor->type = LIGHT_SENSOR_TYPE;
sensor->addr = addr;
}
}
/* LM75 Sensor support */
#define LM75_BASE_IDX 8
#define LM75_NUM_SENSORS 8
#define LM75_BASE_ADDR 0x48
#define LM75_TEMP_REGISTER (uint8_t)0
#define LM75_CONFIG_REGISTER (uint8_t)1
#define LM75_THYST_REGISTER (uint8_t)2
#define LM75_TSET_REGISTER (uint8_t)3
#define LM75_CONFIG_ONE_SHOT 0x80
#define LM75_CONFIG_9BIT_RESOLUTION 0x00
#define LM75_CONFIG_10BIT_RESOLUTION 0x20
#define LM75_CONFIG_11BIT_RESOLUTION 0x40
#define LM75_CONFIG_12BIT_RESOLUTION 0x60
#define LM75_CONFIG_RESOLUTION_MASK 0x60
#define LM75_CONFIG_1_FAULT_QUEUE 0x00
#define LM75_CONFIG_2_FAULT_QUEUE 0x08
#define LM75_CONFIG_4_FAULT_QUEUE 0x10
#define LM75_CONFIG_6_FAULT_QUEUE 0x18
#define LM75_CONFIG_FAULT_QUEUE_MASK 0x18
#define LM75_CONFIG_ALERT_ACTIVE_HIGH 0x04
#define LM75_CONFIG_ALERT_INTERRUPT 0x02
#define LM75_CONFIG_SHUTDOWN 0x01
sensorState_t readLM75Sensor(sensor_t *sensor)
{
uint8_t addr = sensor->addr;
uint8_t config = 0;
uint16_t val;
#ifdef DEBUG_SENSOR
temp_t temp;
#endif
if(sensor->type != LM75_SENSOR_TYPE)
return ERROR_SENSOR_STATE;
switch(sensor->state) {
case START_SENSOR_STATE:
Wire.beginTransmission(addr);
Wire.write(LM75_CONFIG_REGISTER);
Wire.endTransmission();
Wire.requestFrom(addr, (uint8_t)1);
config = Wire.read();
#ifdef DEBUG_SENSOR
debug.print("LM75 Start Conf: 0x");
debug.println(config, HEX);
#endif
config |= LM75_CONFIG_ONE_SHOT;
Wire.beginTransmission(addr);
Wire.write(LM75_CONFIG_REGISTER);
Wire.write(config);
Wire.endTransmission();
sensor->waitTime = millis() + 200UL;
return WAIT_SENSOR_STATE;
break;
case READ_SENSOR_STATE:
Wire.requestFrom(addr, (uint8_t)1);
config = Wire.read();
#ifdef DEBUG_SENSOR
//debug.print("LM75 Read Conf: 0x");
//debug.println(config, HEX);
#endif
if(config & LM75_CONFIG_ONE_SHOT)
return READ_SENSOR_STATE;
Wire.beginTransmission(addr);
Wire.write(LM75_TEMP_REGISTER);
Wire.endTransmission();
Wire.requestFrom(addr, (uint8_t)2);
val = Wire.read() << 8;
val |= Wire.read();
val >>= 4;
sensor->data16[0] = val + (CELSIUS_TO_KELVIN << 4);
#ifdef DEBUG_SENSOR
printSensor(sensor);
temp = (temp_t)val * 0.0625f;
debug.print("I2C Val: ");
debug.println(val, DEC);
printTemp(temp);
break;
#endif
default:
break;
}
return COMPLETED_SENSOR_STATE;
}
void lm75SensorInit(void)
{
for(int8_t i = LM75_NUM_SENSORS-1; i >= 0; i--) {
uint8_t addr = LM75_BASE_ADDR + i;
Wire.beginTransmission(addr);
Wire.write(LM75_CONFIG_REGISTER);
Wire.write(LM75_CONFIG_12BIT_RESOLUTION | LM75_CONFIG_SHUTDOWN);
if(Wire.endTransmission() != 0)
continue;
sensor_t *sensor = &sensors[i+LM75_BASE_IDX];
sensor->type = LM75_SENSOR_TYPE;
sensor->addr = addr;
}
}
/* HIH6130 sensor support */
#define HIH6130_BASE_IDX 16
#define HIH6130_NUM_SENSORS 1
#define HIH6130_BASE_ADDR 0x27
//#define HIH6130_MAX_ADDR HIH6130_BASE_ADDR + 1
#define HIH6130_NORMAL_STATUS 0x0000L
#define HIH6130_STALE_STATUS 0x4000L
#define HIH6130_COMMAND_MODE_STATUS 0x8000L
#define HIH6130_DIAGNOSTIC_STATUS 0xc000L
#define HIH6130_STATUS_MASK 0xc000L
sensorState_t readHIH6130Sensor(sensor_t *sensor)
{
long humidityVal, tempVal;
if(sensor->type != HIH6130_SENSOR_TYPE)
return ERROR_SENSOR_STATE;
switch(sensor->state) {
case START_SENSOR_STATE:
Wire.beginTransmission(sensor->addr);
Wire.endTransmission();
return READ_SENSOR_STATE;
break;
case READ_SENSOR_STATE:
Wire.requestFrom(sensor->addr, (uint8_t)4);
humidityVal = Wire.read() << 8;
humidityVal |= Wire.read();
tempVal = Wire.read() << 8;
tempVal |= Wire.read();
//debug.print("H:");
//debug.print(humidityVal, HEX);
//debug.print(",");
//debug.print(humidityVal & HIH6130_STATUS_MASK, HEX);
//debug.print(",");
//debug.println(HIH6130_STALE_STATUS, HEX);
if((humidityVal & HIH6130_STATUS_MASK) == HIH6130_STALE_STATUS)
return READ_SENSOR_STATE;
break;
default:
return ERROR_SENSOR_STATE;
break;
}
long status = humidityVal & HIH6130_STATUS_MASK;
//debug.print("HIH Humidity: ");
//debug.print(humidityVal, DEC);
//debug.print(", HIH Humidity: ");
//debug.print(humidityVal, DEC);
//debug.print(", HIH loops: ");
//debug.println(i, DEC);
//float humidity = (float)(humidityVal & ~HIH6130_STATUS_MASK) / (float)(2^14 - 1);
#ifdef DEBUG_SENSOR
float humidity = (float)(humidityVal & ~HIH6130_STATUS_MASK) / 16383.f * 100.f;
printSensor(sensor);
debug.print("HIH Humidity: ");
debug.print(humidityVal, DEC);
debug.print(", Temp: ");
debug.println(tempVal, DEC);
debug.print(" Humidity: ");
debug.print(humidity);
debug.print("%, Temperature: ");
#endif
tempVal >>= 2;
sensor->data16[0] = tempVal;
sensor->data16[1] = humidityVal & ~HIH6130_STATUS_MASK;
#ifdef DEBUG_SENSOR
//temp_t temp = (temp_t)tempVal / (float)(2^14 - 1) * (125.f - -40.f);
//temp_t temp = (temp_t)(tempVal & 16383L) / 16383.f * (125.f - -40.f) + -40.f;
temp_t temp = (float)tempVal / 16383.f * 165.f - 40.f;
debug.print(temp * 9.f/5.f + 32.f);
debug.println("°F");
printTemp(temp);
#endif
switch(status) {
case HIH6130_NORMAL_STATUS:
break;
case HIH6130_COMMAND_MODE_STATUS:
debug.println("Command Mode Error");
return ERROR_SENSOR_STATE;
break;
case HIH6130_DIAGNOSTIC_STATUS:
debug.println("Diagnostic Error");
return ERROR_SENSOR_STATE;
break;
default:
debug.println("Unknown Error");
return ERROR_SENSOR_STATE;
break;
}
return COMPLETED_SENSOR_STATE;
}
void hih6130SensorInit(void)
{
for(int8_t i = HIH6130_NUM_SENSORS-1; i >= 0; i--) {
uint8_t addr = HIH6130_BASE_ADDR + i;
Wire.beginTransmission(addr);
if(Wire.endTransmission() != 0)
continue;
sensor_t *sensor = &sensors[i+HIH6130_BASE_IDX];
sensor->type = HIH6130_SENSOR_TYPE;
sensor->addr = addr;
}
}
/* TC Sensor support */
#define TC_BASE_IDX 17
#define TC_NUM_SENSORS 1
#define TC_BASE_ADDR 0
//#define tcOn() digitalWrite(TC_BASE_ADDR, LOW)
//#define tcOff() digitalWrite(TC_BASE_ADDR, HIGH)
#define TC_SIGN_FLAG 0x8000
#define TC_OPEN_FLAG 0x0004
#define TC_DEVICE_ID_FLAG 0x0002
#define TC_TRISTATE_FLAG 0x0001
#define TC_VALUE_MASK 0x7ff8
#define TC_VALUE_SHIFT 1
//#define TC_PRESENCE_TEST TC_TRISTATE_FLAG
//#define TC_PRESENCE_MASK ~(TC_VALUE_MASK | TC_OPEN_FLAG)
#define TC_PRESENCE_TEST 0
#define TC_PRESENCE_MASK ~(TC_VALUE_MASK | TC_OPEN_FLAG | TC_TRISTATE_FLAG)
sensorState_t readTCSensor(sensor_t *sensor)
{
#ifdef DEBUG_SENSOR
temp_t temp;
#endif
if(sensor->type != TC_SENSOR_TYPE)
return ERROR_SENSOR_STATE;
switch(sensor->state) {
uint16_t val;
case START_SENSOR_STATE:
digitalWrite(sensor->addr, LOW); /* SS inactive high */
delayMicroseconds(1);
val = SPI.transfer(0xff) << 8;
val |= SPI.transfer(0xff);
digitalWrite(sensor->addr, HIGH);
#ifdef DEBUG_SENSOR
printSensor(sensor);
temp = (float)((val & TC_VALUE_MASK) >> 3) * 0.25f;
debug.print("SPI Val: ");
debug.println(val, DEC);
printTemp(temp);
#endif
if(val & (TC_SIGN_FLAG | TC_OPEN_FLAG | TC_DEVICE_ID_FLAG)) {
sensor->data16[0] = -1;
return ERROR_SENSOR_STATE;
}
sensor->data16[0] = ((val & TC_VALUE_MASK) >> TC_VALUE_SHIFT) + (CELSIUS_TO_KELVIN << 4);
break;