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


; ***************************************************************************
; Source file for LED controller node on AtTiny 85
;
; This is for the full system (i.e. not the boot loader).
; ***************************************************************************

.equ clock=8000000 ; Define the clock frequency



.nolist
.include "include/tn85def.inc" ; Define device ATtiny85
.list

.include "version.asm"
.include "../defs.asm"
.include "../eeprom.asm"
;.include "./data.asm"

.include "devices/all/defs.asm"

.include "common/calls.asm"
.include "common/utils_io.asm"
.include "common/utils_wait.asm"



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

; ---------------------------------------------------------------------------
; generic

.equ NET_BUFFERS_NUM            =  6
.equ NET_MSGNUMINBUF_SIZE       =  8 ; max buffer nums in ringbuffer (global incoming)
.equ NET_IFACE_OUTMSGBUF_SIZE   =  8 ; max buffer nums in ringbuffer (per interface outbound)



; ---------------------------------------------------------------------------
; firmware settings including list of modules used

#define MODULES_CLOCK
#define MODULES_LED1
#define MODULES_NETWORK
#define MODULES_COM2W
#define MODULES_OWI_MASTER
#define MODULES_DS18B20
#define MODULES_SK6812

#define APPS_NETWORK
#define APPS_REPORTSENSORS
#define APPS_STATS
#define APPS_LIGHTCONTROL



; ---------------------------------------------------------------------------
; defines for modules

.equ VALUE_ID_DS18B20_TEMP    = 0x06

.equ VALUE_ID_LIGHTCONTROL_STATE    = 0x07
.equ VALUE_ID_LIGHTCTRL_NUMLEDS     = 0x82
.equ VALUE_ID_LIGHTCTRL_VALUE       = 0x84
.equ VALUE_ID_LIGHTCTRL_HOLDTIME    = 0x85
.equ VALUE_ID_LIGHTCTRL_TRIGGER1SRC = 0x86
.equ VALUE_ID_LIGHTCTRL_TRIGGER2SRC = 0x87
.equ VALUE_ID_LIGHTCTRL_GUARDSRC    = 0x89
.equ VALUE_ID_LIGHTCTRL_MODE        = 0x8b
.equ VALUE_ID_LIGHTCTRL_GUARDLIM    = 0x8c
.equ VALUE_ID_LIGHTCTRL_TRIGGER1LIM = 0x8d
.equ VALUE_ID_LIGHTCTRL_TRIGGER2LIM = 0x8e





; ***************************************************************************
; code segment

.cseg
.org 000000



; ---------------------------------------------------------------------------
; Reset and interrupt vectors

        rjmp BOOTLOADER_ADDR ; Reset vector  ; use this for flashed system
	reti ; EXT_INT0
        rjmp com2wPcintIsr         ; PCI0
	reti ; OC1A
	reti ; OVF1
	reti ; OVF0
	reti ; ERDY
	reti ; ACI
        reti ; ADCC
	reti ; OC1B
        rjmp baseTimerIrqOC0A ; OC0A
	reti ; OC0B
	reti ; WATCHDOG
	reti ; USI_STR
	reti ; USI_OVF


devInfoBlock:        ; 12 bytes
devInfoManufacturer: .db 'A', 'Q', 'U', 'A'
devInfoId:           .db DEVICEINFO_ID, 0
devInfoVersion:      .db DEVICEINFO_VERSION, DEVICEINFO_REVISION             ; version, revision
firmwareVersion:     .db FIRMWARE_VARIANT_LEDSTRIPS, FIRMWARE_VERSION_MAJOR
                     .db FIRMWARE_VERSION_MINOR, FIRMWARE_VERSION_PATCHLEVEL

; ---------------------------------------------------------------------------
; @routine firmwareStart @global

firmwareStart:
  rjmp main
; @end



; ---------------------------------------------------------------------------
; @routine onSystemStart

onSystemStart:
  ret
; @end



; ---------------------------------------------------------------------------
; @routine onMessageReceived
;
; Called on every message received

onMessageReceived:
  clc
  ret
; @end



; ---------------------------------------------------------------------------
; @routine onEvery100ms
;
; Called every 100ms. Add your routine calls here. No arguments, no results.

onEvery100ms:
onEveryMinute:
onEveryHour:
onEveryDay:
  ret
; @end

onEverySecond:
  ret
  ; debug
;  ldi r19, 0x00           ; G
;  ldi r18, 0xff           ; R
;  ldi r20, 0x55           ; B
;  ldi r21, 0xaa           ; W
;  rcall SK6812_SetRGBW    ; r23 (r16, r17)
;  ret



; ---------------------------------------------------------------------------
; @routine onEveryLoop
;
; Called on every loop (i.e. after awakening from sleep).
;
onEveryLoop:
  ret
; @end





; ***************************************************************************
; includes

.include "devices/all/hw_tn85.asm"
.include "devices/all/includes.asm"
;.include "common/debug.asm"
.include "common/eeprom_tlv.asm"



; ---------------------------------------------------------------------------
; defines for network interface

.equ netInterfaceData = com2w_iface




deviceCodeEnd:
  .if deviceCodeEnd >= BOOTLOADER_ADDR
    .warning "Code reaches into boot loader!"
  .endif


