summaryrefslogtreecommitdiff
path: root/src/battery/include/battery.hpp
blob: 32e02347977ec07a339966ed4b852764697a0b22 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/*
 * Copyright 2023 jacqueline <me@jacqueline.id.au>
 *
 * SPDX-License-Identifier: GPL-3.0-only
 */

#pragma once

#include <cstdint>

#include "freertos/FreeRTOS.h"
#include "freertos/timers.h"

#include "adc.hpp"
#include "samd.hpp"

namespace battery {

class Battery {
 public:
  Battery(drivers::Samd& samd, std::unique_ptr<drivers::AdcBattery> adc);
  ~Battery();

  auto Update() -> void;

  struct BatteryState {
    uint_fast8_t percent;
    bool is_charging;

    bool operator==(const BatteryState& other) const {
      return percent == other.percent && is_charging == other.is_charging;
    }
  };

  auto State() -> std::optional<BatteryState>;

 private:
  auto EmitEvent() -> void;

  drivers::Samd& samd_;
  std::unique_ptr<drivers::AdcBattery> adc_;

  TimerHandle_t timer_;
  std::mutex state_mutex_;
  std::optional<BatteryState> state_;
};

}  // namespace battery