avr: added brightness sensor
This commit is contained in:
@@ -37,6 +37,7 @@
|
||||
valsched
|
||||
xram
|
||||
heap
|
||||
brightness
|
||||
</subdirs>
|
||||
|
||||
</gwbuild>
|
||||
|
||||
12
avr/modules/brightness/0BUILD
Normal file
12
avr/modules/brightness/0BUILD
Normal file
@@ -0,0 +1,12 @@
|
||||
<?xml?>
|
||||
|
||||
<gwbuild>
|
||||
|
||||
<extradist>
|
||||
main.asm
|
||||
send.asm
|
||||
</extradist>
|
||||
|
||||
</gwbuild>
|
||||
|
||||
|
||||
130
avr/modules/brightness/main.asm
Normal file
130
avr/modules/brightness/main.asm
Normal file
@@ -0,0 +1,130 @@
|
||||
; ***************************************************************************
|
||||
; copyright : (C) 2025 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 BRIGHTNESS_INTERVAL = 97
|
||||
|
||||
.equ BRIGHTNESS_FLAGS_VALID_BIT = 7
|
||||
|
||||
|
||||
; ***************************************************************************
|
||||
; data
|
||||
|
||||
.dseg
|
||||
|
||||
brightnessDataBegin:
|
||||
brightnessTimer: .byte 1
|
||||
brightnessFlags: .byte 1
|
||||
brightnessLastValue: .byte 1
|
||||
brightnessDataEnd:
|
||||
|
||||
|
||||
|
||||
|
||||
; ***************************************************************************
|
||||
; code
|
||||
|
||||
.cseg
|
||||
|
||||
|
||||
|
||||
; ---------------------------------------------------------------------------
|
||||
; @routine Brightness_Init
|
||||
;
|
||||
; Init module.
|
||||
;
|
||||
; @return CFLAG always set
|
||||
|
||||
Brightness_Init:
|
||||
; preset SRAM data area
|
||||
ldi xh, HIGH(brightnessDataBegin)
|
||||
ldi xl, LOW(brightnessDataBegin)
|
||||
clr r16
|
||||
ldi r17, (brightnessDataEnd-brightnessDataBegin)
|
||||
rcall Utils_FillSram
|
||||
|
||||
; setup pins
|
||||
cbi BRIGHTNESS_ADC_PORT, BRIGHTNESS_ADC_PIN ; disable internal pullup for ADC
|
||||
cbi BRIGHTNESS_ADC_DDR, BRIGHTNESS_ADC_PIN ; set ADC port as input
|
||||
|
||||
ldi r16, BRIGHTNESS_ADC_MUX ; select input pin, use Vcc as reference voltage
|
||||
out ADMUX, r16
|
||||
ldi r16, (1 << ADLAR)
|
||||
out ADCSRB, r16
|
||||
ldi r16, (1 << ADEN) | (1 << ADPS1) | (1 << ADPS0) ; enable, prescaler 8
|
||||
out ADCSRA, r16
|
||||
|
||||
ldi r16, BRIGHTNESS_INTERVAL
|
||||
sts brightnessTimer, r16
|
||||
|
||||
sec
|
||||
ret
|
||||
|
||||
|
||||
|
||||
Brightness_Fini:
|
||||
sec
|
||||
ret
|
||||
|
||||
|
||||
|
||||
; ---------------------------------------------------------------------------
|
||||
; @routine Brightness_Every100ms @global
|
||||
;
|
||||
|
||||
Brightness_Every100ms:
|
||||
lds r16, brightnessTimer
|
||||
dec r16
|
||||
breq Brightness_Every100ms_readValue
|
||||
sts brightnessTimer, r16
|
||||
cpi r16, 1
|
||||
breq Brightness_Every100ms_startMeasure
|
||||
ret
|
||||
Brightness_Every100ms_startMeasure:
|
||||
sbi ADCSRA, ADSC ; start conversion
|
||||
ret
|
||||
Brightness_Every100ms_readValue:
|
||||
sbic ADCSRA, ADSC
|
||||
ret ; return if bit still set, leave brightnessTimer at "1"
|
||||
; conversion complete, read value
|
||||
ldi r16, BRIGHTNESS_INTERVAL ; restart timer
|
||||
sts brightnessTimer, r16
|
||||
in r16, ADCH ; read value from ADC
|
||||
sts brightnessLastValue, r16
|
||||
; convert to 1/0
|
||||
lds r17, brightnessFlags
|
||||
sbr r17, (1<<BRIGHTNESS_FLAGS_VALID_BIT) ; set valid bit
|
||||
sts brightnessFlags, r17
|
||||
ret
|
||||
; @end
|
||||
|
||||
|
||||
|
||||
; ---------------------------------------------------------------------------
|
||||
; @routine Brightness_GetValue @global
|
||||
;
|
||||
; @return CFLAG set if there is a value, cleared otherwise (standard api)
|
||||
; @return R16 value
|
||||
; @clobbers R16
|
||||
|
||||
Brightness_GetValue:
|
||||
lds r16, brightnessFlags
|
||||
sbrs r16, BRIGHTNESS_FLAGS_VALID_BIT
|
||||
rjmp Brightness_GetValue_retNc
|
||||
lds r16, brightnessLastValue
|
||||
sec
|
||||
ret
|
||||
Brightness_GetValue_retNc:
|
||||
clc
|
||||
ret
|
||||
; @end
|
||||
|
||||
|
||||
|
||||
31
avr/modules/brightness/send.asm
Normal file
31
avr/modules/brightness/send.asm
Normal file
@@ -0,0 +1,31 @@
|
||||
; ***************************************************************************
|
||||
; copyright : (C) 2025 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. *
|
||||
; ***************************************************************************
|
||||
|
||||
.cseg
|
||||
|
||||
|
||||
; ---------------------------------------------------------------------------
|
||||
; @routine Brightness_Send
|
||||
|
||||
Brightness_Send:
|
||||
rcall Brightness_GetValue
|
||||
brcc Brightness_Send_end
|
||||
mov r18, r16
|
||||
clr r19
|
||||
ldi r20, 1
|
||||
clr r21
|
||||
ldi r17, VALUE_ID_BRIGHTNESS ; VALUE ID
|
||||
ldi r22, AQHOME_VALUETYPE_LIGHT ; VALUE TYPE
|
||||
rcall Main_SendValueReport
|
||||
Brightness_Send_end:
|
||||
ret
|
||||
; @end
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user