Page MenuHomePhabricator

No OneTemporary

diff --git a/lightSensor-daemon.c b/lightSensor-daemon.c
--- a/lightSensor-daemon.c
+++ b/lightSensor-daemon.c
@@ -1,314 +1,341 @@
/* lightSensor-daemon for DCS-932L by Andreas Boehler <andreas _AT_ aboehler.at>
* Copyright (c) 2014, Andreas Boehler
* based on GPIO example,
* Copyright (c) 2011, RidgeRun
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the RidgeRun.
* 4. Neither the name of the RidgeRun nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY RIDGERUN ''AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL RIDGERUN BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <poll.h>
/****************************************************************
* Constants
****************************************************************/
#define SYSFS_GPIO_DIR "/sys/class/gpio"
#define POLL_TIMEOUT (3 * 1000) /* 3 seconds */
#define MAX_BUF 64
#define GPIO_LIGHT 11
-#define GPIO_IR_FILTER 14
-#define GPIO_IR_LED 12
+#define GPIO_IR_PIN1 12
+#define GPIO_IR_PIN2 14
/****************************************************************
* gpio_export
****************************************************************/
int gpio_export(unsigned int gpio)
{
int fd, len;
char buf[MAX_BUF];
fd = open(SYSFS_GPIO_DIR "/export", O_WRONLY);
if (fd < 0) {
perror("gpio/export");
return fd;
}
len = snprintf(buf, sizeof(buf), "%d", gpio);
write(fd, buf, len);
close(fd);
return 0;
}
/****************************************************************
* gpio_unexport
****************************************************************/
int gpio_unexport(unsigned int gpio)
{
int fd, len;
char buf[MAX_BUF];
fd = open(SYSFS_GPIO_DIR "/unexport", O_WRONLY);
if (fd < 0) {
perror("gpio/export");
return fd;
}
len = snprintf(buf, sizeof(buf), "%d", gpio);
write(fd, buf, len);
close(fd);
return 0;
}
/****************************************************************
* gpio_set_dir
****************************************************************/
int gpio_set_dir(unsigned int gpio, unsigned int out_flag)
{
int fd, len;
char buf[MAX_BUF];
len = snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/direction", gpio);
fd = open(buf, O_WRONLY);
if (fd < 0) {
perror("gpio/direction");
return fd;
}
if (out_flag)
write(fd, "out", 4);
else
write(fd, "in", 3);
close(fd);
return 0;
}
/****************************************************************
* gpio_set_value
****************************************************************/
int gpio_set_value(unsigned int gpio, unsigned int value)
{
int fd, len;
char buf[MAX_BUF];
len = snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio);
fd = open(buf, O_WRONLY);
if (fd < 0) {
perror("gpio/set-value");
return fd;
}
if (value)
write(fd, "1", 2);
else
write(fd, "0", 2);
close(fd);
return 0;
}
/****************************************************************
* gpio_get_value
****************************************************************/
int gpio_get_value(unsigned int gpio, unsigned int *value)
{
int fd, len;
char buf[MAX_BUF];
char ch;
len = snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio);
fd = open(buf, O_RDONLY);
if (fd < 0) {
perror("gpio/get-value");
return fd;
}
read(fd, &ch, 1);
if (ch != '0') {
*value = 1;
} else {
*value = 0;
}
close(fd);
return 0;
}
/****************************************************************
* gpio_set_edge
****************************************************************/
int gpio_set_edge(unsigned int gpio, char *edge)
{
int fd, len;
char buf[MAX_BUF];
len = snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/edge", gpio);
fd = open(buf, O_WRONLY);
if (fd < 0) {
perror("gpio/set-edge");
return fd;
}
write(fd, edge, strlen(edge) + 1);
close(fd);
return 0;
}
/****************************************************************
* gpio_fd_open
****************************************************************/
int gpio_fd_open(unsigned int gpio)
{
int fd, len;
char buf[MAX_BUF];
len = snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio);
fd = open(buf, O_RDONLY | O_NONBLOCK );
if (fd < 0) {
perror("gpio/fd_open");
}
return fd;
}
/****************************************************************
* gpio_fd_close
****************************************************************/
int gpio_fd_close(int fd)
{
return close(fd);
}
/****************************************************************
* Main
+ * There are two GPIOs for the IR LED/Filter control.
+ * They are coded, meaning:
+ * 0 0 .. IR LEDs ON, filter OUT
+ * 0 1 .. IR LEDs ON, no active filter positioning
+ * 1 0 .. IR LEDs OFF, no active filter positioning
+ * 1 1 .. IR LEDs OFF, filter IN
+ *
+ * In order to reduce heat, the ICR positioning should be
+ * stopped after some time. Periodic repositioning of the filter
+ * is scheduled.
****************************************************************/
int main(int argc, char **argv, char **envp)
{
struct pollfd fdset[1];
int nfds = 1;
int gpio_fd, timeout, rc, count;
char ch;
int dayMode = 0;
unsigned int gpio;
+ int releaseICR = 0;
gpio_export(GPIO_LIGHT);
- gpio_export(GPIO_IR_LED);
- gpio_export(GPIO_IR_FILTER);
+ gpio_export(GPIO_IR_PIN1);
+ gpio_export(GPIO_IR_PIN2);
gpio_set_dir(GPIO_LIGHT, 0);
- gpio_set_dir(GPIO_IR_FILTER, 1);
- gpio_set_dir(GPIO_IR_LED, 1);
+ gpio_set_dir(GPIO_IR_PIN1, 1);
+ gpio_set_dir(GPIO_IR_PIN2, 1);
gpio_fd = gpio_fd_open(GPIO_LIGHT);
gpio_get_value(GPIO_LIGHT, &dayMode);
timeout = POLL_TIMEOUT;
count = 0; // Set to 0 for initial setup based on initial read of light sensor
while (1) {
memset((void*)fdset, 0, sizeof(fdset));
if(count == 0)
{
- count = (300 * 1000) / POLL_TIMEOUT;
+ count = (300 * 1000) / POLL_TIMEOUT; // 5 minutes
if(dayMode)
{
- gpio_set_value(GPIO_IR_LED, 0);
- gpio_set_value(GPIO_IR_FILTER, 1);
+ gpio_set_value(GPIO_IR_PIN1, 1);
+ gpio_set_value(GPIO_IR_PIN2, 1);
}
else
{
- gpio_set_value(GPIO_IR_FILTER, 0);
- gpio_set_value(GPIO_IR_LED, 1);
+ gpio_set_value(GPIO_IR_PIN1, 0);
+ gpio_set_value(GPIO_IR_PIN2, 0);
}
+ releaseICR = 1;
}
fdset[1].fd = gpio_fd;
fdset[1].events = POLLIN;
rc = poll(fdset, nfds, timeout);
if (rc < 0) {
printf("\npoll() failed!\n");
return -1;
}
if (rc == 0) { // timeout
printf(".");
count--;
+ if(releaseICR)
+ {
+ if(dayMode)
+ {
+ gpio_set_value(GPIO_IR_PIN1, 1);
+ gpio_set_value(GPIO_IR_PIN2, 0);
+ }
+ else
+ {
+ gpio_set_value(GPIO_IR_PIN1, 0);
+ gpio_set_value(GPIO_IR_PIN2, 1);
+ }
+ releaseICR = 0;
+ }
}
if (fdset[0].revents & POLLIN) {
read(fdset[1].fd, &ch, 1);
if(ch != '0')
{
printf("Light sensor reported day\n");
// Light sensor went high -> day
if(!dayMode)
{
- gpio_set_value(GPIO_IR_LED, 0);
- gpio_set_value(GPIO_IR_FILTER, 1);
+ gpio_set_value(GPIO_IR_PIN1, 1);
+ gpio_set_value(GPIO_IR_PIN2, 1);
dayMode = 1;
}
}
else
{
printf("Light sensor reported night\n");
// Light sensor went low -> night
if(dayMode)
{
- gpio_set_value(GPIO_IR_FILTER, 0);
- gpio_set_value(GPIO_IR_LED, 1);
+ gpio_set_value(GPIO_IR_PIN1, 0);
+ gpio_set_value(GPIO_IR_PIN2, 0);
dayMode = 0;
}
}
+ releaseICR = 1;
}
}
gpio_fd_close(gpio_fd);
return 0;
}

File Metadata

Mime Type
text/x-diff
Expires
Tue, Dec 24, 5:59 PM (1 d, 15 h ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
532487
Default Alt Text
(9 KB)

Event Timeline