avr: optimized app MA_LIGHT (from 3295 to 3222 words).

This commit is contained in:
Martin Preuss
2026-04-26 15:18:38 +02:00
parent c6fdc94a46
commit 6948028284
7 changed files with 143 additions and 166 deletions

View File

@@ -1,5 +1,5 @@
; ***************************************************************************
; copyright : (C) 2025 by Martin Preuss
; copyright : (C) 2026 by Martin Preuss
; email : martin@libchipcard.de
;
; ***************************************************************************
@@ -16,10 +16,12 @@
appMotionLightDataBegin:
appMotionLightSources: .byte APP_MOTIONLIGHT_SOURCE_NUM*APP_MOTIONLIGHT_SOURCE_SIZE
appMotionLightLSourceAddr: .byte 1
appMotionLightLSourceValueId: .byte 1
appMotionLightSettings:
appMotionLightOnTime: .byte 2
appMotionLightMSources: .byte APP_MOTIONLIGHT_SOURCE_NUM*APP_MOTIONLIGHT_SOURCE_SIZE
appMotionLightLSource: .byte APP_MOTIONLIGHT_SOURCE_SIZE
appMotionLightLSourceValue: .byte 2
appMotionLightLastBrightness: .byte 2
appMotionLightFlags: .byte 1
appMotionLightDataEnd:

View File

@@ -23,3 +23,17 @@
.equ APP_MOTIONLIGHT_FLAGS_HAVELIGHT = 0x01
.equ APP_MOTIONLIGHT_EE_OFFS_ONTIME_LO = 0
.equ APP_MOTIONLIGHT_EE_OFFS_ONTIME_HI = 1
.equ APP_MOTIONLIGHT_EE_OFFS_MSRC1_ADDR = 2
.equ APP_MOTIONLIGHT_EE_OFFS_MSRC1_VALUEID = 3
.equ APP_MOTIONLIGHT_EE_OFFS_MSRC2_ADDR = 4
.equ APP_MOTIONLIGHT_EE_OFFS_MSRC2_VALUEID = 5
.equ APP_MOTIONLIGHT_EE_OFFS_BSRC1_ADDR = 6
.equ APP_MOTIONLIGHT_EE_OFFS_BSRC1_VALUEID = 7
.equ APP_MOTIONLIGHT_EE_OFFS_BLIMIT_LO = 8
.equ APP_MOTIONLIGHT_EE_OFFS_BLIMIT_HI = 9
.equ APP_MOTIONLIGHT_EE_SIZE = 10

View File

@@ -27,7 +27,7 @@ AppMotionLight_Init:
ldi r17, (appMotionLightDataEnd-appMotionLightDataBegin)
rcall Utils_FillSram
rcall appMotionLightReadConfFromEeprom
rcall appMotionLightReadConfFromEeprom ; (r16, r17, X, Y)
ret
; @end
@@ -53,23 +53,17 @@ AppMotionLight_Fini:
; @clobbers r16, r24, Y
appMotionLightHasSource:
ldi yl, LOW(appMotionLightSources)
ldi yh, HIGH(appMotionLightSources)
ldi yl, LOW(appMotionLightMSources)
ldi yh, HIGH(appMotionLightMSources)
ldi r24, APP_MOTIONLIGHT_SOURCE_NUM
appMotionLightHasSource_loop:
ldd r16, Y+APP_MOTIONLIGHT_SOURCE_OFFS_ADDR
cp r16, r22
brne appMotionLightHasSource_next
ldd r16, Y+APP_MOTIONLIGHT_SOURCE_OFFS_VALUEID
cp r16, r17
brne appMotionLightHasSource_next
sec
ret
appMotionLightHasSource_next:
rcall appMotionLightCheckMsgSrc
brcs appMotionLightHasSource_ret
adiw yh:yl, APP_MOTIONLIGHT_SOURCE_SIZE
dec r24
brne appMotionLightHasSource_loop
brne appMotionLightHasSource_loop
clc
appMotionLightHasSource_ret:
ret
; @end
@@ -81,20 +75,37 @@ appMotionLightHasSource_next:
; @return CFLAGS set if we have a matching source entry, cleared otherwise
; @param r17 value id
; @param r22 source address
; @clobbers R16
; @clobbers R16, Y
appMotionLightIsBrightnessSource:
lds r16, appMotionLightLSourceAddr
cp r16, r22
brne appMotionLightIsBrightnessSource_no
lds r16, appMotionLightLSourceValueId
cp r16, r17
brne appMotionLightIsBrightnessSource_no
ldi yl, LOW(appMotionLightLSource)
ldi yh, HIGH(appMotionLightLSource)
rjmp appMotionLightCheckMsgSrc
; @end
; ---------------------------------------------------------------------------
; @routine appMotionLightCheckMsgSrc
;
; @return CFLAGS set if we have a matching source entry, cleared otherwise
; @param r17 value id
; @param r22 source address
; @param Y pointer to addr/valueid pair to investigate
; @clobbers R16
appMotionLightCheckMsgSrc:
ldd r16, Y+APP_MOTIONLIGHT_SOURCE_OFFS_ADDR
cp r16, r22 ; check node addr
brne appMotionLightCheckMsgSrc_no
ldd r16, Y+APP_MOTIONLIGHT_SOURCE_OFFS_VALUEID
cp r16, r17 ; check value id
brne appMotionLightCheckMsgSrc_no
sec
rjmp appMotionLightIsBrightnessSource_ret
appMotionLightIsBrightnessSource_no:
rjmp appMotionLightCheckMsgSrc_ret
appMotionLightCheckMsgSrc_no:
clc
appMotionLightIsBrightnessSource_ret:
appMotionLightCheckMsgSrc_ret:
ret
; @end
@@ -103,56 +114,29 @@ appMotionLightIsBrightnessSource_ret:
; ---------------------------------------------------------------------------
; @routine appMotionLightReadConfFromEeprom
;
; @clobbers
; @clobbers r16, r17, X, Y
appMotionLightReadConfFromEeprom:
push r15
in r15, SREG
cli
ldi xl, LOW(EEPROM_OFFS_MAL_CONF_ONTIME)
ldi xh, HIGH(EEPROM_OFFS_MAL_CONF_ONTIME)
rcall Utils_ReadEepromIncr ; (R16)
mov r18, r16
rcall Utils_ReadEepromIncr ; (R16)
mov r19, r16
and r16, r18
cpi r16, 0xff
breq appMotionLightReadConfFromEeprom_end
bigcall SK6812_SetAutoTimerReload ; (none)
; read source 1
rcall Utils_ReadEepromIncr ; (R16)
mov r18, r16
rcall Utils_ReadEepromIncr ; (R16)
mov r19, r16
sts appMotionLightSources+APP_MOTIONLIGHT_SOURCE_OFFS_ADDR, r18
sts appMotionLightSources+APP_MOTIONLIGHT_SOURCE_OFFS_VALUEID, r19
ldi xl, LOW(EEPROM_OFFS_MAL_CONF)
ldi xh, HIGH(EEPROM_OFFS_MAL_CONF)
ldi yl, LOW(appMotionLightSettings)
ldi yh, HIGH(appMotionLightSettings)
ldi r17, APP_MOTIONLIGHT_EE_SIZE
bigcall Eeprom_ReadBytes ; (r16, r17, X, Y)
brcc appMotionLightReadConfFromEeprom_ret
lds r18, appMotionLightOnTime
lds r19, appMotionLightOnTime+1
mov r16, r18
and r16, r19
inc r16
brne appMotionLightReadConfFromEeprom_reload
ldi r18, LOW(APP_MOTIONLIGHT_DEFAULT_ONTIME)
ldi r19, HIGH(APP_MOTIONLIGHT_DEFAULT_ONTIME)
appMotionLightReadConfFromEeprom_reload:
bigcall SK6812_SetAutoTimerReload ; (none)
; read source 2
rcall Utils_ReadEepromIncr ; (R16)
mov r18, r16
rcall Utils_ReadEepromIncr ; (R16)
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
rcall Utils_ReadEepromIncr ; (R16)
sts appMotionLightLSourceAddr, r16
rcall Utils_ReadEepromIncr ; (R16)
sts appMotionLightLSourceValueId, r16
; read brightness limit
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
appMotionLightReadConfFromEeprom_ret:
ret
; @end
@@ -161,59 +145,15 @@ appMotionLightReadConfFromEeprom_end:
; ---------------------------------------------------------------------------
; @routine appMotionLightWriteConfToEeprom
;
; @clobbers r16, r17, X
; @clobbers r16, r17, X, Y
appMotionLightWriteConfToEeprom:
push r15
in r15, SREG
cli
ldi xl, LOW(EEPROM_OFFS_MAL_CONF_ONTIME)
ldi xh, HIGH(EEPROM_OFFS_MAL_CONF_ONTIME)
rcall SK6812_GetAutoTimerReload
mov r16, r18
rcall Eeprom_WriteByteIfChanged ; (R17)
adiw xh:xl, 1
mov r16, r19
rcall Eeprom_WriteByteIfChanged ; (R17)
adiw xh:xl, 1
; write source 1
lds r16, appMotionLightSources+APP_MOTIONLIGHT_SOURCE_OFFS_ADDR
rcall Eeprom_WriteByteIfChanged ; (R17)
adiw xh:xl, 1
lds r16, appMotionLightSources+APP_MOTIONLIGHT_SOURCE_OFFS_VALUEID
rcall Eeprom_WriteByteIfChanged ; (R17)
adiw xh:xl, 1
; write source 2
lds r16, appMotionLightSources+APP_MOTIONLIGHT_SOURCE_SIZE+APP_MOTIONLIGHT_SOURCE_OFFS_ADDR
rcall Eeprom_WriteByteIfChanged ; (R17)
adiw xh:xl, 1
lds r16, appMotionLightSources+APP_MOTIONLIGHT_SOURCE_SIZE+APP_MOTIONLIGHT_SOURCE_OFFS_VALUEID
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
ret
ldi xl, LOW(EEPROM_OFFS_MAL_CONF)
ldi xh, HIGH(EEPROM_OFFS_MAL_CONF)
ldi yl, LOW(appMotionLightSettings)
ldi yh, HIGH(appMotionLightSettings)
ldi r17, APP_MOTIONLIGHT_EE_SIZE
bigjmp Eeprom_WriteBytes ; (r16, r17, r18, X, Y)
; @end

View File

@@ -1,5 +1,5 @@
; ***************************************************************************
; copyright : (C) 2025 by Martin Preuss
; copyright : (C) 2026 by Martin Preuss
; email : martin@libchipcard.de
;
; ***************************************************************************
@@ -30,33 +30,43 @@ AppMotionLight_OnPacketReceived:
; ---------------------------------------------------------------------------
; @routine appMotionLightHandleReport
;
; @param R17 value id
; @param R19:R18 value
; @param R21:R20 denom (e.g. 100, meaning value must be divided by 100)
; @param R22 source address
; @param R23 command
; @param R25:R24 message id
appMotionLightHandleReport:
rcall appMotionLightIsBrightnessSource ; (R16)
; is brightness source?
rcall appMotionLightIsBrightnessSource ; (R16, Y)
brcs appMotionLightHandleReport_brightness
; motion source?
; is motion source?
rcall appMotionLightHasSource ; (r16, r24, Y)
brcc appMotionLightHandleReport_ret
; motion detected?
mov r16, r18
or r16, r19
breq appMotionLightHandleReport_ret ; zero value, ignore
rcall appMotionLightHasSource ; (r16, r24, Y)
brcc appMotionLightHandleReport_ret
breq appMotionLightHandleReport_ret ; zero value, ignore
; motion detected, check for brightness source
lds r16, appMotionLightLSourceAddr ; do we have a source for brightness value?
lds r16, (appMotionLightLSource+APP_MOTIONLIGHT_SOURCE_OFFS_ADDR) ; do we have a source for brightness value?
tst r16
breq appMotionLightHandleReport_trigger ; no, jmp and don't check brightness
breq appMotionLightHandleReport_trigger ; no, jmp and don't check brightness
cpi r16, 0xff
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
lds r16, appMotionLightFlags ; check last brightness reported
andi r16, APP_MOTIONLIGHT_FLAGS_HAVELIGHT
breq appMotionLightHandleReport_ret ; no brightness, yet
breq appMotionLightHandleReport_ret ; no brightness, yet
appMotionLightHandleReport_checkBrightness:
lds r16, appMotionLightLSourceValue ; check last brightness reported
lds r16, appMotionLightLSourceValue ; check last brightness reported
lds r17, appMotionLightLSourceValue+1
lds r24, appMotionLightLastBrightness ; r25:r24>r17:r16?
lds r25, appMotionLightLastBrightness+1 ; appMotionLightLastBrightness>appMotionLightLSourceValue?
lds r24, appMotionLightLastBrightness ; r25:r24>r17:r16?
lds r25, appMotionLightLastBrightness+1 ; appMotionLightLastBrightness>appMotionLightLSourceValue?
sub r16, r24
sbc r17, r25
brcs appMotionLightHandleReport_ret ; yes, too bright to turn on
brcs appMotionLightHandleReport_ret ; yes, too bright to turn on
appMotionLightHandleReport_trigger:
bigcall SK6812_Trigger
rjmp appMotionLightHandleReport_ret
@@ -68,7 +78,7 @@ appMotionLightHandleReport_brightness:
ori r16, APP_MOTIONLIGHT_FLAGS_HAVELIGHT
sts appMotionLightFlags, r16
appMotionLightHandleReport_ret:
clc
clc ; don't send ACK
ret
; @end
@@ -77,20 +87,35 @@ appMotionLightHandleReport_ret:
; ---------------------------------------------------------------------------
; @routine appMotionLightSetValueOnTime
;
; @param R17 value id
; @param R19:R18 value
; @param R21:R20 denom (e.g. 100, meaning value must be divided by 100)
; @param R22 source address
; @param R23 command
; @param R25:R24 message id
appMotionLightSetValueOnTime:
sts appMotionLightOnTime, r18
sts appMotionLightOnTime+1, r19
rcall SK6812_SetAutoTimerReload
rjmp appMotionLightSetValueReturn
; @end
; ---------------------------------------------------------------------------
; @routine appMotionLightSetValueSource1
;
; @param R17 value id
; @param R19:R18 value
; @param R21:R20 denom (e.g. 100, meaning value must be divided by 100)
; @param R22 source address
; @param R23 command
; @param R25:R24 message id
appMotionLightSetValueSource1:
sts appMotionLightSources, r18 ; peerAddr
sts appMotionLightSources+1, r19 ; valueId
sts (appMotionLightMSources+APP_MOTIONLIGHT_SOURCE_OFFS_ADDR), r18 ; peerAddr
sts (appMotionLightMSources+APP_MOTIONLIGHT_SOURCE_OFFS_VALUEID), r19 ; valueId
rjmp appMotionLightSetValueReturn
; @end
@@ -99,10 +124,16 @@ appMotionLightSetValueSource1:
; ---------------------------------------------------------------------------
; @routine appMotionLightSetValueSource2
;
; @param R17 value id
; @param R19:R18 value
; @param R21:R20 denom (e.g. 100, meaning value must be divided by 100)
; @param R22 source address
; @param R23 command
; @param R25:R24 message id
appMotionLightSetValueSource2:
sts appMotionLightSources+APP_MOTIONLIGHT_SOURCE_SIZE, r18 ; peerAddr
sts appMotionLightSources+APP_MOTIONLIGHT_SOURCE_SIZE+1, r19 ; valueId
sts (appMotionLightMSources+APP_MOTIONLIGHT_SOURCE_SIZE+APP_MOTIONLIGHT_SOURCE_OFFS_ADDR), r18 ; peerAddr
sts (appMotionLightMSources+APP_MOTIONLIGHT_SOURCE_SIZE+APP_MOTIONLIGHT_SOURCE_OFFS_VALUEID), r19 ; valueId
rjmp appMotionLightSetValueReturn
; @end
@@ -111,10 +142,16 @@ appMotionLightSetValueSource2:
; ---------------------------------------------------------------------------
; @routine appMotionLightSetValueBValue
;
; @param R17 value id
; @param R19:R18 value
; @param R21:R20 denom (e.g. 100, meaning value must be divided by 100)
; @param R22 source address
; @param R23 command
; @param R25:R24 message id
appMotionLightSetValueBValue:
sts appMotionLightSources, r18 ; peerAddr
sts appMotionLightSources+1, r19 ; valueId
sts (appMotionLightMSources+APP_MOTIONLIGHT_SOURCE_OFFS_ADDR), r18 ; peerAddr
sts (appMotionLightMSources+APP_MOTIONLIGHT_SOURCE_OFFS_VALUEID), r19 ; valueId
rjmp appMotionLightSetValueReturn
; @end
@@ -126,7 +163,7 @@ appMotionLightSetValueBValue:
; Common return code
appMotionLightSetValueReturn:
rcall appMotionLightWriteConfToEeprom
rcall appMotionLightWriteConfToEeprom ; (r16, r17, X, Y)
ldi r23, NETMSG_CMD_VALUE_SET_ACK
sec
ret

View File

@@ -77,15 +77,7 @@
.equ EEPROM_OFFS_OSCCAL_SLOW = 12 ; 1 byte
.equ EEPROM_OFFS_OSCCAL_FAST = 13 ; 1 byte
.equ EEPROM_OFFS_MAL_CONF_ONTIME = 14 ; 2 bytes
.equ EEPROM_OFFS_MAL_CONF_SRC1_ADDR = 16 ; 1 byte
.equ EEPROM_OFFS_MAL_CONF_SRC1_VALUEID = 17 ; 1 byte
.equ EEPROM_OFFS_MAL_CONF_SRC2_ADDR = 18 ; 1 byte
.equ EEPROM_OFFS_MAL_CONF_SRC2_VALUEID = 19 ; 1 byte
.equ EEPROM_OFFS_MAL_CONF_BSRC_ADDR = 20 ; 1 byte light source addr
.equ EEPROM_OFFS_MAL_CONF_BSRC_VALUEID = 21 ; 1 byte light source value id
.equ EEPROM_OFFS_MAL_CONF_BSRC_VALUE = 22 ; 2 bytes light source trigger value
.equ EEPROM_OFFS_MAL_CONF = 14 ; 10 bytes
.equ EEPROM_OFFS_ROUTER_RANGE_BEGIN = 24 ; 1 byte
.equ EEPROM_OFFS_ROUTER_RANGE_END = 25 ; 1 byte

View File

@@ -57,11 +57,10 @@
#define MODULES_OWI_MASTER
#define MODULES_DS18B20
#define MODULES_SK6812
;#define MODULES_MOTION_LIGHT
#define APPS_NETWORK
#define APPS_REPORTSENSORS
;#define APPS_STATS
#define APPS_STATS
#define APPS_MA_LIGHT
@@ -69,12 +68,6 @@
; ---------------------------------------------------------------------------
; defines for modules
;.equ VALUE_ID_SI7021_TEMP = 0x01
;.equ VALUE_ID_SI7021_HUM = 0x02
;.equ VALUE_ID_ADC = 0x03
;.equ VALUE_ID_REED1 = 0x04
;.equ VALUE_ID_REED2 = 0x05
.equ VALUE_ID_DS18B20_TEMP = 0x06
.equ VALUE_ID_MAL_STATE = 0x07
@@ -91,7 +84,7 @@
.equ VALUE_ID_LED_MODE = 0x8b
.equ VALUE_ID_LED_TYPE = 0x8c
.equ VALUE_ID_LEDSIMPLE_TIMING = 0x88

View File

@@ -45,7 +45,6 @@
.equ NET_MSGNUMINBUF_SIZE = 8 ; max buffer nums in ringbuffer (global incoming)
.equ NET_IFACE_OUTMSGBUF_SIZE = 8 ; max buffer nums in ringbuffer (per interface outbound)
.equ DEVICE_SEND_MINUTES = 3 ; send "DEVICE" message very 3 minutes
; ---------------------------------------------------------------------------