Last active
December 6, 2018 19:49
-
-
Save msloth/77ddae6841d7ebd6bdbb3a9480cc9d09 to your computer and use it in GitHub Desktop.
When light is on, also turns on a GPIO (DIO7). Light output via PWM on DIO5. Has a default schedule (see code).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "thsq.h" | |
#include "ti-lib.h" | |
#include "gpio-interrupt.h" | |
#include "lib/sensors.h" | |
#include "batmon-sensor.h" | |
#include "dev/leds-arch.h" | |
#include "dev/cc26xx-uart.h" | |
/*---------------------------------------------------------------------------*/ | |
/* defines to help set default schedules */ | |
#define MON (1 << 0) | |
#define TUE (1 << 1) | |
#define WED (1 << 2) | |
#define THU (1 << 3) | |
#define FRI (1 << 4) | |
#define SAT (1 << 5) | |
#define SUN (1 << 6) | |
#define WEEKDAYS (MON | TUE | WED | THU | FRI) | |
#define WEEKEND (SAT | SUN) | |
#define ALL_WEEK (WEEKDAYS | WEEKEND) | |
#define LEN_PER_DEFAULT_SCHEDULE 7 /* if RGB, then each schedule entry is 7 bytes */ | |
/*---------------------------------------------------------------------------*/ | |
void | |
init_leds(void) | |
{ | |
leds_arch_set_pins(IOID_6, IOID_7, IOID_UNUSED); | |
} | |
/*---------------------------------------------------------------------------*/ | |
void | |
init_uart(void) | |
{ | |
cc26xx_uart_init(IOID_3, IOID_2, IOID_UNUSED, IOID_UNUSED); | |
} | |
/*---------------------------------------------------------------------------*/ | |
static void | |
callback_thsq(enum thsq_reason r, const char *msg, int len) | |
{ | |
if(r == THSQ_PERIOD) { | |
int value; | |
value = batmon_sensor.value(BATMON_SENSOR_TYPE_VOLT); | |
thsq_sset_printf("b", "%d.%03d", value / 1000, value % 1000); | |
thsq_sset("t", batmon_sensor.value(BATMON_SENSOR_TYPE_TEMP)); | |
thsq_push(); | |
} | |
} | |
/*---------------------------------------------------------------------------*/ | |
static void | |
lighting_cb(const uint8_t *rgb, int len, const char *reason) | |
{ | |
printf("lighting %u, %s\n", rgb[0], reason); | |
if(rgb != NULL && len > 0) { | |
if(rgb[0] == 0 || rgb[0] > 100) { | |
/* turn off GPIO */ | |
thsq_gpio_set_gpio(0); | |
} else if(rgb[0] <= 100) { | |
/* turn on GPIO */ | |
thsq_gpio_set_gpio(1); | |
} | |
} | |
} | |
/*---------------------------------------------------------------------------*/ | |
void | |
app(void) | |
{ | |
/* Initialize the lighting module */ | |
thsq_lighting_init(); | |
thsq_lighting_set_lamp(); | |
static struct thsq_lighting_callback tlcb; | |
thsq_lighting_add_callback(&tlcb, lighting_cb); | |
/* Initialize the configurable GPIO module */ | |
thsq_gpio_init(); | |
/* | |
* GPIO defaults | |
* aabbccddD | |
* | |
* aa = gpio out | |
* bb = adc in | |
* cc = pwm out | |
* dd = gpio in, D = U/D/N (pullup/down/none) | |
* aa/bb/cc/dd are zero-padded integers corresponding to IOID_n, or -- for none/remove | |
*/ | |
thsq_gpio_set_default("07--05--N"); | |
// thsq_gpio_set_default("07230515D"); | |
// thsq_gpio_trigger_pir_on_gpio(1); | |
/* Make the GPIO module listen to events from the lighting module. */ | |
thsq_gpio_attach_lighting(); | |
/* default schedule, set | |
50% at 0800 mon-fri | |
0% at 1015 mon-fri | |
75% at 1200 mon-fri | |
0% at 1700 mon-fri (ie off during the weekend) | |
*/ | |
uint8_t default_sch[] = {'w',WEEKDAYS,8,0,50,50,50, | |
'w',WEEKDAYS,10,15,0,0,0, | |
'w',WEEKDAYS,12,0,75,75,75, | |
'w',WEEKDAYS,17,0,0,0,0}; | |
thsq_schedule_set_default(default_sch, LEN_PER_DEFAULT_SCHEDULE * 4); | |
/* Restore lighting settings from before last reboot. This must be | |
called after attaching the GPIO module to the lighting module to | |
make the lighting settings be transported to the GPIO module. */ | |
thsq_lighting_apply_last(); | |
/* Setup the periodic callback for the temperature sensor */ | |
static struct thsq_callback cb; | |
thsq_add_callback(&cb, callback_thsq); | |
SENSORS_ACTIVATE(batmon_sensor); | |
} | |
/*---------------------------------------------------------------------------*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment