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



; ***************************************************************************
; defines

.equ APPCO2WATCH_WARN = 1000
.equ APPCO2WATCH_CRIT = 2000

.equ APPCO2WATCH_LEVEL_OK   = 0
.equ APPCO2WATCH_LEVEL_WARN = 1
.equ APPCO2WATCH_LEVEL_CRIT = 2

.equ APPCO2WATCH_LED_ONTIME  = 3
.equ APPCO2WATCH_LED_OFFTIME = 17



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

.dseg

appCo2WatchDatBegin:
  appCo2WatchLimitWarnLo: .byte 1
  appCo2WatchLimitWarnHi: .byte 1
  appCo2WatchLimitCritLo: .byte 1
  appCo2WatchLimitCritHi: .byte 1
appCo2WatchDatEnd:




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

.cseg


AppCo2Watch_Init:
  ldi r16, LOW(APPCO2WATCH_WARN)
  ldi r17, HIGH(APPCO2WATCH_WARN)
  sts appCo2WatchLimitWarnLo, r16
  sts appCo2WatchLimitWarnHi, r17

  ldi r16, LOW(APPCO2WATCH_CRIT)
  ldi r17, HIGH(APPCO2WATCH_CRIT)
  sts appCo2WatchLimitCritLo, r16
  sts appCo2WatchLimitCritHi, r17

  ldi r18, 50  ; 5secs on
  ldi r19, 1   ; 100ms off
  ldi r20, 1   ; go into standard heartbeat mode after period
  bigcall LED1_SetTiming

  ldi r18, 40  ; 4secs on
  ldi r19, 1   ; 100ms off
  ldi r20, 255 ; turn off after period
  bigcall LED2_SetTiming

  ldi r18, 30  ; 3secs on
  ldi r19, 1   ; 100ms off
  ldi r20, 255 ; turn off after period
  bigcall LED3_SetTiming
  
  sec
  ret
; @end



AppCo2Watch_OnEveryMinute:
  ldi r16, SGP30_VALUE_CO2
  bigcall SGP30_GetValue   ; R19:R18=value, R21:R20=denom
  ldi r23, APPCO2WATCH_LEVEL_OK
  brcc AppCo2Watch_OnEveryMinute_ret
  lds r16, appCo2WatchLimitCritLo
  lds r17, appCo2WatchLimitCritHi
  cp r18, r16
  cpc r19, r17
  brcs AppCo2Watch_OnEveryMinute_next ; below critical
  ldi r23, APPCO2WATCH_LEVEL_CRIT
  rjmp AppCo2Watch_OnEveryMinute_setLed
AppCo2Watch_OnEveryMinute_next:
  lds r16, appCo2WatchLimitWarnLo
  lds r17, appCo2WatchLimitWarnHi
  cp r18, r16
  cpc r19, r17
  brcs AppCo2Watch_OnEveryMinute_setLed
  ldi r23, APPCO2WATCH_LEVEL_WARN
AppCo2Watch_OnEveryMinute_setLed:
  cpi r23, APPCO2WATCH_LEVEL_OK
  breq AppCo2Watch_OnEveryMinute_setGreen
  cpi r23, APPCO2WATCH_LEVEL_WARN
  breq AppCo2Watch_OnEveryMinute_setYellow
  cpi r23, APPCO2WATCH_LEVEL_CRIT
  breq AppCo2Watch_OnEveryMinute_setRed
AppCo2Watch_OnEveryMinute_setGreen:
  rcall appCo2WatchLed2Off
  rcall appCo2WatchLed3Off
  rjmp AppCo2Watch_OnEveryMinute_ret
AppCo2Watch_OnEveryMinute_setYellow:
  rcall appCo2WatchLed2On
  rcall appCo2WatchLed3Off
  rjmp AppCo2Watch_OnEveryMinute_ret
AppCo2Watch_OnEveryMinute_setRed:
  rcall appCo2WatchLed2Off
  rcall appCo2WatchLed3On
AppCo2Watch_OnEveryMinute_ret:
  ret
; @end




appCo2WatchLed2On:
  ldi r18, APPCO2WATCH_LED_ONTIME
  ldi r19, APPCO2WATCH_LED_OFFTIME
  clr r20
  bigjmp Led2_SetTiming

appCo2WatchLed2Off:
  clr r18
  clr r19
  clr r20
  bigjmp Led2_SetTiming

appCo2WatchLed3On:
  ldi r18, APPCO2WATCH_LED_ONTIME
  ldi r19, APPCO2WATCH_LED_OFFTIME
  clr r20
  bigjmp Led3_SetTiming

appCo2WatchLed3Off:
  clr r18
  clr r19
  clr r20
  bigjmp Led3_SetTiming




