avr: improved module SK6812 and app MA_LIGHT.

- RGBW values are now only set by SK6812
- MA_LIGHT can now also take into account a brightness value reported
  by any node to switch on light only after dawn
This commit is contained in:
Martin Preuss
2025-09-22 15:09:45 +02:00
parent 42e477098c
commit 666e7690a6
15 changed files with 354 additions and 78 deletions

View File

@@ -16,10 +16,13 @@
appMotionLightDataBegin:
appMotionLightTimer: .byte 2
appMotionLightOnTime: .byte 2
appMotionLightColor: .byte 4
appMotionLightSources: .byte APP_MOTIONLIGHT_SOURCE_NUM*APP_MOTIONLIGHT_SOURCE_SIZE
appMotionLightTimer: .byte 2
appMotionLightOnTime: .byte 2
appMotionLightSources: .byte APP_MOTIONLIGHT_SOURCE_NUM*APP_MOTIONLIGHT_SOURCE_SIZE
appMotionLightLSourceAddr: .byte 1
appMotionLightLSourceValueId: .byte 1
appMotionLightLSourceValue: .byte 2
appMotionLightLastBrightness: .byte 2
appMotionLightDataEnd:

View File

@@ -27,14 +27,6 @@ AppMotionLight_Init:
ldi r17, (appMotionLightDataEnd-appMotionLightDataBegin)
rcall Utils_FillSram
ldi xh, HIGH(appMotionLightColor)
ldi xl, LOW(appMotionLightColor)
clr r16
ldi r17, 0x80
st X+, r16 ; R
st X+, r16 ; G
st X+, r17 ; B
st X+, r16 ; WW
ldi r16, LOW(APP_MOTIONLIGHT_DEFAULT_ONTIME)
sts appMotionLightOnTime, r16
ldi r16, HIGH(APP_MOTIONLIGHT_DEFAULT_ONTIME)
@@ -76,7 +68,7 @@ AppMotionLight_Every100ms:
AppMotionLight_Every100ms_ret:
ret
AppMotionLight_Every100ms_off:
rjmp appMotionLightTurnOff ; (r16, r17, r18, r19, r20, r21, r23)
rjmp appMotionLightTurnOff ; (r16, r17, r18, r19, r20, r21, r23, r24, r25)
; @end
@@ -112,6 +104,31 @@ appMotionLightHasSource_next:
; ---------------------------------------------------------------------------
; @routine appMotionLightIsBrightnessSource
;
; @return CFLAGS set if we have a matching source entry, cleared otherwise
; @param r17 value id
; @param r22 source address
; @clobbers R16
appMotionLightIsBrightnessSource:
lds r16, appMotionLightLSourceAddr
cp r16, r22
brne appMotionLightIsBrightnessSource_no
lds r16, appMotionLightLSourceValueId
cp r16, r17
brne appMotionLightIsBrightnessSource_no
sec
rjmp appMotionLightIsBrightnessSource_ret
appMotionLightIsBrightnessSource_no:
clc
appMotionLightIsBrightnessSource_ret:
ret
; @end
; ---------------------------------------------------------------------------
; @routine appMotionLightStartTimer
;
@@ -130,14 +147,11 @@ appMotionLightStartTimer:
; ---------------------------------------------------------------------------
; @routine appMotionLightTurnOn
;
; @clobbers r18, r19, r20, r21 (r16, t17, r23)
; @clobbers r18 (r16, r17, r19, r20, r21, r23, r24, r25)
appMotionLightTurnOn:
lds r18, appMotionLightColor
lds r19, appMotionLightColor+1
lds r20, appMotionLightColor+2
lds r21, appMotionLightColor+3
rjmp SK6812_SetAllColor ; (r16, r17, r23)
ldi r18, 1
rjmp SK6812_SetState ; (r16, r17, r18, r19, r20, r21, r23, r24, r25)
; @end
@@ -145,14 +159,11 @@ appMotionLightTurnOn:
; ---------------------------------------------------------------------------
; @routine appMotionLightTurnOff
;
; @clobbers r18, r19, r20, r21 (r16, t17, r23)
; @clobbers r18 (r16, r17, r19, r20, r21, r23, r24, r25)
appMotionLightTurnOff:
clr r18
clr r19
clr r20
clr r21
rjmp SK6812_SetAllColor ; (r16, r17, r23)
rjmp SK6812_SetState ; (r16, r17, r18, r19, r20, r21, r23, r24, r25)
; @end
@@ -193,7 +204,27 @@ appMotionLightReadConfFromEeprom:
mov r19, r16
sts appMotionLightSources+APP_MOTIONLIGHT_SOURCE_SIZE+APP_MOTIONLIGHT_SOURCE_OFFS_ADDR, r18
sts appMotionLightSources+APP_MOTIONLIGHT_SOURCE_SIZE+APP_MOTIONLIGHT_SOURCE_OFFS_VALUEID, r19
; read brightness source
ldi xl, LOW(EEPROM_OFFS_MAL_CONF_BSRC_ADDR)
ldi xh, HIGH(EEPROM_OFFS_MAL_CONF_BSRC_ADDR)
rcall Utils_ReadEepromIncr ; (R16)
sts appMotionLightLSourceAddr, r16
ldi xl, LOW(EEPROM_OFFS_MAL_CONF_BSRC_VALUEID)
ldi xh, HIGH(EEPROM_OFFS_MAL_CONF_BSRC_VALUEID)
rcall Utils_ReadEepromIncr ; (R16)
sts appMotionLightLSourceValueId, r16
; read brightness limit
ldi xl, LOW(EEPROM_OFFS_MAL_CONF_BSRC_VALUE)
ldi xh, HIGH(EEPROM_OFFS_MAL_CONF_BSRC_VALUE)
rcall Utils_ReadEepromIncr ; (R16)
mov r18, r16
rcall Utils_ReadEepromIncr ; (R16)
mov r19, r16
sts appMotionLightLSourceValue, r18
sts appMotionLightLSourceValue+1, r19
appMotionLightReadConfFromEeprom_end:
out SREG, r15
pop r15
@@ -215,21 +246,43 @@ appMotionLightWriteConfToEeprom:
ldi xh, HIGH(EEPROM_OFFS_MAL_CONF_ONTIME)
lds r16, appMotionLightOnTime
rcall Utils_WriteEepromIncr ; (R17)
rcall Eeprom_WriteByteIfChanged ; (R17)
adiw xh:xl, 1
lds r16, appMotionLightOnTime+1
rcall Utils_WriteEepromIncr ; (R17)
rcall Eeprom_WriteByteIfChanged ; (R17)
adiw xh:xl, 1
; write source 1
lds r16, appMotionLightSources+APP_MOTIONLIGHT_SOURCE_OFFS_ADDR
rcall Utils_WriteEepromIncr ; (R16)
rcall Eeprom_WriteByteIfChanged ; (R17)
adiw xh:xl, 1
lds r16, appMotionLightSources+APP_MOTIONLIGHT_SOURCE_OFFS_VALUEID
rcall Utils_WriteEepromIncr ; (R16)
rcall Eeprom_WriteByteIfChanged ; (R17)
adiw xh:xl, 1
; write source 2
lds r16, appMotionLightSources+APP_MOTIONLIGHT_SOURCE_SIZE+APP_MOTIONLIGHT_SOURCE_OFFS_ADDR
rcall Utils_WriteEepromIncr ; (R16)
rcall Eeprom_WriteByteIfChanged ; (R17)
adiw xh:xl, 1
lds r16, appMotionLightSources+APP_MOTIONLIGHT_SOURCE_SIZE+APP_MOTIONLIGHT_SOURCE_OFFS_VALUEID
rcall Utils_WriteEepromIncr ; (R16)
rcall Eeprom_WriteByteIfChanged ; (R17)
adiw xh:xl, 1
; write brightness source
lds r16, appMotionLightLSourceAddr
rcall Eeprom_WriteByteIfChanged ; (R17)
adiw xh:xl, 1
lds r16, appMotionLightLSourceValueId
rcall Eeprom_WriteByteIfChanged ; (R17)
adiw xh:xl, 1
; write brightness limit
lds r16, appMotionLightLSourceValue
rcall Eeprom_WriteByteIfChanged ; (R17)
adiw xh:xl, 1
lds r16, appMotionLightLSourceValue+1
rcall Eeprom_WriteByteIfChanged ; (R17)
adiw xh:xl, 1
out SREG, r15
pop r15

View File

@@ -33,6 +33,8 @@ AppMotionLight_OnPacketReceived:
; "report value" message
AppMotionLight_OnPacketReceived_report:
rcall NETMSG_ValueRead ; (none)
rcall appMotionLightIsBrightnessSource ; (R16)
brcs AppMotionLight_OnPacketReceived_isBrightness
mov r16, r18
or r16, r19
breq AppMotionLight_OnPacketReceived_clcRet ; zero value, ignore
@@ -40,41 +42,62 @@ AppMotionLight_OnPacketReceived_report:
brcs AppMotionLight_OnPacketReceived_turnOn
ret
AppMotionLight_OnPacketReceived_turnOn:
lds r16, appMotionLightTimer
lds r16, appMotionLightLSourceAddr ; do we have a source for brightness value?
tst r16
breq AppMotionLight_OnPacketReceived_checkTimer ; no, jmp and don't check brightness
lds r16, appMotionLightLastBrightness
lds r17, appMotionLightLastBrightness+1
tst r16
brne AppMotionLight_OnPacketReceived_checkBrightness
tst r17
breq AppMotionLight_OnPacketReceived_secRet ; we don't have a brightness value, keep light off
AppMotionLight_OnPacketReceived_checkBrightness:
lds r16, appMotionLightLSourceValue ; check last brightness reported
lds r17, appMotionLightLSourceValue+1
lds r24, appMotionLightLastBrightness
lds r25, appMotionLightLastBrightness+1
sub r16, r24
sbc r17, r25
brcs AppMotionLight_OnPacketReceived_secRet ; too bright to turn on
AppMotionLight_OnPacketReceived_checkTimer:
lds r16, appMotionLightTimer ; time up?
lds r17, appMotionLightTimer+1
or r16, r17
brne AppMotionLight_OnPacketReceived_startTimer
rcall appMotionLightTurnOn ; (r16, r17, r18, r19, r20, r21, r23)
brne AppMotionLight_OnPacketReceived_startTimer ; no, just restart timer (light already on)
rcall appMotionLightTurnOn ; (r16, r17, r18, r19, r20, r21, r23, r24, r25)
AppMotionLight_OnPacketReceived_startTimer:
rcall appMotionLightStartTimer
AppMotionLight_OnPacketReceived_secRet:
sec
ret
AppMotionLight_OnPacketReceived_isBrightness:
sts appMotionLightLastBrightness, r18
sts appMotionLightLastBrightness+1, r19
sec
ret
; "set value" message
AppMotionLight_OnPacketReceived_set:
rcall NETMSG_ValueRead ; (none)
cpi r17, VALUE_ID_MAL_RGBW_VALUE
breq AppMotionLight_OnPacketReceived_setRGBW
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_setRGBW: ; "setValue nodes/XXXXXXXX/MALRGBWVALUE GRWB"
sts appMotionLightColor, r18
sts appMotionLightColor+1, r19
sts appMotionLightColor+2, r20
sts appMotionLightColor+3, r21
push r24
push r25
rcall appMotionLightStartTimer ; immediately ON with new color (r16)
rcall appMotionLightTurnOn ; (r16, r17, r18, r19, r20, r21, r23)
pop r25
pop r24
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