Files
aqhomecontrol/avr/modules/motion/main.asm
2024-12-15 19:56:38 +01:00

120 lines
2.8 KiB
NASM

; ***************************************************************************
; copyright : (C) 2024 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 MOTION_SEND_1_AFTER = 1 ; send motion message after 100ms
.equ MOTION_SEND_2_AFTER = 11 ; send motion message after 1100ms
.equ MOTION_SEND_3_AFTER = 50 ; repeat motion message after 5s
.equ MOTION_RESTART_AFTER = 150 ; restart motion report counter after 15s
.dseg
motionDataBegin:
motionCurrentState: .byte 1
motionStateCounter: .byte 1
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:
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:
clr r16
sbic MOTION_INPUT, MOTION_PIN
inc r16
; check against stored state
lds r17, motionCurrentState
cp r16, r17
brne Motion_Every100ms_stateChanged
; state not changed
tst r16
breq Motion_Every100ms_end
; state unchanged, active motion
lds r16, motionStateCounter
inc r16
sts motionStateCounter, r16
cpi r16, MOTION_SEND_1_AFTER
breq Motion_Every100ms_send
cpi r16, MOTION_SEND_2_AFTER
breq Motion_Every100ms_send
; cpi r16, MOTION_SEND_3_AFTER
; breq Motion_Every100ms_send
cpi r16, MOTION_RESTART_AFTER
brne Motion_Every100ms_end
clr r16
sts motionStateCounter, r16 ; restart motion state counter (thus report again on motion)
ret
Motion_Every100ms_stateChanged:
sts motionCurrentState, r16
clr r16
sts motionStateCounter, r16
ret ; only send according to timers above
Motion_Every100ms_send:
lds r18, motionCurrentState
; only send if state is != 0
tst r18
breq Motion_Every100ms_end
rjmp Motion_SendReport
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