forked from vitotai/BrewPiLess
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeviceManager.cpp
More file actions
1219 lines (1077 loc) · 35.6 KB
/
Copy pathDeviceManager.cpp
File metadata and controls
1219 lines (1077 loc) · 35.6 KB
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
/*
* Copyright 2013 Matthew McGowan
* Copyright 2013 BrewPi/Elco Jacobs.
*
* This file is part of BrewPi.
*
* BrewPi is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* BrewPi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with BrewPi. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Brewpi.h"
#include "BrewpiStrings.h"
#include "DeviceManager.h"
#include "TempControl.h"
#include "Actuator.h"
#include "Sensor.h"
#include "TempSensorDisconnected.h"
#include "TempSensorExternal.h"
#include "PiLink.h"
#include "EepromFormat.h"
#include "Wire.h"
#if AUTO_CAP
#include "AutoCapControl.h"
#endif
#if EanbleParasiteTempControl
#include "ParasiteTempController.h"
#endif
#define CALIBRATION_OFFSET_PRECISION (4)
#ifdef ARDUINO
#include "OneWireTempSensor.h"
#include "OneWireActuator.h"
#include "DS2413.h"
#include <OneWire.h>
#include "DallasTemperature.h"
#include "ActuatorArduinoPin.h"
#include "SensorArduinoPin.h"
#endif
#if BREWPI_EXTERNAL_SENSOR
#include "TempSensorWireless.h"
WirelessTempSensor* WirelessTempSensor::theWirelessTempSensor=NULL;
#endif
#if EnableDHTSensorSupport
#include "HumidityControl.h"
#include "Bme280Sensor.h"
#include "TempSensorEnv.h"
#endif
/*
* Defaults for sensors, actuators and temperature sensors when not defined in the eeprom.
*/
ValueSensor<bool> defaultSensor(false); // off
ValueActuator defaultActuator;
DisconnectedTempSensor defaultTempSensor;
#if !BREWPI_SIMULATE
#ifdef oneWirePin
OneWire DeviceManager::primaryOneWireBus(oneWirePin);
#else
OneWire DeviceManager::beerSensorBus(beerSensorPin);
OneWire DeviceManager::fridgeSensorBus(fridgeSensorPin);
#endif
#endif
OneWire* DeviceManager::oneWireBus(uint8_t pin) {
#if !BREWPI_SIMULATE
#ifdef oneWirePin
if (pin == oneWirePin)
return &primaryOneWireBus;
#else
if (pin==beerSensorPin)
return &beerSensorBus;
if (pin==fridgeSensorPin)
return &fridgeSensorBus;
#endif
#endif
return NULL;
}
bool DeviceManager::firstDeviceOutput;
bool DeviceManager::isDefaultTempSensor(BasicTempSensor* sensor) {
return sensor==&defaultTempSensor;
}
#if EnableHumidityControlSupport
bool isEnvironmentSensorAvailable(){
return humidityControl.roomSensor != &nullEnvironmentSensor
|| humidityControl.chamberSensor != &nullEnvironmentSensor;
}
#endif
/**
* Sets devices to their unconfigured states. Each device is initialized to a static no-op instance.
* This method is idempotent, and is called each time the eeprom is reset.
*/
void DeviceManager::setupUnconfiguredDevices()
{
// right now, uninstall doesn't care about chamber/beer distinction.
// but this will need to match beer/function when multiferment is available
DeviceConfig cfg;
cfg.chamber = 1; cfg.beer = 1;
for (uint8_t i=0; i<DEVICE_MAX; i++) {
cfg.deviceFunction = DeviceFunction(i);
uninstallDevice(cfg);
}
}
/**
* Creates a new device for the given config.
*/
void* DeviceManager::createDevice(DeviceConfig& config, DeviceType dt)
{
switch (config.deviceHardware) {
case DEVICE_HARDWARE_NONE:
break;
case DEVICE_HARDWARE_PIN:
if (dt==DEVICETYPE_SWITCH_SENSOR)
#if BREWPI_SIMULATE
return new ValueSensor<bool>(false);
#else
return new DigitalPinSensor(config.hw.pinNr, config.hw.invert);
#endif
#if EnableDHTSensorSupport
else if(dt == DEVICETYPE_ENVIRONMENT_SENSOR){
// Serial.printf("create sensorType:%d, pinNr:%d\n",config.hw.humiditySensorType,config.hw.pinNr);
// calibration for temperature is in fixed_4_4 fromat
DHTSensor *dht= new DHTSensor(config.hw.pinNr,config.hw.humiditySensorType);
dht->setHumidityCalibration(tempDiffToInt(temperature(config.hw.calibration)<<(TEMP_FIXED_POINT_BITS-CALIBRATION_OFFSET_PRECISION)));
return dht;
}
#endif
else
#if BREWPI_SIMULATE
return new ValueActuator();
#else
// use hardware actuators even for simulator
return new DigitalPinActuator(config.hw.pinNr, config.hw.invert);
#endif
case DEVICE_HARDWARE_ONEWIRE_TEMP:
#if BREWPI_SIMULATE
return new ExternalTempSensor(false);// initially disconnected, so init doesn't populate the filters with the default value of 0.0
#else
return new OneWireTempSensor(oneWireBus(config.hw.pinNr), config.hw.address, config.hw.calibration);
#endif
#if BREWPI_EXTERNAL_SENSOR //vito: create sensor object
case DEVICE_HARDWARE_EXTERNAL_SENSOR:
return new WirelessTempSensor(false,config.hw.calibration);// initially disconnected, so init doesn't populate the filters with the default value of 0.0
#endif
#if EnableDHTSensorSupport
case DEVICE_HARDWARE_ENVIRONMENT_TEMP:
return new EnvTempSensor( config.hw.pinNr==0? HumidityControl::chamberSensor:HumidityControl::roomSensor,
config.hw.calibration); //VTODO
#endif
#if EnableBME280Support
case DEVICE_HARDWARE_BME280:{
Bme280Sensor* bme=new Bme280Sensor(config.hw.pinNr);
bme->setHumidityCalibration(tempDiffToInt(temperature(config.hw.calibration)<<(TEMP_FIXED_POINT_BITS-CALIBRATION_OFFSET_PRECISION)));
return bme;
}
#endif
#if BREWPI_DS2413
case DEVICE_HARDWARE_ONEWIRE_2413:
#if BREWPI_SIMULATE
if (dt==DEVICETYPE_SWITCH_SENSOR)
return new ValueSensor<bool>(false);
else
return new ValueActuator();
#else
return new OneWireActuator(oneWireBus(config.hw.pinNr), config.hw.address, config.hw.pio, config.hw.invert);
#endif
#endif
}
return NULL;
}
/**
* Returns the pointer to where the device pointer resides. This can be used to delete the current device and install a new one.
* For Temperature sensors, the returned pointer points to a TempSensor*. The basic device can be fetched by calling
* TempSensor::getSensor().
*/
inline void** deviceTarget(DeviceConfig& config)
{
// for multichamber, will write directly to the multi-chamber managed storage.
// later...
if (config.chamber>1 || config.beer>1)
return NULL;
void** ppv;
switch (config.deviceFunction) {
case DEVICE_CHAMBER_ROOM_TEMP:
ppv = (void**)&tempControl.ambientSensor;
break;
case DEVICE_CHAMBER_DOOR:
ppv = (void**)&tempControl.door;
break;
case DEVICE_CHAMBER_LIGHT:
ppv = (void**)&tempControl.light;
break;
case DEVICE_CHAMBER_HEAT:
ppv = (void**)&tempControl.heater;
break;
case DEVICE_CHAMBER_COOL:
ppv = (void**)&tempControl.cooler;
break;
case DEVICE_CHAMBER_TEMP:
ppv = (void**)&tempControl.fridgeSensor;
break;
case DEVICE_CHAMBER_FAN:
ppv = (void**)&tempControl.fan;
break;
#if AUTO_CAP
case DEVICE_BEER_CAPPER:
ppv = (void**)&autoCapControl.capper;
break;
#endif
#if EanbleParasiteTempControl
case DEVICE_PTC_COOL:
ppv = (void**)& parasiteTempController.cooler;
break;
#endif
#if EnableDHTSensorSupport
case DEVICE_CHAMBER_HUMIDITY_SENSOR:
ppv =(void**) & humidityControl.chamberSensor;
break;
case DEVICE_CHAMBER_ROOM_HUMIDITY_SENSOR:
ppv =(void**) & humidityControl.roomSensor;
break;
case DEVICE_CHAMBER_HUMIDIFIER:
ppv = (void**) & humidityControl.humidifier;
break;
case DEVICE_CHAMBER_DEHUMIDIFIER:
ppv = (void**) & humidityControl.dehumidifier;
break;
#endif
case DEVICE_BEER_TEMP:
ppv = (void**)&tempControl.beerSensor;
break;
default:
ppv = NULL;
}
return ppv;
}
// A pointer to a "temp sensor" may be a TempSensor* or a BasicTempSensor* .
// These functions allow uniform treatment.
inline bool isBasicSensor(DeviceFunction function) {
// currently only ambient sensor is basic. The others are wrapped in a TempSensor.
return function==DEVICE_CHAMBER_ROOM_TEMP;
}
inline BasicTempSensor& unwrapSensor(DeviceFunction f, void* pv) {
return isBasicSensor(f) ? *(BasicTempSensor*)pv : ((TempSensor*)pv)->sensor();
}
inline void setSensor(DeviceFunction f, void** ppv, BasicTempSensor* sensor) {
if (isBasicSensor(f))
*ppv = sensor;
else
((TempSensor*)*ppv)->setSensor(sensor);
}
/**
* Removes an installed device.
* /param config The device to remove. The fields that are used are
* chamber, beer, hardware and function.
*/
void DeviceManager::uninstallDevice(DeviceConfig& config)
{
DeviceType dt = deviceType(config.deviceFunction);
void** ppv = deviceTarget(config);
if (ppv==NULL)
return;
BasicTempSensor* s;
switch(dt) {
case DEVICETYPE_NONE:
break;
case DEVICETYPE_TEMP_SENSOR:
// sensor may be wrapped in a TempSensor class, or may stand alone.
s = &unwrapSensor(config.deviceFunction, *ppv);
if (s!=&defaultTempSensor) {
setSensor(config.deviceFunction, ppv, &defaultTempSensor);
#if FridgeSensorFallBack
if(config.deviceFunction == DEVICE_BEER_TEMP)
tempControl.fridgeSensor->setBackupSensor(&defaultTempSensor);
#endif
// DEBUG_ONLY(logInfoInt(INFO_UNINSTALL_TEMP_SENSOR, config.deviceFunction));
delete s;
}
break;
case DEVICETYPE_SWITCH_ACTUATOR:
if (*ppv!=&defaultActuator) {
// DEBUG_ONLY(logInfoInt(INFO_UNINSTALL_ACTUATOR, config.deviceFunction));
delete (Actuator*)*ppv;
*ppv = &defaultActuator;
}
break;
case DEVICETYPE_SWITCH_SENSOR:
if (*ppv!=&defaultSensor) {
// DEBUG_ONLY(logInfoInt(INFO_UNINSTALL_SWITCH_SENSOR, config.deviceFunction));
delete (SwitchSensor*)*ppv;
*ppv = &defaultSensor;
}
break;
#if EnableDHTSensorSupport
case DEVICETYPE_ENVIRONMENT_SENSOR:
if (*ppv!=&nullEnvironmentSensor) {
if( ((EnvironmentSensor*)*ppv)->sensorType() == SensorType_BME280) delete (Bme280Sensor*)*ppv;
else delete (DHTSensor*)*ppv;
*ppv = &nullEnvironmentSensor;
}
break;
#endif
}
}
/**
* Creates and installs a device in the current chamber.
*/
void DeviceManager::installDevice(DeviceConfig& config)
{
DeviceType dt = deviceType(config.deviceFunction);
void** ppv = deviceTarget(config);
if (ppv==NULL || config.hw.deactivate)
return;
BasicTempSensor* s;
TempSensor* ts;
switch(dt) {
case DEVICETYPE_NONE:
break;
case DEVICETYPE_TEMP_SENSOR:
DEBUG_ONLY(logInfoInt(INFO_INSTALL_TEMP_SENSOR, config.deviceFunction));
// sensor may be wrapped in a TempSensor class, or may stand alone.
s = (BasicTempSensor*)createDevice(config, dt);
if (*ppv==NULL){
logErrorInt(ERROR_OUT_OF_MEMORY_FOR_DEVICE, config.deviceFunction);
}
if (isBasicSensor(config.deviceFunction)) {
s->init();
*ppv = s;
}
else {
ts = ((TempSensor*)*ppv);
ts->setSensor(s);
#if FridgeSensorFallBack
if(config.deviceFunction == DEVICE_BEER_TEMP)
tempControl.fridgeSensor->setBackupSensor(s);
#endif
ts->init();
}
#if BREWPI_SIMULATE
((ExternalTempSensor*)s)->setConnected(true); // now connect the sensor after init is called
#endif
break;
#if EnableDHTSensorSupport
case DEVICETYPE_ENVIRONMENT_SENSOR:
//fall through
#endif
case DEVICETYPE_SWITCH_ACTUATOR:
case DEVICETYPE_SWITCH_SENSOR:
DEBUG_ONLY(logInfoInt(INFO_INSTALL_DEVICE, config.deviceFunction));
*ppv = createDevice(config, dt);
#if (BREWPI_DEBUG > 0)
if (*ppv==NULL)
logErrorInt(ERROR_OUT_OF_MEMORY_FOR_DEVICE, config.deviceFunction);
#endif
break;
}
}
struct DeviceDefinition {
int8_t id;
int8_t chamber;
int8_t beer;
int8_t deviceFunction;
int8_t deviceHardware;
int8_t pinNr;
int8_t invert;
int8_t pio;
int8_t deactivate;
int8_t calibrationAdjust;
int8_t hsensorType;
DeviceAddress address;
/**
* Lists the first letter of the key name for each attribute.
*/
static const char ORDER[13];
};
// the special cases are placed at the end. All others should map directly to an int8_t via atoi().
const char DeviceDefinition::ORDER[13] = "icbfhpxndjsa";
const char DEVICE_ATTRIB_INDEX = 'i';
const char DEVICE_ATTRIB_CHAMBER = 'c';
const char DEVICE_ATTRIB_BEER = 'b';
const char DEVICE_ATTRIB_FUNCTION = 'f';
const char DEVICE_ATTRIB_HARDWARE = 'h';
const char DEVICE_ATTRIB_PIN = 'p';
const char DEVICE_ATTRIB_INVERT = 'x';
const char DEVICE_ATTRIB_DEACTIVATED = 'd';
const char DEVICE_ATTRIB_ADDRESS = 'a';
#if BREWPI_DS2413
const char DEVICE_ATTRIB_PIO = 'n';
#endif
#if EnableDHTSensorSupport
const char DEVICE_ATTRIB_HUMIDITY_SENSOR_TYPE = 's';
#endif
const char DEVICE_ATTRIB_CALIBRATEADJUST = 'j'; // value to add to temp sensors to bring to correct temperature
const char DEVICE_ATTRIB_VALUE = 'v'; // print current values
const char DEVICE_ATTRIB_WRITE = 'w'; // write value to device
const char DEVICE_ATTRIB_TYPE = 't';
void handleDeviceDefinition(const char* key, const char* val, void* pv)
{
DeviceDefinition* def = (DeviceDefinition*) pv;
// logDebug("deviceDef %s:%s", key, val);
// the characters are listed in the same order as the DeviceDefinition struct.
int8_t idx = indexOf(DeviceDefinition::ORDER, key[0]);
if (key[0]==DEVICE_ATTRIB_ADDRESS)
parseBytes(def->address, val, 8);
else if (key[0]==DEVICE_ATTRIB_CALIBRATEADJUST) {
def->calibrationAdjust = fixed4_4(stringToTempDiff(val)>>(TEMP_FIXED_POINT_BITS-CALIBRATION_OFFSET_PRECISION));
}
else if (idx>=0)
((uint8_t*)def)[idx] = (uint8_t)atol(val);
}
bool inRangeUInt8(uint8_t val, uint8_t min, int8_t max) {
return min<=val && val<=max;
}
bool inRangeInt8(int8_t val, int8_t min, int8_t max) {
return min<=val && val<=max;
}
void assignIfSet(int8_t value, uint8_t* target) {
if (value>=0)
*target = (uint8_t)value;
}
#define assignIfSetMacro(value,target,type) if ((value)>=0) target =(type) value;
/**
* Updates the device definition. Only changes that result in a valid device, with no conflicts with other devices
* are allowed.
*/
void DeviceManager::parseDeviceDefinition()
{
static DeviceDefinition dev;
fill((int8_t*)&dev, sizeof(dev));
piLink.parseJson(&handleDeviceDefinition, &dev);
if (!inRangeInt8(dev.id, 0, MAX_DEVICE_SLOT)) // no device id given, or it's out of range, can't do anything else.
return;
// save the original device so we can revert
DeviceConfig target;
DeviceConfig original;
// todo - should ideally check if the eeprom is correctly initialized.
eepromManager.fetchDevice(original, dev.id);
memcpy(&target, &original, sizeof(target));
#ifndef ESP8266_ONE
piLink.print("Dev Chamber: %d, Dev Beer: %d, Dev Function: %d, Dev Hardware: %d, Dev PinNr: %d\r\n", dev.chamber, dev.beer, dev.deviceFunction, dev.deviceHardware, dev.pinNr);
piLink.print("target Chamber: %d, target Beer: %d, target Function: %d, target Hardware: %d, target PinNr: %d\r\n", target.chamber,
target.beer, target.deviceFunction, target.deviceHardware, target.hw.pinNr);
#endif
assignIfSet(dev.chamber, &target.chamber);
assignIfSet(dev.beer, &target.beer);
// assignIfSet(dev.deviceFunction, (uint8_t*)&target.deviceFunction);
// assignIfSet(dev.deviceHardware, (uint8_t*)&target.deviceHardware);
// above code woudl break when the size of enum is 4.
assignIfSetMacro(dev.deviceFunction, target.deviceFunction, DeviceFunction);
assignIfSetMacro(dev.deviceHardware, target.deviceHardware, DeviceHardware);
assignIfSet(dev.pinNr, &target.hw.pinNr);
// Serial.printf("sizeof(target.deviceFunction) =%d, sizeof(uint8_t)=%d\n",sizeof(target.deviceFunction), sizeof(uint8_t));
#if EnableDHTSensorSupport
assignIfSet(dev.hsensorType, &target.hw.humiditySensorType);
#endif
#if BREWPI_DS2413
assignIfSet(dev.pio, &target.hw.pio);
#endif
if (dev.calibrationAdjust!=-1) // since this is a union, it also handles pio for 2413 sensors
target.hw.calibration = dev.calibrationAdjust;
//assignIfSet(dev.invert, (uint8_t*)&target.hw.invert);
assignIfSetMacro(dev.invert, target.hw.invert,bool);
if (dev.address[0] != 0xFF) {// first byte is family identifier. I don't have a complete list, but so far 0xFF is not used.
#ifndef ESP8266_ONE
piLink.print("Dev Address: %s, Target Address: %s\r\n", dev.address, target.hw.address);
#endif
memcpy(target.hw.address, dev.address, 8);
}
//assignIfSet(dev.deactivate, (uint8_t*)&target.hw.deactivate);
assignIfSetMacro(dev.deactivate, target.hw.deactivate,bool);
// setting function to none clears all other fields.
if (target.deviceFunction==DEVICE_NONE) {
#ifndef ESP8266_ONE
piLink.print("Function set to NONE\r\n");
#endif
clear((uint8_t*)&target, sizeof(target));
}
bool valid = isDeviceValid(target, original, dev.id);
DeviceConfig* print = &original;
if (valid) {
print = ⌖
// remove the device associated with the previous function
uninstallDevice(original);
// also remove any existing device for the new function, since install overwrites any existing definition.
uninstallDevice(target);
installDevice(target);
eepromManager.storeDevice(target, dev.id);
}
else {
logError(ERROR_DEVICE_DEFINITION_UPDATE_SPEC_INVALID);
}
piLink.printResponse('U');
deviceManager.beginDeviceOutput();
deviceManager.printDevice(dev.id, *print, NULL);
piLink.printNewLine();
}
/**
* Determines if a given device definition is valid.
* chamber/beer must be within bounds
* device function must match the chamber/beer spec, and must not already be defined for the same chamber/beer combination
* device hardware type must be applicable with the device function
* pinNr must be unique for digital pin devices?
* pinNr must be a valid OneWire bus for one wire devices.
* for onewire temp devices, address must be unique.
* for onewire ds2413 devices, address+pio must be unique.
*/
bool DeviceManager::isDeviceValid(DeviceConfig& config, DeviceConfig& original, uint8_t deviceIndex)
{
#if 1
/* Implemented checks to ensure the system will not crash when supplied with invalid data.
More refined checks that may cause confusing results are not yet implemented. See todo below. */
/* chamber and beer within range.*/
if (!inRangeUInt8(config.chamber, 0, EepromFormat::MAX_CHAMBERS))
{
logErrorInt(ERROR_INVALID_CHAMBER, config.chamber);
return false;
}
/* 0 is allowed - represents a chamber device not assigned to a specific beer */
if (!inRangeUInt8(config.beer, 0, ChamberBlock::MAX_BEERS))
{
logErrorInt(ERROR_INVALID_BEER, config.beer);
return false;
}
if (!inRangeUInt8(config.deviceFunction, 0, DEVICE_MAX-1))
{
logErrorInt(ERROR_INVALID_DEVICE_FUNCTION, config.deviceFunction);
return false;
}
DeviceOwner owner = deviceOwner(config.deviceFunction);
if (!((owner==DEVICE_OWNER_BEER && config.beer) || (owner==DEVICE_OWNER_CHAMBER && config.chamber)
|| (owner==DEVICE_OWNER_NONE && !config.beer && !config.chamber)))
{
logErrorIntIntInt(ERROR_INVALID_DEVICE_CONFIG_OWNER, owner, config.beer, config.chamber);
return false;
}
// todo - find device at another index with the same chamber/beer/function spec.
// with duplicate function defined for the same beer, that they will both try to create/delete the device in the target location.
// The highest id will win.
DeviceType dt = deviceType(config.deviceFunction);
if (!isAssignable(dt, config.deviceHardware)) {
logErrorIntInt(ERROR_CANNOT_ASSIGN_TO_HARDWARE, dt, config.deviceHardware);
return false;
}
// todo - check pinNr uniqueness for direct digital I/O devices?
/* pinNr for a onewire device must be a valid bus. While this won't cause a crash, it's a good idea to validate this. */
if (isOneWire(config.deviceHardware)) {
if (!oneWireBus(config.hw.pinNr)) {
logErrorInt(ERROR_NOT_ONEWIRE_BUS, config.hw.pinNr);
return false;
}
}
else { // regular pin device
// todo - could verify that the pin nr corresponds to enumActuatorPins/enumSensorPins
}
#endif
// todo - for onewire temp, ensure address is unique
// todo - for onewire 2413 check address+pio nr is unique
return true;
}
void printAttrib(Print& p, char c, int8_t val, bool first=false)
{
if (!first)
p.print(',');
char tempString[32]; // resulting string limited to 128 chars
sprintf_P(tempString, PSTR("\"%c\":%d"), c, val);
p.print(tempString);
}
// I really want to buffer stuff rather than printing directly to the serial stream
void appendAttrib(String& str, char c, int8_t val, bool first = false)
{
if (!first)
str += ",";
char tempString[32]; // resulting string limited to 128 chars
sprintf_P(tempString, PSTR("\"%c\":%d"), c, val);
str += tempString;
}
inline bool hasInvert(DeviceHardware hw)
{
return hw==DEVICE_HARDWARE_PIN
#if BREWPI_DS2413
|| hw==DEVICE_HARDWARE_ONEWIRE_2413
#endif
;
}
inline bool hasOnewire(DeviceHardware hw)
{
return
#if BREWPI_DS2413
hw==DEVICE_HARDWARE_ONEWIRE_2413 ||
#endif
hw==DEVICE_HARDWARE_ONEWIRE_TEMP;
}
void DeviceManager::printDevice(device_slot_t slot, DeviceConfig& config, const char* value)
{
String deviceString;
char buf[17];
DeviceType dt = deviceType(config.deviceFunction);
if (!firstDeviceOutput) {
// p.print('\n');
// p.print(',');
deviceString = ",";
} else {
deviceString = "";
}
firstDeviceOutput = false;
deviceString += "{";
appendAttrib(deviceString, DEVICE_ATTRIB_INDEX, slot, true);
appendAttrib(deviceString, DEVICE_ATTRIB_TYPE, dt);
appendAttrib(deviceString, DEVICE_ATTRIB_CHAMBER, config.chamber);
appendAttrib(deviceString, DEVICE_ATTRIB_BEER, config.beer);
appendAttrib(deviceString, DEVICE_ATTRIB_FUNCTION, config.deviceFunction);
appendAttrib(deviceString, DEVICE_ATTRIB_HARDWARE, config.deviceHardware);
appendAttrib(deviceString, DEVICE_ATTRIB_DEACTIVATED, config.hw.deactivate);
appendAttrib(deviceString, DEVICE_ATTRIB_PIN, config.hw.pinNr);
#if EnableDHTSensorSupport
if(config.deviceFunction == DEVICE_CHAMBER_HUMIDITY_SENSOR) //VTODO: PIN device: DHT serious, DEVICE_HARDWARE_BME280
appendAttrib(deviceString,DEVICE_ATTRIB_HUMIDITY_SENSOR_TYPE,config.hw.humiditySensorType);
#endif
if (value && *value) {
deviceString += ",\"v\":";
deviceString += value;
}
if (hasInvert(config.deviceHardware))
appendAttrib(deviceString, DEVICE_ATTRIB_INVERT, config.hw.invert);
if (hasOnewire(config.deviceHardware)) {
deviceString += ",\"a\":\"";
printBytes(config.hw.address, 8, buf);
deviceString += buf;
deviceString += '"';
}
#if BREWPI_DS2413
if (config.deviceHardware==DEVICE_HARDWARE_ONEWIRE_2413) {
appendAttrib(deviceString, DEVICE_ATTRIB_PIO, config.hw.pio);
}
#endif
if (config.deviceHardware==DEVICE_HARDWARE_ONEWIRE_TEMP
#if BREWPI_EXTERNAL_SENSOR
|| config.deviceHardware==DEVICE_HARDWARE_EXTERNAL_SENSOR
#endif
#if EnableDHTSensorSupport
|| config.deviceFunction==DEVICE_CHAMBER_HUMIDITY_SENSOR //VTODO
|| config.deviceHardware==DEVICE_HARDWARE_ENVIRONMENT_TEMP
#endif
) {
tempDiffToString(buf, temperature(config.hw.calibration)<<(TEMP_FIXED_POINT_BITS-CALIBRATION_OFFSET_PRECISION), 3, 8);
deviceString += ",\"j\":";
deviceString += buf;
}
deviceString += '}';
piLink.print_P(deviceString.c_str());
}
bool DeviceManager::allDevices(DeviceConfig& config, uint8_t deviceIndex)
{
return eepromManager.fetchDevice(config, deviceIndex);
}
void parseBytes(uint8_t* data, const char* s, uint8_t len) {
char c;
while ((c=*s++)) {
uint8_t d = (c>='A' ? c-'A'+10 : c-'0')<<4;
c = *s++;
d |= (c>='A' ? c-'A'+10 : c-'0');
*data++ = d;
}
}
void printBytes(uint8_t* data, uint8_t len, char* buf) // prints 8-bit data in hex
{
for (int i=0; i<len; i++) {
uint8_t b = (data[i] >> 4) & 0x0f;
*buf++ = (b>9 ? b-10+'A' : b+'0');
b = data[i] & 0x0f;
*buf++ = (b>9 ? b-10+'A' : b+'0');
}
*buf = 0;
}
void DeviceManager::OutputEnumeratedDevices(DeviceConfig* config, void* pv)
{
DeviceOutput* out = (DeviceOutput*)pv;
printDevice(out->slot, *config, out->value);
}
bool DeviceManager::enumDevice(DeviceDisplay& dd, DeviceConfig& dc, uint8_t idx)
{
if (dd.id==-1)
return (dd.empty || dc.deviceFunction); // if enumerating all devices, honor the unused request param
else
return (dd.id==idx); // enumerate only the specific device requested
}
struct EnumerateHardware
{
int8_t hardware; // restrict the types of devices requested
int8_t pin; // pin to search
int8_t values; // fetch values for the devices.
int8_t unused; // 0 don't care about unused state, 1 unused only.
int8_t function; // restrict to devices that can be used with this function
};
void handleHardwareSpec(const char* key, const char* val, void* pv)
{
EnumerateHardware* h = (EnumerateHardware*)pv;
// logDebug("hardwareSpec %s:%s", key, val);
int8_t idx = indexOf("hpvuf", key[0]);
if (idx>=0) {
*((int8_t*)h+idx) = atol(val);
}
}
inline bool matchAddress(uint8_t* detected, uint8_t* configured, uint8_t count) {
if (!configured[0])
return true;
while (count-->0) {
if (detected[count]!=configured[count])
return false;
}
return true;
}
/**
* Find a device based on it's location.
* A device's location is:
* pinNr for simple digital pin devices
* pinNr+address for one-wire devices
* pinNr+address+pio for 2413
*/
device_slot_t findHardwareDevice(DeviceConfig& find)
{
DeviceConfig config;
for (device_slot_t slot= 0; deviceManager.allDevices(config, slot); slot++) {
if (find.deviceHardware==config.deviceHardware) {
bool match = true;
switch (find.deviceHardware) {
#if BREWPI_DS2413
case DEVICE_HARDWARE_ONEWIRE_2413:
match &= find.hw.pio==config.hw.pio;
// fall through
#endif
case DEVICE_HARDWARE_ONEWIRE_TEMP:
match &= matchAddress(find.hw.address, config.hw.address, 8);
// fall through
case DEVICE_HARDWARE_PIN:
match &= find.hw.pinNr==config.hw.pinNr;
break;
#if EnableDHTSensorSupport
case DEVICE_HARDWARE_ENVIRONMENT_TEMP:
match &= isEnvironmentSensorAvailable();
break;
#endif
#if BREWPI_EXTERNAL_SENSOR
case DEVICE_HARDWARE_EXTERNAL_SENSOR:
match &= true;
break;
#endif
default: // this should not happen - if it does the device will be returned as matching.
break;
}
if (match)
return slot;
}
}
return INVALID_SLOT;
}
inline void DeviceManager::readTempSensorValue(DeviceConfig::Hardware hw, char* out)
{
#if !BREWPI_SIMULATE
OneWire* bus = oneWireBus(hw.pinNr);
OneWireTempSensor sensor(bus, hw.address, 0); // NB: this value is uncalibrated, since we don't have the calibration offset until the device is configured
temperature temp = INVALID_TEMP;
if (sensor.init())
temp = sensor.read();
tempToString(out, temp, 3, 9);
#else
strcpy_P(out, PSTR("0.00"));
#endif
}
void DeviceManager::handleEnumeratedDevice(DeviceConfig& config, EnumerateHardware& h, EnumDevicesCallback callback, DeviceOutput& out)
{
if (h.function && !isAssignable(deviceType(DeviceFunction(h.function)), config.deviceHardware))
return; // device not applicable for required function
// logDebug("Handling device");
out.slot = findHardwareDevice(config);
DEBUG_ONLY(logInfoInt(INFO_MATCHING_DEVICE, out.slot));
if (isDefinedSlot(out.slot)) {
if (h.unused) // only list unused devices, and this one is already used
return;
// display the actual matched value
deviceManager.allDevices(config, out.slot);
}
out.value[0] = 0;
if (h.values) {
// logDebug("Fetching device value");
switch (config.deviceHardware) {
case DEVICE_HARDWARE_ONEWIRE_TEMP:
readTempSensorValue(config.hw, out.value);
break;
// unassigned pins could be input or output so we can't determine any other details from here.
// values can be read once the pin has been assigned a function
default:
break;
}
}
// logDebug("Passing device to callback");
callback(&config, &out);
}
void DeviceManager::enumeratePinDevices(EnumerateHardware& h, EnumDevicesCallback callback, DeviceOutput& output)
{
DeviceConfig config;
clear((uint8_t*)&config, sizeof(config));
config.deviceHardware = DEVICE_HARDWARE_PIN;
config.chamber = 1; // chamber 1 is default
int8_t pin;
for (uint8_t count=0; (pin=deviceManager.enumerateActuatorPins(count))>=0; count++) {
if (h.pin!=-1 && h.pin!=pin)
continue;
config.hw.pinNr = pin;
config.hw.invert = true; // make inverted default, because shiels have transistor on them
handleEnumeratedDevice(config, h, callback, output);
}
for (uint8_t count=0; (pin=deviceManager.enumerateSensorPins(count))>=0; count++) {
if (h.pin!=-1 && h.pin!=pin)
continue;
config.hw.pinNr = pin;
handleEnumeratedDevice(config, h, callback, output);
}
}
void DeviceManager::enumerateOneWireDevices(EnumerateHardware& h, EnumDevicesCallback callback, DeviceOutput& output)
{
#if !BREWPI_SIMULATE
int8_t pin;
for (uint8_t count=0; (pin=deviceManager.enumOneWirePins(count))>=0; count++) {
DeviceConfig config;
clear((uint8_t*)&config, sizeof(config));
if (h.pin!=-1 && h.pin!=pin)
continue;
config.hw.pinNr = pin;
config.chamber = 1; // chamber 1 is default
// logDebug("Enumerating one-wire devices on pin %d", pin);
OneWire* wire = oneWireBus(pin);
if (wire!=NULL) {
wire->reset_search();
while (wire->search(config.hw.address)) {
// hardware device type from OneWire family ID
switch (config.hw.address[0]) {
#if BREWPI_DS2413
case DS2413_FAMILY_ID:
config.deviceHardware = DEVICE_HARDWARE_ONEWIRE_2413;
break;
#endif
case DS18B20MODEL:
config.deviceHardware = DEVICE_HARDWARE_ONEWIRE_TEMP;
break;
default:
config.deviceHardware = DEVICE_HARDWARE_NONE;
}
switch (config.deviceHardware) {
#if BREWPI_DS2413
// for 2408 this will require iterating 0..7
case DEVICE_HARDWARE_ONEWIRE_2413:
// enumerate each pin separately
for (uint8_t i=0; i<2; i++) {
config.hw.pio = i;
handleEnumeratedDevice(config, h, callback, output);
}
break;
#endif
case DEVICE_HARDWARE_ONEWIRE_TEMP:
#if !ONEWIRE_PARASITE_SUPPORT
{ // check that device is not parasite powered
DallasTemperature sensor(wire);
if(sensor.initConnection(config.hw.address)){
handleEnumeratedDevice(config, h, callback, output);
}
}
#else
handleEnumeratedDevice(config, h, callback, output);
#endif
break;
default:
handleEnumeratedDevice(config, h, callback, output);
}
}
}
}
#endif
}
#if BREWPI_EXTERNAL_SENSOR //vito: enumerate device
void DeviceManager::enumerateExternalDevices(EnumerateHardware& h, EnumDevicesCallback callback, DeviceOutput& output){
DeviceConfig config;
clear((uint8_t*)&config, sizeof(config));