avr: move timing control from ma_light to sk6812.

This allows for better control over the LED strip. We can now trigger the
LED strip externally (e.g. by setting a new RGBW value).
This commit is contained in:
Martin Preuss
2026-03-24 23:13:28 +01:00
parent 42874f27cd
commit 67be74d2ac
10 changed files with 262 additions and 209 deletions

View File

@@ -16,8 +16,6 @@
appMotionLightDataBegin:
appMotionLightTimer: .byte 2
appMotionLightOnTime: .byte 2
appMotionLightSources: .byte APP_MOTIONLIGHT_SOURCE_NUM*APP_MOTIONLIGHT_SOURCE_SIZE
appMotionLightLSourceAddr: .byte 1
appMotionLightLSourceValueId: .byte 1

View File

@@ -27,11 +27,6 @@ AppMotionLight_Init:
ldi r17, (appMotionLightDataEnd-appMotionLightDataBegin)
rcall Utils_FillSram
ldi r16, LOW(APP_MOTIONLIGHT_DEFAULT_ONTIME)
sts appMotionLightOnTime, r16
ldi r16, HIGH(APP_MOTIONLIGHT_DEFAULT_ONTIME)
sts appMotionLightOnTime+1, r16
rcall appMotionLightReadConfFromEeprom
ret
@@ -44,31 +39,7 @@ AppMotionLight_Init:
;
AppMotionLight_Fini:
clr r16
sts appMotionLightTimer, r16 ; clear timer
sts appMotionLightTimer+1, r16
rjmp appMotionLightTurnOff
; @end
; ---------------------------------------------------------------------------
; @routine AppMotionLight_Every100ms @global
;
; @clobbers r16, r24, r26, (r17, r18, r19, r20, r21, r23)
AppMotionLight_Every100ms:
lds r24, appMotionLightTimer
lds r25, appMotionLightTimer+1
sbiw r25:r24, 1
brcs AppMotionLight_Every100ms_ret
sts appMotionLightTimer, r24
sts appMotionLightTimer+1, r25
breq AppMotionLight_Every100ms_off
AppMotionLight_Every100ms_ret:
ret
AppMotionLight_Every100ms_off:
rjmp appMotionLightTurnOff ; (r16, r17, r18, r19, r20, r21, r22, r23, r24, r25, X)
; @end
@@ -129,71 +100,6 @@ appMotionLightIsBrightnessSource_ret:
; ---------------------------------------------------------------------------
; @routine appMotionLightStartTimer
;
; @clobbers r16
appMotionLightStartTimer:
lds r16, appMotionLightOnTime
sts appMotionLightTimer, r16
lds r16, appMotionLightOnTime+1
sts appMotionLightTimer+1, r16
ret
; @end
; ---------------------------------------------------------------------------
; @routine appMotionLightTurnOn
;
; @clobbers r18 (r16, r17, r19, r20, r21, r22, r23, r24, r25, X)
appMotionLightTurnOn:
ldi r18, 1
rcall SK6812_SetState ; (R16, R17, R18, R19, R20, R21, R23, R24, R25)
ldi r18, 1
rjmp appMotionLightReportState ; (R16, R17, R18, R19, R20, R21, R22, R23, R24, R25, X)
; @end
; ---------------------------------------------------------------------------
; @routine appMotionLightTurnOff
;
; @clobbers r18 (r16, r17, r19, r20, r21, r22, r23, r24, r25, X)
appMotionLightTurnOff:
clr r18
rcall SK6812_SetState ; (R16, R17, R18, R19, R20, R21, R23, R24, R25)
clr r18
rjmp appMotionLightReportState ; (R16, R17, R18, R19, R20, R21, R22, R23, R24, R25, X)
; @end
; ---------------------------------------------------------------------------
; @routine appMotionLightReportState
;
; @param R18 state (0=off, 1=on)
; @clobbers (R16, R17, R18, R19, R20, R21, R22, R23, R24, R25, X)
appMotionLightReportState:
ldi r17, VALUE_ID_MAL_STATE
clr r19
ldi r20, 1
clr r21
ldi r22, AQHOME_VALUETYPE_ONOFF
push yl
push yh
rcall Main_SendValueReport ; (R16, R17, R18, R19, R20, R21, R23, R24, R25, X)
pop yh
pop yl
ret
; @end
; ---------------------------------------------------------------------------
; @routine appMotionLightReadConfFromEeprom
;
@@ -212,8 +118,7 @@ appMotionLightReadConfFromEeprom:
and r16, r18
cpi r16, 0xff
breq appMotionLightReadConfFromEeprom_end
sts appMotionLightOnTime, r18
sts appMotionLightOnTime+1, r19
bigcall SK6812_SetAutoTimerReload ; (none)
; read source 1
rcall Utils_ReadEepromIncr ; (R16)
@@ -265,10 +170,11 @@ appMotionLightWriteConfToEeprom:
ldi xl, LOW(EEPROM_OFFS_MAL_CONF_ONTIME)
ldi xh, HIGH(EEPROM_OFFS_MAL_CONF_ONTIME)
lds r16, appMotionLightOnTime
rcall SK6812_GetAutoTimerReload
mov r16, r18
rcall Eeprom_WriteByteIfChanged ; (R17)
adiw xh:xl, 1
lds r16, appMotionLightOnTime+1
mov r16, r19
rcall Eeprom_WriteByteIfChanged ; (R17)
adiw xh:xl, 1

View File

@@ -21,116 +21,127 @@
; @clobbers any, -X
AppMotionLight_OnPacketReceived:
adiw xh:xl, 2 ; command
ld r16, X
sbiw xh:xl, 2
cpi r16, NETMSG_CMD_VALUE_SET
breq AppMotionLight_OnPacketReceived_set
cpi r16, NETMSG_CMD_VALUE_REPORT
breq AppMotionLight_OnPacketReceived_report
clc ; unexpected msg
ret
; "report value" message
AppMotionLight_OnPacketReceived_report:
rcall NETMSG_ValueRead ; (none)
ldi zl, LOW(appMotionLightMsgTable*2)
ldi zh, HIGH(appMotionLightMsgTable*2)
rjmp Main_HandleValueMsg
; ---------------------------------------------------------------------------
; @routine appMotionLightHandleReport
;
appMotionLightHandleReport:
rcall appMotionLightIsBrightnessSource ; (R16)
brcs AppMotionLight_OnPacketReceived_isBrightness
brcs appMotionLightHandleReport_brightness
; motion source?
mov r16, r18
or r16, r19
breq AppMotionLight_OnPacketReceived_clcRet ; zero value, ignore
breq appMotionLightHandleReport_ret ; zero value, ignore
rcall appMotionLightHasSource ; (r16, r24, Y)
brcs AppMotionLight_OnPacketReceived_turnOn
ret
AppMotionLight_OnPacketReceived_turnOn:
brcc appMotionLightHandleReport_ret
; motion detected, check for brightness source
lds r16, appMotionLightLSourceAddr ; do we have a source for brightness value?
tst r16
breq AppMotionLight_OnPacketReceived_checkTimer ; no, jmp and don't check brightness
breq appMotionLightHandleReport_trigger ; no, jmp and don't check brightness
; we have a brightness source configured, do we have a value?
lds r16, appMotionLightFlags ; check last brightness reported
andi r16, APP_MOTIONLIGHT_FLAGS_HAVELIGHT
breq AppMotionLight_OnPacketReceived_secRet ; no brightness, yet
AppMotionLight_OnPacketReceived_checkBrightness:
breq appMotionLightHandleReport_ret ; no brightness, yet
appMotionLightHandleReport_checkBrightness:
lds r16, appMotionLightLSourceValue ; check last brightness reported
lds r17, appMotionLightLSourceValue+1
lds r24, appMotionLightLastBrightness ; r25:r24>r17:r16?
lds r25, appMotionLightLastBrightness+1 ; appMotionLightLastBrightness>appMotionLightLSourceValue?
sub r16, r24
sbc r17, r25
brcs AppMotionLight_OnPacketReceived_secRet ; yes, too bright to turn on
AppMotionLight_OnPacketReceived_checkTimer:
lds r16, appMotionLightTimer ; time up?
lds r17, appMotionLightTimer+1
or r16, r17
brne AppMotionLight_OnPacketReceived_startTimer ; no, just restart timer (light already on)
push xl
push xh
rcall appMotionLightTurnOn ; (r16, r17, r18, r19, r20, r21, r22, r23, r24, r25, X)
pop xh
pop xl
AppMotionLight_OnPacketReceived_startTimer:
rcall appMotionLightStartTimer
AppMotionLight_OnPacketReceived_secRet:
sec
ret
AppMotionLight_OnPacketReceived_isBrightness:
brcs appMotionLightHandleReport_ret ; yes, too bright to turn on
appMotionLightHandleReport_trigger:
bigcall SK6812_Trigger
rjmp appMotionLightHandleReport_ret
; received report is our brightness source
appMotionLightHandleReport_brightness:
sts appMotionLightLastBrightness, r18
sts appMotionLightLastBrightness+1, r19
lds r16, appMotionLightFlags
ori r16, APP_MOTIONLIGHT_FLAGS_HAVELIGHT
sts appMotionLightFlags, r16
sec
appMotionLightHandleReport_ret:
clc
ret
; "set value" message
AppMotionLight_OnPacketReceived_set:
rcall NETMSG_ValueRead ; (none)
cpi r17, VALUE_ID_MAL_ONTIME
breq AppMotionLight_OnPacketReceived_setOnTime
cpi r17, VALUE_ID_MAL_SOURCE1
breq AppMotionLight_OnPacketReceived_setSource1
cpi r17, VALUE_ID_MAL_SOURCE2
breq AppMotionLight_OnPacketReceived_setSource2
cpi r17, VALUE_ID_MAL_BSOURCE
breq AppMotionLight_OnPacketReceived_setBSource
cpi r17, VALUE_ID_MAL_BVALUE
breq AppMotionLight_OnPacketReceived_setBValue
AppMotionLight_OnPacketReceived_clcRet:
clc ; unexpected message
ret
AppMotionLight_OnPacketReceived_setBSource: ; setValue "nodes/XXXXXXXX/MALSOURCEB 0xVVNN" (VV=value id, NN=node addr)
sts appMotionLightLSourceAddr, r18
sts appMotionLightLSourceValueId, r19
rjmp AppMotionLight_OnPacketReceived_sendAck
AppMotionLight_OnPacketReceived_setBValue: ; "setValue nodes/XXXXXXXX/MALVALUEB n" (n brightness value)
sts appMotionLightLSourceValue, r18
sts appMotionLightLSourceValue+1, r19
rjmp AppMotionLight_OnPacketReceived_sendAck
AppMotionLight_OnPacketReceived_setOnTime: ; "setValue nodes/XXXXXXXX/MALONTIME n" (n in 1/10 secs)
sts appMotionLightOnTime, r18
sts appMotionLightOnTime+1, r19
lds r16, appMotionLightTimer ; timer active?
lds r17, appMotionLightTimer+1
or r16, r17
breq AppMotionLight_OnPacketReceived_sendAck ; nope, just set it and leave
sts appMotionLightTimer, r18 ; restart timer with new value
sts appMotionLightTimer+1, r19
rjmp AppMotionLight_OnPacketReceived_sendAck
AppMotionLight_OnPacketReceived_setSource1: ; setValue "nodes/XXXXXXXX/MALSOURCE1 0xVVNN" (VV=value id, NN=node addr)
; @end
; ---------------------------------------------------------------------------
; @routine appMotionLightSetValueOnTime
;
appMotionLightSetValueOnTime:
rcall SK6812_SetAutoTimerReload
rjmp appMotionLightSetValueReturn
; @end
; ---------------------------------------------------------------------------
; @routine appMotionLightSetValueSource1
;
appMotionLightSetValueSource1:
sts appMotionLightSources, r18 ; peerAddr
sts appMotionLightSources+1, r19 ; valueId
rjmp AppMotionLight_OnPacketReceived_sendAck
AppMotionLight_OnPacketReceived_setSource2: ; setValue "nodes/XXXXXXXX/MALSOURCE2 0xVVNN" (VV=value id, NN=node addr)
rjmp appMotionLightSetValueReturn
; @end
; ---------------------------------------------------------------------------
; @routine appMotionLightSetValueSource2
;
appMotionLightSetValueSource2:
sts appMotionLightSources+APP_MOTIONLIGHT_SOURCE_SIZE, r18 ; peerAddr
sts appMotionLightSources+APP_MOTIONLIGHT_SOURCE_SIZE+1, r19 ; valueId
AppMotionLight_OnPacketReceived_sendAck:
push xl
push xh
ldi r23, NETMSG_CMD_VALUE_SET_ACK
rcall Main_SendValueResponse ; (clobbers all, including Y)
rcall appMotionLightWriteConfToEeprom ; (r16, r17, X)
pop xh
pop xl
rjmp appMotionLightSetValueReturn
; @end
; ---------------------------------------------------------------------------
; @routine appMotionLightSetValueBValue
;
appMotionLightSetValueBValue:
sts appMotionLightSources, r18 ; peerAddr
sts appMotionLightSources+1, r19 ; valueId
rjmp appMotionLightSetValueReturn
; @end
; ---------------------------------------------------------------------------
; @routine appMotionLightSetValueReturn
;
; Common return code
appMotionLightSetValueReturn:
rcall appMotionLightWriteConfToEeprom
ldi r23, NETMSG_CMD_VALUE_SET_ACK
sec
ret
; @end
appMotionLightMsgTable:
.db NETMSG_CMD_VALUE_REPORT, 0, LOW(appMotionLightHandleReport), HIGH(appMotionLightHandleReport)
.db NETMSG_CMD_VALUE_SET, VALUE_ID_MAL_ONTIME, LOW(appMotionLightSetValueOnTime), HIGH(appMotionLightSetValueOnTime)
.db NETMSG_CMD_VALUE_SET, VALUE_ID_MAL_SOURCE1, LOW(appMotionLightSetValueSource1), HIGH(appMotionLightSetValueSource1)
.db NETMSG_CMD_VALUE_SET, VALUE_ID_MAL_SOURCE2, LOW(appMotionLightSetValueSource2), HIGH(appMotionLightSetValueSource2)
.db NETMSG_CMD_VALUE_SET, VALUE_ID_MAL_BVALUE, LOW(appMotionLightSetValueBValue), HIGH(appMotionLightSetValueBValue)
.db 0, 0, 0, 0