working sample

This commit is contained in:
andreask 2020-09-06 00:06:11 +02:00
parent 616a3af2c0
commit f5ae34863b
1 changed files with 75 additions and 28 deletions

View File

@ -66,7 +66,7 @@
SHT21 sensor;
#endif
#if defined HAS_ALARM || defined HAS_SHT21_ALARM || defined HAS_BME280_ALARM || defined HAS_MANNLCOUNTER
#if defined HAS_ALARM || defined HAS_SHT21_ALARM || defined HAS_BME280_ALARM
volatile boolean alarm = false;
// Get the Interrupt Mask for the selected Alarm PIN
@ -93,9 +93,47 @@
#endif
#ifdef HAS_MANNLCOUNTER
volatile uint16_t count_min = 0;
volatile uint32_t count_all = 0;
volatile unsigned long alteZeit=0, entprellZeit=20; //ms
#include <util/delay.h>
uint16_t count_min = 0;
uint32_t count_all = 0;
unsigned long lastTime = millis();
#define debounce( port, pin ) \
({ \
static uint8_t flag = 0; /* new variable on every macro usage */ \
uint8_t i = 0; \
\
if( flag ){ /* check for key release: */ \
for(;;){ /* loop ... */ \
if( !(port & 1<<pin) ){ /* ... until key pressed or ... */ \
i = 0; /* 0 = bounce */ \
break; \
} \
_delay_us( 98 ); /* * 256 = 25ms */ \
if( --i == 0 ){ /* ... until key >25ms released */ \
flag = 0; /* clear press flag */ \
i = 0; /* 0 = key release debounced */ \
break; \
} \
} \
}else{ /* else check for key press: */ \
for(;;){ /* loop ... */ \
if( (port & 1<<pin) ){ /* ... until key released or ... */ \
i = 0; /* 0 = bounce */ \
break; \
} \
_delay_us( 98 ); /* * 256 = 25ms */ \
if( --i == 0 ){ /* ... until key >25ms pressed */ \
flag = 1; /* set press flag */ \
i = 1; /* 1 = key press debounced */ \
break; \
} \
} \
} \
i; /* return value of Macro */ \
})
#endif
// Global Variable to Track Deep Sleep
@ -140,6 +178,7 @@ int16_t brightness()
}
#endif
#ifndef HAS_MANNLCOUNTER
// Setup Wakeup Interrupt Timer
void init_wdt()
{
@ -179,29 +218,20 @@ ISR(WATCHDOG_vect) {
// enable WDT interrupt
WDTCSR = (1<<WDIE)|(1<<WDP3)|(1<<WDP0);
}
#endif
#if defined HAS_ALARM || defined HAS_SHT21_ALARM || defined HAS_BME280_ALARM || defined HAS_MANNLCOUNTER
#if defined HAS_ALARM || defined HAS_SHT21_ALARM || defined HAS_BME280_ALARM
// innterrupt handler pin change
ISR(PCINT0_vect)
{
#ifdef HAS_MANNLCOUNTER //counter mode
if((millis() - alteZeit) > entprellZeit) {
// innerhalb der entprellZeit nichts machen
count_min++;
count_all++;
alteZeit = millis(); // last button push
}
#else //Alarm mode
sleep_disable();
#ifdef HAS_LED
blink(1);
#endif
alarm = true;
#endif
}
#endif
// Get Battery Voltage
int32_t readVcc() {
bitClear(PRR, PRADC);
@ -237,9 +267,11 @@ uint8_t calcEepromAddr(uint16_t framecounter) {
void setup()
{
// Initialize Sleep Timer
init_wdt();
PRR = bit(PRTIM1);
#ifndef HAS_MANNLCOUNTER
// Initialize Sleep Timer
init_wdt();
PRR = bit(PRTIM1);
#endif
#ifdef RF_LORA
// Setup LoraWAN
@ -281,7 +313,7 @@ void setup()
radio.sleep();
#endif
#if defined HAS_ALARM || defined HAS_SHT21_ALARM || defined HAS_BME280_ALARM || defined HAS_MANNLCOUNTER
#if defined HAS_ALARM || defined HAS_SHT21_ALARM || defined HAS_BME280_ALARM
MCUCR = (MCUCR & ~(bit(ISC01)|bit(ISC00))) | bit(ISC01); // fallende Flanke
GIMSK = (1<<PCIE0);
PCMSK0 = (1<<ALARM_INT);
@ -388,11 +420,6 @@ void loop()
alarm = false;
#endif
#ifdef HAS_MANNLCOUNTER
data.count_all=count_all;
data.count_min=count_min;
#endif
#if defined DS18B20_PIN
#ifdef DS18B20_POWER
@ -411,6 +438,27 @@ void loop()
#endif
#endif
#ifdef HAS_MANNLCOUNTER
pinMode(ALARM_PIN, INPUT_PULLUP);
// FIXME Timer interrupt
while(millis() - lastTime < 60000){
if( debounce( PINA, PA0 ) ){
count_min++;
count_all++;
digitalWrite(LED_PIN, !digitalRead(LED_PIN));
}
}
lastTime = millis();
data.count_all=count_all;
data.count_min=count_min;
count_min=0;
// nach 60 sek
#endif
// Add Battery Voltage, 20mv steps, encoded into 1 Byte
uint32_t batv = readVcc();
data.bat = (uint8_t)(batv/20);
@ -436,9 +484,6 @@ void loop()
#else
// Send LoRa Packet, Increment Frame Counter
lora.Send_Data((unsigned char *)&data, sizeof(data), Frame_Counter_Tx, SF7BW125, 0x01);
#ifdef HAS_MANNLCOUNTER
count_min = 0;
#endif
#endif
// Save the next FrameCounter to EEPROM
Frame_Counter_Tx++;
@ -455,6 +500,8 @@ void loop()
digitalWrite(LED_PIN, 0);
#endif
#ifndef HAS_MANNLCOUNTER
// Sleep until next Measurement
sleep(SLEEP_TIME);
#endif
}