summaryrefslogtreecommitdiff
path: root/minion/src/temperature.hpp
blob: a4eea715cd11bbda342bf77c9bdfc7db9484f2d3 (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
#pragma once

#include <stddef.h>
#include <stdint.h>

namespace TemperatureSensorTiming
{

constexpr auto START_SIGNAL_TIME_MILLIS = 18U;
constexpr auto RESPONSE_SIGNAL_WAIT_TIME_MICROS = 40U;
constexpr auto RESPONSE_SIGNAL_TIME_MICROS = 80U;
constexpr auto START_TO_TRANSMIT_SIGNAL_TIME_MICROS = 50U;
constexpr auto DATA_TRANSMIT_TIMEOUT_MICROS = 80U;

} // namespace TemperatureSensorTiming

constexpr auto TEMPERATURE_SENSOR_RESPONSE_DATA_BYTE_CNT = 5U;
constexpr auto BITS_IN_BYTE = 8U;

enum class TemperatureSensorStatus
{
	OK,
	LOW_RESPONSE_SIGNAL_TIMEOUT,
	HIGH_RESPONSE_SIGNAL_TIMEOUT,
	START_TO_TRANSMIT_SIGNAL_TIMEOUT,
	DATA_TRANSMIT_TIMEOUT,
	CHECKSUM_ERROR
};

class TemperatureSensor
{
public:
	explicit TemperatureSensor(uint8_t pin) noexcept;

	auto read_temperature() noexcept -> TemperatureSensorStatus;

	auto temperature() const noexcept -> uint8_t;

private:
	const uint8_t _pin;

	uint8_t _temperature{};

	auto _wait_read(uint8_t level, size_t timeout_micros) noexcept -> bool;

	void _restore_pin() noexcept;
};