// (C) 2026 Philip Endecott.
// Distributed under the Boost Software License, Version 1.0.
// See accompanying file LICENCE.txt or copy at https://www.boost.org/LICENSE_1_0.txt

#ifndef lipstick_opentherm_hh
#define lipstick_opentherm_hh

#include "power.hh"

#include <cstdint>


namespace opentherm {

constexpr auto power_bits = PCTIM1;

void setup();

__attribute__((interrupt("IRQ")))
__attribute__((target("arm")))
void timer_irq_handler();

void start_sync_process();

//void handle_interrupt(uint8_t interrupt_bits);


uint16_t get_register(uint16_t address);

void set_register(uint16_t address, uint16_t value);

enum class Register: uint16_t {
  // Note that an offset of 16 is applied to the addresses in boiler.cc.

  // All registers are writable with set_register() and 
  // readable with get_register() above, but normally one 
  // or other operation makes sense.
  // The following are primarily written to:
  ch_enable,             // Write 0 = off, 1 = on; or write 256 = on, 512 = off 
                         // for compatibility with how the GPIO outputs are used. 
                         // Reading returns the status value from the boiler, which 
                         // includes bits for DHW on, flame on, etc; these could be useful.
  ch_flow_target_temp,
  dhw_target_temp,

  // These are primarily read from:
  ch_flow_temp,
  ch_return_temp,
  outside_temp,
  ch_pressure,
  modulation,

  // The temperatures, pressure and percentage are in fixed point decimal 
  // with one decimal place, e.g. 256 = 25.6 degrees, 17 = 1.7 bar, 999 = 99.9%.

  // These report the status of this device's OpenTherm code, 
  // rather than the boiler; they are also readable with get_register():
  error_count = 16,  // Can be written to, e.g. to reset it.
  last_error,        // See RecieveError enum in opentherm.cc; note 0 = OK.
};


}; // namespace opentherm

#endif

