Loading...
Searching...
No Matches
AnalogShield.h
Go to the documentation of this file.
1
10#pragma once
11
12#include <Arduino.h>
13
17#define ANALOG_SHIELD_VERSION "0.1.2"
18#define ANALOG_SHIELD_VERSION_MAJOR 0
19#define ANALOG_SHIELD_VERSION_MINOR 1
20#define ANALOG_SHIELD_VERSION_PATCH 2
22
27
31constexpr uint8_t TEMP_SIG_MCU = A0;
32constexpr uint8_t SENSOR_IN = A1;
33constexpr uint8_t TEMP_SIG_EXT = A3;
34constexpr uint8_t IR_VOUT = A4;
35constexpr uint8_t POT = A5;
37
40constexpr uint8_t ENC_SW = 2;
41constexpr uint8_t ENC_A = 3;
42constexpr uint8_t ENC_B = 4;
43constexpr uint8_t LED2 = 5;
44constexpr uint8_t LED1 = 6;
46
47} // namespace AnalogShieldPins
48
52enum class TempSource : uint8_t {
56};
57
65 public:
72 using EncoderCallback = void (*)(int32_t count, int8_t step);
73
79 using SwitchCallback = void (*)(bool pressed);
80
86 void begin();
87
91 void end();
92
95 static constexpr int32_t COUNTS_PER_DETENT = 2;
96 static constexpr int32_t DETENTS_PER_REV = 30;
98
103 [[nodiscard]] int32_t EncoderCount() const noexcept;
104
110 [[nodiscard]] int32_t EncoderDetents() const noexcept;
111
116 void SetEncoderCount(int32_t count = 0) noexcept;
117
122 void SetEncoderCallback(EncoderCallback callback) noexcept;
123
128 [[nodiscard]] bool SwitchPressed() const noexcept;
129
134 void SetSwitchCallback(SwitchCallback callback) noexcept;
135
146 bool SetAdcResolution(uint8_t bits);
147
152 [[nodiscard]] uint8_t AdcResolution() const noexcept;
153
158
165 [[nodiscard]] int TempRaw(TempSource source, uint8_t samples = 1) const;
166
175 [[nodiscard]] int32_t TempMillivolts(TempSource source, uint8_t samples = 1) const;
176
195 [[nodiscard]] float TempCelsius(TempSource source, uint8_t samples = 1) const;
196
203 [[nodiscard]] float TempFahrenheit(TempSource source, uint8_t samples = 1) const;
204
211 [[nodiscard]] float TempKelvin(TempSource source, uint8_t samples = 1) const;
212
218 [[nodiscard]] int PotRaw(uint8_t samples = 1) const;
219
227 [[nodiscard]] int32_t PotMillivolts(uint8_t samples = 1) const;
228
234 [[nodiscard]] float PotPercent(uint8_t samples = 1) const;
235
237
243 void LedON(uint8_t index, bool on = true) noexcept;
244
249 void LedToggle(uint8_t index) noexcept;
250
251 private:
253 static void OnEncoderA();
255 static void OnEncoderB();
257 static void EncoderStep(int8_t step);
259 static void OnSwitch();
261 [[nodiscard]] static float ReadAverage(uint8_t pin, uint8_t samples);
263 [[nodiscard]] float CountsToMillivolts(float counts, uint8_t pin) const noexcept;
265 [[nodiscard]] static float MillivoltsToCelsius(float mv, TempSource source) noexcept;
266
268 static AnalogShield* instance;
269
270 uint8_t adc_bits = 10;
271
272 volatile int32_t count = 0;
273 volatile EncoderCallback callback = nullptr;
274 volatile SwitchCallback switch_callback = nullptr;
275 volatile bool switch_pressed = false;
276};
TempSource
Temperature signal to read with AnalogShield::TempRaw() or AnalogShield::TempMillivolts().
Definition AnalogShield.h:52
@ MCU
Conditioned signal on TEMP_SIG_MCU (A0, RP2040 ADC), JP2 at Internal.
Definition AnalogShield.h:54
@ SENSOR
Raw sensor output on SENSOR_IN (A1).
Definition AnalogShield.h:53
@ EXT
Conditioned signal on TEMP_SIG_EXT (A3, MAX11635 external ADC on the Phyduino Pico),...
Definition AnalogShield.h:55
Encoder, switch, temperature signals, potentiometer and LEDs on the Analog Shield.
Definition AnalogShield.h:64
void(*)(bool pressed) SwitchCallback
Called when the encoder switch is pressed or released.
Definition AnalogShield.h:79
void LedON(uint8_t index, bool on=true) noexcept
Turns an LED on or off.
Definition AnalogShield.cpp:266
void(*)(int32_t count, int8_t step) EncoderCallback
Called on every encoder step.
Definition AnalogShield.h:72
int32_t TempMillivolts(TempSource source, uint8_t samples=1) const
Reads the voltage of a temperature signal.
Definition AnalogShield.cpp:216
static constexpr int32_t DETENTS_PER_REV
Clicks per full turn.
Definition AnalogShield.h:96
void end()
Stops tracking the encoder and its switch.
Definition AnalogShield.cpp:82
void SetEncoderCount(int32_t count=0) noexcept
Sets the encoder count, for example to zero it at a home position.
Definition AnalogShield.cpp:98
float TempKelvin(TempSource source, uint8_t samples=1) const
Reads a temperature signal and converts it to kelvins.
Definition AnalogShield.cpp:248
uint8_t AdcResolution() const noexcept
ADC resolution set by SetAdcResolution().
Definition AnalogShield.cpp:186
int32_t EncoderDetents() const noexcept
Encoder clicks (detents) since begin() or the last SetEncoderCount().
Definition AnalogShield.cpp:95
float TempFahrenheit(TempSource source, uint8_t samples=1) const
Reads a temperature signal and converts it to degrees Fahrenheit.
Definition AnalogShield.cpp:244
float TempCelsius(TempSource source, uint8_t samples=1) const
Reads a temperature signal and converts it to degrees Celsius.
Definition AnalogShield.cpp:240
void SetEncoderCallback(EncoderCallback callback) noexcept
Sets the function called on every encoder step.
Definition AnalogShield.cpp:104
int TempRaw(TempSource source, uint8_t samples=1) const
Reads a temperature signal with the ADC.
Definition AnalogShield.cpp:212
bool SetAdcResolution(uint8_t bits)
Sets the ADC resolution used by analogRead() and by every reading below.
Definition AnalogShield.cpp:171
int32_t EncoderCount() const noexcept
Encoder steps since begin() or the last SetEncoderCount().
Definition AnalogShield.cpp:93
float PotPercent(uint8_t samples=1) const
Reads the potentiometer position.
Definition AnalogShield.cpp:261
int32_t PotMillivolts(uint8_t samples=1) const
Reads the potentiometer wiper voltage.
Definition AnalogShield.cpp:256
int PotRaw(uint8_t samples=1) const
Reads the potentiometer RV1 with the ADC.
Definition AnalogShield.cpp:252
static constexpr int32_t COUNTS_PER_DETENT
EncoderCount() steps per click.
Definition AnalogShield.h:95
void begin()
Sets pin modes, turns both LEDs off and starts tracking the encoder and its switch.
Definition AnalogShield.cpp:56
bool SwitchPressed() const noexcept
Reads the encoder switch.
Definition AnalogShield.cpp:144
void SetSwitchCallback(SwitchCallback callback) noexcept
Sets the function called when the encoder switch is pressed or released.
Definition AnalogShield.cpp:146
void LedToggle(uint8_t index) noexcept
Toggles an LED.
Definition AnalogShield.cpp:272
Shield signals by Arduino pin, from the Rev-C schematic.
Definition AnalogShield.h:26
constexpr uint8_t SENSOR_IN
Raw temperature sensor (JP1: Internal U1 or External J1).
Definition AnalogShield.h:32
constexpr uint8_t ENC_SW
Rotary encoder push switch, active low (pulled up to VDD).
Definition AnalogShield.h:40
constexpr uint8_t LED1
D1 green LED, active low (anode to VDD).
Definition AnalogShield.h:44
constexpr uint8_t IR_VOUT
Sharp GP2Y0A21YK0F IR distance sensor (J2).
Definition AnalogShield.h:34
constexpr uint8_t LED2
D2 red LED, active low (anode to VDD).
Definition AnalogShield.h:43
constexpr uint8_t TEMP_SIG_EXT
Conditioned temperature signal, JP2 at External (MAX11635 ADC on the Phyduino Pico).
Definition AnalogShield.h:33
constexpr uint8_t ENC_B
Rotary encoder channel B, active low (pulled up to VDD).
Definition AnalogShield.h:42
constexpr uint8_t POT
Potentiometer RV1 wiper (0 V to VDD).
Definition AnalogShield.h:35
constexpr uint8_t TEMP_SIG_MCU
Conditioned temperature signal, JP2 at Internal (RP2040 ADC).
Definition AnalogShield.h:31
constexpr uint8_t ENC_A
Rotary encoder channel A, active low (pulled up to VDD).
Definition AnalogShield.h:41