; ***************************************************************************
; 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_FINISHED = 0x02
.equ CNY70_FLAGS_ADC_VALID    = 0x04

.equ CNY70_TIMER_100MS        = 20 ; every 2s


cny70DataBegin:
  cny70Flags:        .byte 1
  cny70LastData:     .byte 1
  cny70TimerCounter: .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 and interrupts
  sbi CNY70_DDR_LED, CNY70_PINNUM_LED         ; set DATA port as output
  cbi CNY70_PORT_LED, CNY70_PINNUM_LED        ; LED off
  cbi CNY70_PORT_ADC, CNY70_PINNUM_ADC        ; disable internal pullup for ADC
  cbi CNY70_DDR_ADC, CNY70_PINNUM_ADC         ; set ADC port as input

  ldi r16, (1<<ADLAR) | (1<<CNY70_ADCSRB_ADC) ; "8 bit" mode, disable digital pin input buffer 
  out ADCSRB, r16

  sec
  ret



CNY70_Fini:
  sec
  ret



CNY70_Run:
  ret



CNY70_Every100ms:
  push r15
    in r15, SREG
    cli
    lds r16, cny70Flags
    andi r16, CNY70_FLAGS_ADC_STARTED
    brne CNY70_Every100ms_done
    
    lds r16, cny70TimerCounter
    inc r16
    cpi r16, CNY70_TIMER_100MS
    brcc CNY70_Every100ms_startConversion
    sts cny70TimerCounter, r16
    rjmp CNY70_Every100ms_done

CNY70_Every100ms_startConversion:
    clr r16
    sts cny70TimerCounter, r16
    rcall cny70StartConversion
CNY70_Every100ms_done:
    out SREG, r15
  pop r15
  ret



cny70StartConversion:
  sbi CNY70_PORT_LED, CNY70_PINNUM_LED ; LED on
  nop
  nop
  nop
  ; setup adc
  ldi r16, CNY70_MUX_ADC
  out ADMUX, r16
  ; enable adc, enable irq, clear irq, select prescaler 8 (125kHz)
  ldi r16, (1<<ADEN) | (1<<ADIE) | (1<<ADIF) | (1<<ADPS1) | (1<<ADPS0)
  out ADCSRA, r16

  sbic ADCSRA, ADSC ; start conversion

  push r15
    in r15, SREG
    cli
    lds r16, cny70Flags
    ori r16, CNY70_FLAGS_ADC_STARTED
    andi r16, ~CNY70_FLAGS_ADC_FINISHED
    sts cny70Flags, r16
    out SREG, r15
  pop r15
  ret



cny70Isr:
  push r15
    in r15, SREG
    push r16
      cbi CNY70_PORT_LED, CNY70_PINNUM_LED ; LED off
      in r16, ADCH
      sts cny70LastData, r16    ; store value
      clr r16                   ; disable ADC
      out ADCSRA, r16
      lds r16, cny70Flags
      ori r16, CNY70_FLAGS_ADC_FINISHED | CNY70_FLAGS_ADC_VALID
      andi r16, ~CNY70_FLAGS_ADC_STARTED
      sts cny70Flags, r16
    pop r16
    out SREG, r15
  pop r15
  reti





CNY70_SendAdc:
  lds r16, cny70Flags
  andi r16, CNY70_FLAGS_ADC_VALID
  brne CNY70_SendAdc_haveValue
  sec
  ret
CNY70_SendAdc_haveValue:
  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_WriteValue
  rjmp COM2_SendPacket




