; ***************************************************************************
; copyright   : (C) 2023 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.   *
; ***************************************************************************


; doesn't work, yet



; ***************************************************************************
; data

.dseg


.equ CNY70_FLAGS_ADC_STARTED  = 0x01
.equ CNY70_FLAGS_ADC_UPDATED  = 0x02

cny70DataBegin:
  cny70Flags:        .byte 1
  cny70LastData:     .byte 1
cny70DataEnd:




; ***************************************************************************
; code

.cseg



; ---------------------------------------------------------------------------
; Init module.
;
; IN:
; - nothing
; OUT:
; - CFLAG: set if okay, clear on error
; USED:

CNY70_Init:
  ; preset SRAM data area
  ldi xh, HIGH(cny70DataBegin)
  ldi xl, LOW(cny70DataBegin)
  clr r16
  ldi r17, (cny70DataEnd-cny70DataBegin)
  rcall Utils_FillSram

  ; setup pins
  sbi CNY70_LED_DDR, CNY70_LED_PIN                ; set DATA port as output
  sbi CNY70_LED_PORT, CNY70_LED_PIN               ; LED off
  cbi CNY70_ADC_PORT, CNY70_ADC_PIN               ; disable internal pullup for ADC
  cbi CNY70_ADC_DDR, CNY70_ADC_PIN                ; set ADC port as input

  ldi r16, CNY70_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
  
  sec
  ret



CNY70_Fini:
  sec
  ret



CNY70_Run:
  lds r16, cny70Flags
  andi r16, CNY70_FLAGS_ADC_UPDATED ; new value?
  breq CNY70_Run_done               ; nope, jump
  rcall CNY70_SendAdc               ; send value
  brcc CNY70_Run_done               ; jump on error
  lds r16, cny70Flags               ; value sent, clear flag
  andi r16, ~CNY70_FLAGS_ADC_UPDATED
  sts cny70Flags, r16
CNY70_Run_done:
  clc
  ret



CNY70_OnTimer:
  lds r16, cny70Flags
  andi r16, CNY70_FLAGS_ADC_STARTED
  breq CNY70_OnTimer_startConversion
  ; conversion is running, complete?
  sbic ADCSRA, ADSC
  ret ; only if bit still set
  
  ; conversion complete, read value, set flags
  lds r16, cny70Flags
  andi r16, ~CNY70_FLAGS_ADC_STARTED
  ori r16, CNY70_FLAGS_ADC_UPDATED
  sts cny70Flags, r16
  in r16, ADCH                         ; read value from ADC
  sts cny70LastData, r16
  sbi CNY70_LED_PORT, CNY70_LED_PIN ; LED off
  nop
  nop
  nop
  ret

CNY70_OnTimer_startConversion:
  cbi CNY70_LED_PORT, CNY70_LED_PIN ; LED on
  lds r16, cny70Flags
  ori r16, CNY70_FLAGS_ADC_STARTED
  sts cny70Flags, r16
  sbi ADCSRA, ADSC                     ; start conversion
  ret



CNY70_SendAdc:
  ldi r16, 0xff                      ; destination address
  ldi r17, VALUE_ID_ADC              ; value id
  ldi r22, AQHOME_VALUETYPE_UNKNOWN
  lds r18, cny70LastData             ; value
  clr r19
  ldi r20, 1                         ; denominator
  clr r21
  ldi xl, LOW(com2SendBuffer)
  ldi xh, HIGH(com2SendBuffer)
  rcall CPRO_WriteReportValue
  rjmp COM2_SendPacket




