thermferm/devices.c

changeset 392
034746506c3d
parent 362
c92651a54969
child 393
220f2f30bf68
equal deleted inserted replaced
391:a89b3ac7e97b 392:034746506c3d
166 } 166 }
167 167
168 #endif 168 #endif
169 169
170 170
171 /*
172 * Read one byte from a 1-wire device like a DS2413
173 */
171 int read_w1(char *address, char *file) 174 int read_w1(char *address, char *file)
172 { 175 {
173 char *addr = NULL; 176 char *addr = NULL;
174 int fn = -1, rc = -1, retries = 5; 177 int fn = -1, rc = -1, retries = 5;
175 uint8_t val; 178 uint8_t val;
185 188
186 while (retries--) { 189 while (retries--) {
187 if ((read(fn, &val, 1)) == 1) { 190 if ((read(fn, &val, 1)) == 1) {
188 rc = (int)val; 191 rc = (int)val;
189 goto leave; 192 goto leave;
190 // } else {
191 // syslog(LOG_NOTICE, "read_w1() read %s try=%d: %s", addr, retries, strerror(errno));
192 } 193 }
193 } 194 }
194 syslog(LOG_NOTICE, "read_w1() read %s fatal: %s", addr, strerror(errno)); 195 syslog(LOG_NOTICE, "read_w1() read %s fatal: %s", addr, strerror(errno));
195 196
196 } else { 197 } else {
212 return rc; 213 return rc;
213 } 214 }
214 215
215 216
216 217
218 /*
219 * Write a byte to a 1-wire device like a DS2413
220 */
217 int write_w1(char *address, char *file, uint8_t val) 221 int write_w1(char *address, char *file, uint8_t val)
218 { 222 {
219 char *addr = NULL; 223 char *addr = NULL;
220 int fn = -1, rc = -1, retries = 5; 224 int fn = -1, rc = -1, retries = 5;
221 225
384 piUnlock(LOCK_DEVICES); 388 piUnlock(LOCK_DEVICES);
385 #endif 389 #endif
386 390
387 return 0; 391 return 0;
388 } 392 }
393
394
395 /*
396 * Returns 0 if failed.
397 * Returns 1 if success, value contains new value.
398 */
399 int device_in(char *uuid, int *value)
400 {
401 devices_list *device;
402
403 if (uuid == NULL)
404 return 0;
405
406 #ifdef HAVE_WIRINGPI_H
407 piLock(LOCK_DEVICES);
408 #endif
409
410 for (device = Config.devices; device; device = device->next) {
411 if (! strcmp(uuid, device->uuid)) {
412
413 }
414 }
415
416 #ifdef HAVE_WIRINGPI_H
417 piUnlock(LOCK_DEVICES);
418 #endif
419
420 return 0;
421 }
422
389 423
390 424
391 425
392 /* 426 /*
393 * Auto detect hotplugged or known to be present devices 427 * Auto detect hotplugged or known to be present devices
519 ndev->uuid = malloc(37); 553 ndev->uuid = malloc(37);
520 uuid_generate(uu); 554 uuid_generate(uu);
521 uuid_unparse(uu, ndev->uuid); 555 uuid_unparse(uu, ndev->uuid);
522 ndev->type = DEVTYPE_GPIO; 556 ndev->type = DEVTYPE_GPIO;
523 ndev->value = digitalRead(pin); 557 ndev->value = digitalRead(pin);
558 ndev->offset = 0;
524 ndev->present = DEVPRESENT_YES; 559 ndev->present = DEVPRESENT_YES;
525 ndev->address = xstrcpy((char *)"GPIO"); 560 ndev->address = xstrcpy((char *)"GPIO");
526 snprintf(buf, 39, "Raspberry GPIO %d", i); 561 snprintf(buf, 39, "Raspberry GPIO %d", i);
527 ndev->description = xstrcpy(buf); 562 ndev->description = xstrcpy(buf);
528 ndev->subdevice = i; 563 ndev->subdevice = i;
846 break; 881 break;
847 882
848 #ifdef HAVE_WIRINGPI_H 883 #ifdef HAVE_WIRINGPI_H
849 case DEVTYPE_DHT: 884 case DEVTYPE_DHT:
850 /* 885 /*
851 * Make sure we don't read the sensor withing 2 seconds. 886 * Make sure we don't read the sensor within 2 seconds.
852 */ 887 */
853 now = time(NULL); 888 now = time(NULL);
854 if ((int)(now - dht11_last) > 2) { 889 if ((int)(now - dht11_last) > 2) {
855 dht11_pin = device->gpiopin; 890 dht11_pin = device->gpiopin;
856 dht11Read(); 891 dht11Read();
876 device->present = DEVPRESENT_ERROR; 911 device->present = DEVPRESENT_ERROR;
877 } 912 }
878 piUnlock(LOCK_DEVICES); 913 piUnlock(LOCK_DEVICES);
879 } 914 }
880 break; 915 break;
916
917 case DEVTYPE_GPIO:
918 if (device->direction == DEVDIR_IN_BIN) {
919 piLock(LOCK_DEVICES);
920 device->value = digitalRead(device->gpiopin);
921 device->offset = 0;
922 device->timestamp = time(NULL);
923 piUnlock(LOCK_DEVICES);
924 }
925 break;
926
881 #endif 927 #endif
882 #ifdef USE_SIMULATOR 928 #ifdef USE_SIMULATOR
883 case DEVTYPE_SIM: 929 case DEVTYPE_SIM:
884 #ifdef HAVE_WIRINGPI_H 930 #ifdef HAVE_WIRINGPI_H
885 piLock(LOCK_DEVICES); 931 piLock(LOCK_DEVICES);

mercurial