From 9ea722607f3fe4b4139081d239c970280c2d26e8 Mon Sep 17 00:00:00 2001 From: Martin Preuss Date: Mon, 23 Jun 2025 19:21:49 +0200 Subject: [PATCH] avr: added brightness sensor --- avr/apps/reportsensors/main.asm | 26 ++++--- avr/devices/all/includes.asm | 8 ++ avr/devices/all/main.asm | 10 +++ avr/devices/all/modules.asm | 4 + avr/devices/n24/defs.asm | 11 +++ avr/devices/n24/main/main.asm | 3 + avr/devices/n26/defs.asm | 3 +- avr/modules/0BUILD | 1 + avr/modules/brightness/0BUILD | 12 +++ avr/modules/brightness/main.asm | 130 ++++++++++++++++++++++++++++++++ avr/modules/brightness/send.asm | 31 ++++++++ avr/version.asm | 2 +- 12 files changed, 229 insertions(+), 12 deletions(-) create mode 100644 avr/modules/brightness/0BUILD create mode 100644 avr/modules/brightness/main.asm create mode 100644 avr/modules/brightness/send.asm diff --git a/avr/apps/reportsensors/main.asm b/avr/apps/reportsensors/main.asm index 027da1f..51b9a25 100644 --- a/avr/apps/reportsensors/main.asm +++ b/avr/apps/reportsensors/main.asm @@ -95,10 +95,8 @@ AppReportSensors_OnEverySecond_store: #ifdef MODULES_SGP30 cpi r16, 53 - breq AppReportSensors_OnEverySecond_measureValue5 - cpi r16, 63 breq AppReportSensors_OnEverySecond_sendValue5 - cpi r16, 73 + cpi r16, 63 breq AppReportSensors_OnEverySecond_sendValue6 #endif @@ -114,6 +112,11 @@ AppReportSensors_OnEverySecond_store: breq AppReportSensors_OnEverySecond_sendCCS811_CO2 #endif +#ifdef MODULES_BRIGHTNESS + cpi r16, 97 + breq AppReportSensors_OnEverySecond_sendBrightness +#endif + ret #ifdef MODULES_SI7021 @@ -140,13 +143,11 @@ AppReportSensors_OnEverySecond_sendValue3: #endif #ifdef MODULES_SGP30 - AppReportSensors_OnEverySecond_measureValue5: - rjmp SGP30_Measure - AppReportSensors_OnEverySecond_sendValue5: - rjmp SGP30_SendTVOC - ret - AppReportSensors_OnEverySecond_sendValue6: - rjmp SGP30_SendCO2 + AppReportSensors_OnEverySecond_sendValue5: + rjmp SGP30_SendTVOC + ret + AppReportSensors_OnEverySecond_sendValue6: + rjmp SGP30_SendCO2 #endif #ifdef MODULES_CCS811 @@ -156,6 +157,11 @@ AppReportSensors_OnEverySecond_sendCCS811_CO2: rjmp CCS811_SendCO2 #endif +#ifdef MODULES_BRIGHTNESS +AppReportSensors_OnEverySecond_sendBrightness: + rjmp Brightness_Send +#endif + ; @end diff --git a/avr/devices/all/includes.asm b/avr/devices/all/includes.asm index 3d6df0b..ad3a6a2 100644 --- a/avr/devices/all/includes.asm +++ b/avr/devices/all/includes.asm @@ -195,6 +195,14 @@ #endif +#ifdef MODULES_BRIGHTNESS +.include "modules/brightness/main.asm" + #ifdef MODULES_NETWORK + .include "modules/brightness/send.asm" + #endif +#endif + + #ifdef APPS_MOTION .include "modules/f_keepup/main.asm" .include "modules/valsched/main.asm" diff --git a/avr/devices/all/main.asm b/avr/devices/all/main.asm index 694afe7..522c148 100644 --- a/avr/devices/all/main.asm +++ b/avr/devices/all/main.asm @@ -112,6 +112,11 @@ onSystemTimerTick: #endif +#ifdef MODULES_BRIGHTNESS + bigcall Brightness_Every100ms +#endif + + #ifdef APPS_NETWORK ldi yl, LOW(netInterfaceData) ldi yh, HIGH(netInterfaceData) @@ -145,6 +150,11 @@ sysOnEverySecond: #ifdef APPS_REPORTSENSORS bigcall AppReportSensors_OnEverySecond #endif + +#ifdef MODULES_SGP30 + bigcall SGP30_EverySecond +#endif + bigjmp onEverySecond ; @end diff --git a/avr/devices/all/modules.asm b/avr/devices/all/modules.asm index bb63e77..6823fe7 100644 --- a/avr/devices/all/modules.asm +++ b/avr/devices/all/modules.asm @@ -148,6 +148,10 @@ initModules: bigcall ILI9341_Init #endif +#ifdef MODULES_BRIGHTNESS + bigcall Brightness_Init +#endif + ; done ret diff --git a/avr/devices/n24/defs.asm b/avr/devices/n24/defs.asm index 34ceb17..d1b401f 100644 --- a/avr/devices/n24/defs.asm +++ b/avr/devices/n24/defs.asm @@ -146,3 +146,14 @@ +; --------------------------------------------------------------------------- +; Brightness + + +.equ BRIGHTNESS_ADC_PORT = PORTA ; adc0 +.equ BRIGHTNESS_ADC_DDR = DDRA +.equ BRIGHTNESS_ADC_PIN = PORTA0 +.equ BRIGHTNESS_ADC_MUX = 0 + + + diff --git a/avr/devices/n24/main/main.asm b/avr/devices/n24/main/main.asm index 2b0b7b7..7a1e810 100644 --- a/avr/devices/n24/main/main.asm +++ b/avr/devices/n24/main/main.asm @@ -73,6 +73,7 @@ ;#define MODULES_DS18B20 #define MODULES_MOTION ;#define MODULES_CCS811 +#define MODULES_BRIGHTNESS #define APPS_NETWORK #define APPS_MOTION @@ -96,6 +97,8 @@ .equ VALUE_ID_SGP30_TVOC = 0x09 .equ VALUE_ID_SGP30_CO2 = 0x0a +.equ VALUE_ID_BRIGHTNESS = 0x0b + ;.equ VALUE_ID_REED_CONF = 0x81 .equ VALUE_ID_DEBUG = 0x7f diff --git a/avr/devices/n26/defs.asm b/avr/devices/n26/defs.asm index 04cc3d0..8f27140 100644 --- a/avr/devices/n26/defs.asm +++ b/avr/devices/n26/defs.asm @@ -13,7 +13,7 @@ ; AtTiny84 ; -------- ; VCC 1 14 GND -; PB0 2 13 PA0 +; PB0 2 13 PA0 Brightness ; PB1 3 12 PA1 COM-DATA ; /RESET PB3 4 11 PA2 ; PB2 5 10 PA3 LED @@ -96,3 +96,4 @@ + diff --git a/avr/modules/0BUILD b/avr/modules/0BUILD index d2ca36a..6b7dfdf 100644 --- a/avr/modules/0BUILD +++ b/avr/modules/0BUILD @@ -37,6 +37,7 @@ valsched xram heap + brightness diff --git a/avr/modules/brightness/0BUILD b/avr/modules/brightness/0BUILD new file mode 100644 index 0000000..c3dec70 --- /dev/null +++ b/avr/modules/brightness/0BUILD @@ -0,0 +1,12 @@ + + + + + + main.asm + send.asm + + + + + diff --git a/avr/modules/brightness/main.asm b/avr/modules/brightness/main.asm new file mode 100644 index 0000000..49d9e11 --- /dev/null +++ b/avr/modules/brightness/main.asm @@ -0,0 +1,130 @@ +; *************************************************************************** +; copyright : (C) 2025 by Martin Preuss +; email : martin@libchipcard.de +; +; *************************************************************************** +; * This file is part of the project "AqHome". * +; * Please see toplevel file COPYING of that project for license details. * +; *************************************************************************** + + + +.equ BRIGHTNESS_INTERVAL = 97 + +.equ BRIGHTNESS_FLAGS_VALID_BIT = 7 + + +; *************************************************************************** +; data + +.dseg + +brightnessDataBegin: + brightnessTimer: .byte 1 + brightnessFlags: .byte 1 + brightnessLastValue: .byte 1 +brightnessDataEnd: + + + + +; *************************************************************************** +; code + +.cseg + + + +; --------------------------------------------------------------------------- +; @routine Brightness_Init +; +; Init module. +; +; @return CFLAG always set + +Brightness_Init: + ; preset SRAM data area + ldi xh, HIGH(brightnessDataBegin) + ldi xl, LOW(brightnessDataBegin) + clr r16 + ldi r17, (brightnessDataEnd-brightnessDataBegin) + rcall Utils_FillSram + + ; setup pins + cbi BRIGHTNESS_ADC_PORT, BRIGHTNESS_ADC_PIN ; disable internal pullup for ADC + cbi BRIGHTNESS_ADC_DDR, BRIGHTNESS_ADC_PIN ; set ADC port as input + + ldi r16, BRIGHTNESS_ADC_MUX ; select input pin, use Vcc as reference voltage + out ADMUX, r16 + ldi r16, (1 << ADLAR) + out ADCSRB, r16 + ldi r16, (1 << ADEN) | (1 << ADPS1) | (1 << ADPS0) ; enable, prescaler 8 + out ADCSRA, r16 + + ldi r16, BRIGHTNESS_INTERVAL + sts brightnessTimer, r16 + + sec + ret + + + +Brightness_Fini: + sec + ret + + + +; --------------------------------------------------------------------------- +; @routine Brightness_Every100ms @global +; + +Brightness_Every100ms: + lds r16, brightnessTimer + dec r16 + breq Brightness_Every100ms_readValue + sts brightnessTimer, r16 + cpi r16, 1 + breq Brightness_Every100ms_startMeasure + ret +Brightness_Every100ms_startMeasure: + sbi ADCSRA, ADSC ; start conversion + ret +Brightness_Every100ms_readValue: + sbic ADCSRA, ADSC + ret ; return if bit still set, leave brightnessTimer at "1" + ; conversion complete, read value + ldi r16, BRIGHTNESS_INTERVAL ; restart timer + sts brightnessTimer, r16 + in r16, ADCH ; read value from ADC + sts brightnessLastValue, r16 + ; convert to 1/0 + lds r17, brightnessFlags + sbr r17, (1<