; ***************************************************************************
; 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.   *
; ***************************************************************************


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

.equ APP_REPORT_SENSORS_INTERVAL_SECS = 180



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

.cseg


; ---------------------------------------------------------------------------
; @routine AppReportSensors_Init @global
;
; @clobbers r16

AppReportSensors_Init:
  clr r16
  sts reportSensorTimer, r16
  sec
  ret
; @end



; ---------------------------------------------------------------------------
; @routine AppReportSensors_Fini @global
;

AppReportSensors_Fini:
  ; nothing to do
  ret
; @end




; ---------------------------------------------------------------------------
; @routine AppReportSensors_OnEverySecond @global
;

AppReportSensors_OnEverySecond:
  push r15
    in r15, SREG
    cli
    rcall AppReportSensors_OnEverySecond_noIrqs
    out SREG, r15
  pop r15
  ret
AppReportSensors_OnEverySecond_noIrqs:
  lds r16, reportSensorTimer
  inc r16
  cpi r16, APP_REPORT_SENSORS_INTERVAL_SECS
  brcs AppReportSensors_OnEverySecond_store
  clr r16
AppReportSensors_OnEverySecond_store:
  sts reportSensorTimer, r16

  rcall appReportSensorsWalkTable
  ret
; @end



; ---------------------------------------------------------------------------
; @routine appReportSensorsWalkTable
;
; @clobbers r16, r18, r19, r22, Z, (any)

appReportSensorsWalkTable:
  ldi zl, LOW(appReportSensorsTable*2)
  ldi zh, HIGH(appReportSensorsTable*2)
  lds r16, reportSensorTimer
  bigcall WalkTable
  ret
; @end



; ---------------------------------------------------------------------------
; table
; time  routine (word address)

appReportSensorsTable:
#ifdef MODULES_SI7021
  .dw   1, SI7021_MeasureTemp
  .dw  11, SI7021_MeasureHumidity
  .dw  16, SI7021_SendTemperature
  .dw  21, SI7021_SendHumidity
#endif
#ifdef MODULES_MULTIFAN
  .dw  32, MultiFan_ReportSpeed1
  .dw  33, MultiFan_ReportSpeed2
  .dw  34, MultiFan_ReportSpeed3
#endif
#ifdef MODULES_SGP40
  .dw  53, SGP40_MeasureRawSignal
  .dw  63, SGP40_SendTVOC
#endif
#ifdef MODULES_SGP30
  .dw  53, SGP30_SendTVOC
  .dw  63, SGP30_SendCO2
#endif
#ifdef MODULES_DS18B20
  .dw  12, Ds18b20_StartMeasure
  .dw  46, Ds18b20_ReadResult
  .dw  84, Ds18b20_SendTemperature
#endif
#ifdef MODULES_CCS811
  .dw  94, CCS811_SendTVOC
  .dw 104, CCS811_SendCO2
#endif
#ifdef MODULES_BRIGHTNESS
  .dw  97, Brightness_Send
#endif
#ifdef APPS_DOOR
  .dw 100, appDoorSendValue
#endif
#ifdef APPS_MOTION
  .dw 108, appMotionSendValue
#endif
#ifdef APPS_LIGHTCONTROL
  .dw 110, AppLightControl_ReportState
#endif
#ifdef APPS_FANCONTROL
  .dw 115, AppFanControl_ReportState
#endif

  ; end of table
  .dw 0, 0



