; ***************************************************************************
; 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 2
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
  outr ADMUX, r16
  ldi r16, (0 << ADLAR)
  outr ADCSRB, r16
  ldi r16, (1 << ADEN) | (1 << ADPS1) | (1 << ADPS0)         ; enable, prescaler 8
  outr 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:
  inr r16, ADCSRA                      ; start conversion
  sbr r16, (1<<ADSC)
  outr ADCSRA, r16
;  sbi ADCSRA, ADSC                     ; start conversion
  ret
Brightness_Every100ms_readValue:
  inr r16, ADCSRA
  sbrc r16, ADSC
    ret ; return if bit still set, leave brightnessTimer at "1"
  ; conversion complete, read value
  ldi r16, BRIGHTNESS_INTERVAL                 ; restart timer
  sts brightnessTimer, r16
  inr r16, ADCL
  inr r17, ADCH                                 ; read value from ADC
  sts brightnessLastValue, r16
  sts brightnessLastValue+1, r17
  ; 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 R19:R18 value
; @clobbers R16

Brightness_GetValue:
  lds r16, brightnessFlags
  sbrs r16, BRIGHTNESS_FLAGS_VALID_BIT
    rjmp Brightness_GetValue_retNc
  lds r18, brightnessLastValue
  lds r19, brightnessLastValue+1
  sec
  ret
Brightness_GetValue_retNc:
  clc
  ret
; @end



