-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathanalog_output.h
More file actions
95 lines (81 loc) · 2.32 KB
/
analog_output.h
File metadata and controls
95 lines (81 loc) · 2.32 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/**
* @file analog_output.h
* @author Adam Wonak (https://github.com/awonak)
* @brief Class for interacting with analog control voltage outputs.
* @version 0.1
* @date 2024-06-28
*
* @copyright Copyright (c) 2024
*
*/
#ifndef ANALOGL_OUTPUT_H
#define ANALOG_OUTPUT_H
#include <Arduino.h>
namespace modulove {
const int MAX_OUTPUT = (1 << 8) - 1; // Max 8 bit output resolution.
const int MAX_OUTPUT_10BIT = (1 << 10) - 1; // Max 10 bit output resolution.
class AnalogOutput {
public:
/**
* @brief Initializes an Analog CV Output object.
*
* @param pin gpio pin for the cv output
*/
void Init(uint8_t pin) {
pinMode(pin, OUTPUT); // Analog CV Output
cv_pin_ = pin;
}
/**
* @brief Initializes an LED & CV Output paired object.
*
* @param cv_pin gpio pin for the cv output
* @param led_pin gpio pin for the LED
*/
void Init(uint8_t cv_pin, uint8_t led_pin) {
pinMode(led_pin, OUTPUT); // LED
led_pin_ = led_pin;
#define LED_PIN_DEFINED
Init(cv_pin);
}
/**
* @brief Set the output pin to the given 8 bit value.
*
* @param val Arduino analog value between 0 and 255 (0..10v).
*/
inline void Update(int val) {
update((val <= MAX_OUTPUT) ? val : MAX_OUTPUT);
}
/**
* @brief Set the output pin to the given 10 bit value.
*
* @param val Arduino analog value between 0 and 1024 (0..10v).
*/
inline void Update10bit(int val) {
val = val <= MAX_OUTPUT_10BIT ? val : MAX_OUTPUT_10BIT;
val = map(val, 0, MAX_OUTPUT_10BIT, 0, MAX_OUTPUT);
update(val);
}
/// @brief Sets the cv output HIGH to about 10v.
inline void High() { update(MAX_OUTPUT); }
/// @brief Sets the cv output LOW to 0v.
inline void Low() { update(0); }
/**
* @brief Return an integer value between 0 and 1023 (0..10v) representing the current value of the output.
*
* @return integer value of cv from 0 to 1023.
*/
inline uint16_t GetValue() { return cv_; }
private:
uint8_t cv_pin_;
uint8_t led_pin_;
uint8_t cv_;
void update(uint16_t val) {
cv_ = val;
analogWrite(cv_pin_, cv_);
#ifdef LED_PIN_DEFINED
analogWrite(led_pin_, cv_);
#endif
}
};
} // namespace modulove
#endif