Skip to content

Commit 4cb902f

Browse files
committed
global upper and lower bound limit for DAC
1 parent be296b6 commit 4cb902f

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

m4/src/Config.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,15 @@ const static SPISettings DAC_SPI_SETTINGS(22000000, MSBFIRST, SPI_MODE1 );
4747
const static SPISettings ADC_SPI_SETTINGS(8000000, MSBFIRST, SPI_MODE0);
4848
#endif
4949

50+
// Global DAC voltage limits for fast clamping
51+
namespace DACLimits {
52+
inline float upper_voltage_limit = 10.0;
53+
inline float lower_voltage_limit = -10.0;
54+
55+
// Super fast inline clamp function using ternary operators
56+
inline float clampVoltage(float v) {
57+
return (v > upper_voltage_limit) ? upper_voltage_limit :
58+
((v < lower_voltage_limit) ? lower_voltage_limit : v);
59+
}
60+
}
61+

m4/src/Peripherals/DAC/DACChannel.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ class DACChannel {
4242
}
4343

4444
float setVoltage(float v) {
45+
// Super fast low-level clamp to global limits
46+
v = DACLimits::clampVoltage(v);
47+
4548
byte b1;
4649
byte b2;
4750
byte b3;
@@ -61,6 +64,9 @@ class DACChannel {
6164
}
6265

6366
void setVoltageNoTransactionNoLdac(float v) {
67+
// Super fast low-level clamp to global limits
68+
v = DACLimits::clampVoltage(v);
69+
6470
byte b1;
6571
byte b2;
6672
byte b3;

m4/src/Peripherals/DAC/DACController.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,17 @@ class DACController {
1717
inline static std::vector<DACChannel> dac_channels;
1818

1919
public:
20+
// Setter functions for global voltage limits
21+
inline static OperationResult setUpperLimit(float limit) {
22+
DACLimits::upper_voltage_limit = limit;
23+
return OperationResult::Success("UPPER_LIMIT_SET_TO_" + String(limit, 6));
24+
}
25+
26+
inline static OperationResult setLowerLimit(float limit) {
27+
DACLimits::lower_voltage_limit = limit;
28+
return OperationResult::Success("LOWER_LIMIT_SET_TO_" + String(limit, 6));
29+
}
30+
2031
inline static void initializeRegistry() {
2132
registerMemberFunction(setVoltage, "SET");
2233
registerMemberFunction(getVoltage, "GET_DAC");
@@ -28,6 +39,8 @@ class DACController {
2839
registerMemberFunction(autoRamp2, "RAMP2");
2940
registerMemberFunctionVector(autoRampN, "RAMP_N");
3041
registerMemberFunction(toggleLdacTest, "TOGGLE_LDAC");
42+
registerMemberFunction(setUpperLimit, "SET_UPPER_LIMIT");
43+
registerMemberFunction(setLowerLimit, "SET_LOWER_LIMIT");
3144
}
3245

3346
inline static void addChannel(int cs_pin) {
@@ -68,6 +81,9 @@ class DACController {
6881
return OperationResult::Failure("Invalid channel index " +
6982
String(channel_index));
7083
}
84+
// Fast clamp voltage to global limits
85+
voltage = DACLimits::clampVoltage(voltage);
86+
7187
DACChannel dac_channel = dac_channels[channel_index];
7288
if (voltage < dac_channel.getLowerBound() ||
7389
voltage > dac_channel.getUpperBound()) {
@@ -84,6 +100,9 @@ class DACController {
84100
if (!isChannelIndexValid(channel_index)) {
85101
return;
86102
}
103+
// Fast clamp voltage to global limits
104+
voltage = DACLimits::clampVoltage(voltage);
105+
87106
DACChannel dac_channel = dac_channels[channel_index];
88107
if (voltage < dac_channel.getLowerBound() ||
89108
voltage > dac_channel.getUpperBound()) {

0 commit comments

Comments
 (0)