crashtest-r0ket/firmware/basic/night.c

48 lines
904 B
C
Raw Normal View History

#include <sysinit.h>
#include "basic/basic.h"
2011-07-31 20:13:06 +00:00
#define SAMPCT (4)
uint32_t light=300*SAMPCT;
char _isnight=1;
2011-07-31 20:13:06 +00:00
#define threshold GLOBAL(daytrig)
#define RANGE GLOBAL(daytrighyst)
void LightCheck(void){
int iocon;
char iodir;
int value;
iocon=IOCON_PIO1_11;
// iodir=gpioGetDir(RB_LED3);
iodir= (GPIO_GPIO1DIR & (1 << (RB_LED3) ))?1:0;
gpioSetDir(RB_LED3, gpioDirection_Input);
IOCON_PIO1_11 = IOCON_PIO1_11_FUNC_AD7|IOCON_PIO1_11_ADMODE_ANALOG;
2011-07-31 20:13:06 +00:00
light-=light/SAMPCT;
light += (adcRead(7)/2);
gpioSetDir(RB_LED3, iodir);
IOCON_PIO1_11=iocon;
if(threshold==0){ // uninitialized?
threshold=320;
};
2011-07-31 20:13:06 +00:00
if(_isnight && light/SAMPCT>(threshold+RANGE))
_isnight=0;
2011-07-31 20:13:06 +00:00
if(!_isnight && light/SAMPCT<threshold)
_isnight=1;
};
uint32_t GetLight(void){
2011-07-31 20:13:06 +00:00
return light/SAMPCT;
};
char isNight(void){
return _isnight;
};