From 8904d337894b0da22af1856c8e3012b1fe78506b Mon Sep 17 00:00:00 2001 From: Martin Preuss Date: Mon, 28 Oct 2024 23:44:34 +0100 Subject: [PATCH] avr: implemented motion detector module. --- avr/main.asm | 8 +++ avr/modules/motion/main.asm | 101 ++++++++++++++++++++++++------------ 2 files changed, 77 insertions(+), 32 deletions(-) diff --git a/avr/main.asm b/avr/main.asm index 632b591..3a2780d 100644 --- a/avr/main.asm +++ b/avr/main.asm @@ -154,6 +154,10 @@ initModules: rcall SK6812_Init #endif +#ifdef MODULES_MOTION + rcall Motion_Init +#endif + ; done ret @@ -203,6 +207,10 @@ runModules_ComEnd: rcall Ds18b20_Run #endif +#ifdef MODULES_MOTION + rcall Motion_Run +#endif + ; add more modules here ret diff --git a/avr/modules/motion/main.asm b/avr/modules/motion/main.asm index e28d054..d80c816 100644 --- a/avr/modules/motion/main.asm +++ b/avr/modules/motion/main.asm @@ -7,42 +7,14 @@ ; * Please see toplevel file COPYING of that project for license details. * ; *************************************************************************** -; - check motion -; - motion detected? -; - yes: -; - set motion HW bit -; - reset motion timeout counter -; - SW bit set? -; - no: -; - report motion -; - reset motionReportTimeout (use lower value to repeat motion message once) -; - set motion SW bit -; - yes -; - dec motionReportTimeout -; - 0? -; - report current SW state -; - reset motionReportTimeout (use higher value since motion should already be known) -; - no: -; - clear motion hw bit -; - dec motionTimeout -; - 0? -; - yes: -; - clear SW motion bit -; - report no motion - - - -.equ MOTION_STATE_MOTION_HW_BIT = 0 -.equ MOTION_STATE_MOTION_SW_BIT = 1 +.equ MOTION_TIMER_REPORT = 30 ; report motion at most every 3s .dseg motionDataBegin: -motionState: .byte 1 -motionTimeout: .byte 1 -motionReportTimeout: .byte 1 +motionTimer: .byte 1 motionDataEnd: @@ -50,12 +22,77 @@ motionDataEnd: .cseg + + Motion_Init: + ; preset SRAM data area + ldi xh, HIGH(motionDataBegin) + ldi xl, LOW(motionDataBegin) + clr r16 + ldi r17, (motionDataEnd-motionDataBegin) + rcall Utils_FillSram + + ; setup pins + cbi MOTION_DDR, MOTION_PIN ; set DATA port as input + cbi MOTION_OUTPUT, MOTION_PIN ; disable internal pullup for TXD + sec + ret + + Motion_Fini: -Motion_Run: -Motion_Every100ms: + cbi MOTION_DDR, MOTION_PIN ; set DATA port as input + cbi MOTION_OUTPUT, MOTION_PIN ; disable internal pullup for TXD ret +Motion_Run: + clc + ret + + + +Motion_Every100ms: + sbis MOTION_INPUT, MOTION_PIN + rjmp Motion_Every100ms_skipReport + ; pin high + lds r16, motionTimer + tst r16 + brne Motion_Every100ms_skipReport + ; timer was 0 + ldi r18, 1 + rcall Motion_SendReport + brcc Motion_Every100ms_end + ldi r16, MOTION_TIMER_REPORT ; reset report timer + sts motionTimer, r16 + +Motion_Every100ms_skipReport: + lds r16, motionTimer + tst r16 + breq Motion_Every100ms_end + dec r16 + sts motionTimer, r16 +Motion_Every100ms_end: + ret + + + +; @param R18 sensor value + +Motion_SendReport: + ldi r16, 0xff ; destination address + ldi r17, VALUE_ID_MOTION ; value id + ldi r22, AQHOME_VALUETYPE_MOTION + clr r19 + ldi r20, 1 ; denominator + clr r21 + ldi xl, LOW(com2SendBuffer) + ldi xh, HIGH(com2SendBuffer) + rcall CPRO_WriteReportValue + rjmp COM2_SendPacket + + + + +