Skip to content

Commit 41b0208

Browse files
committed
added better sample
1 parent 2a38725 commit 41b0208

1 file changed

Lines changed: 53 additions & 64 deletions

File tree

samples/dac/main.cpp

Lines changed: 53 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
#include <core/io/UART.hpp>
1515
#include <core/io/pin.hpp>
1616
#include <core/manager.hpp>
17-
#include <core/utils/log.hpp>
1817
#include <core/utils/time.hpp>
1918

2019
#ifdef STM32F3xx
@@ -27,66 +26,62 @@
2726

2827
namespace io = core::io;
2928
namespace time = core::time;
30-
namespace log = core::log;
3129

3230
constexpr float TOLERANCE = 0.1f; // 100mV tolerance for ADC readings
3331

34-
bool testDACInitialization(io::DACBase& dac) {
32+
bool testDACInitialization(io::DACBase& dac, io::UART& uart) {
3533
// Test 1: Verify DAC starts at zero
3634
float initialVoltage = dac.getVoltage();
3735
bool passed = (initialVoltage < 0.1f); // Should be near 0V
38-
log::LOGGER.log(log::Logger::LogLevel::INFO, "DAC Init Test: %s (%.2fV)", passed ? "PASS" : "FAIL", initialVoltage);
36+
uart.printf("DAC Init Test: %s (%dmV)\r\n", passed ? "PASS" : "FAIL", (int)(initialVoltage * 1000));
3937
return passed;
4038
}
4139

42-
bool testDACSetValue(io::DACBase& dac, uint32_t value, float expectedVoltage) {
40+
bool testDACSetValue(io::DACBase& dac, uint32_t value, float expectedVoltage, io::UART& uart) {
4341
dac.setValue(value);
4442
float actualVoltage = dac.getVoltage();
4543
float difference =
4644
(actualVoltage > expectedVoltage) ? (actualVoltage - expectedVoltage) : (expectedVoltage - actualVoltage);
4745
bool passed = (difference < TOLERANCE);
48-
log::LOGGER.log(log::Logger::LogLevel::INFO,
49-
"DAC setValue(%d): %s (Expected: %.2fV, Actual: %.2fV, Diff: %.2fV)",
50-
value,
51-
passed ? "PASS" : "FAIL",
52-
expectedVoltage,
53-
actualVoltage,
54-
difference);
46+
uart.printf("DAC setValue(%d): %s (Expected: %dmV, Actual: %dmV, Diff: %dmV)\r\n",
47+
value,
48+
passed ? "PASS" : "FAIL",
49+
(int)(expectedVoltage * 1000),
50+
(int)(actualVoltage * 1000),
51+
(int)(difference * 1000));
5552
return passed;
5653
}
5754

58-
bool testDACSetVoltage(io::DACBase& dac, float voltage) {
55+
bool testDACSetVoltage(io::DACBase& dac, float voltage, io::UART& uart) {
5956
dac.setVoltage(voltage);
6057
float actualVoltage = dac.getVoltage();
6158
float difference = (actualVoltage > voltage) ? (actualVoltage - voltage) : (voltage - actualVoltage);
6259
bool passed = (difference < TOLERANCE);
63-
log::LOGGER.log(log::Logger::LogLevel::INFO,
64-
"DAC setVoltage(%.2fV): %s (Actual: %.2fV, Diff: %.2fV)",
65-
voltage,
66-
passed ? "PASS" : "FAIL",
67-
actualVoltage,
68-
difference);
60+
uart.printf("DAC setVoltage(%dmV): %s (Actual: %dmV, Diff: %dmV)\r\n",
61+
(int)(voltage * 1000),
62+
passed ? "PASS" : "FAIL",
63+
(int)(actualVoltage * 1000),
64+
(int)(difference * 1000));
6965
return passed;
7066
}
7167

72-
bool testADCReading(io::ADC& adc, float expectedVoltage) {
68+
bool testADCReading(io::ADC& adc, float expectedVoltage, io::UART& uart) {
7369
float adcVoltage = adc.read();
7470
float difference = (adcVoltage > expectedVoltage) ? (adcVoltage - expectedVoltage) : (expectedVoltage - adcVoltage);
7571
bool passed = (difference < TOLERANCE);
76-
log::LOGGER.log(log::Logger::LogLevel::INFO,
77-
"ADC Reading: %s (Expected: %.2fV, Actual: %.2fV, Diff: %.2fV)",
78-
passed ? "PASS" : "FAIL",
79-
expectedVoltage,
80-
adcVoltage,
81-
difference);
72+
uart.printf("ADC Reading: %s (Expected: %dmV, Actual: %dmV, Diff: %dmV)\r\n",
73+
passed ? "PASS" : "FAIL",
74+
(int)(expectedVoltage * 1000),
75+
(int)(adcVoltage * 1000),
76+
(int)(difference * 1000));
8277
return passed;
8378
}
8479

85-
bool testDALoopback(io::DACBase& dac, io::ADC& adc, float testVoltage) {
86-
log::LOGGER.log(log::Logger::LogLevel::INFO, "DAC-ADC Loopback Test: %.2fV", testVoltage);
80+
bool testDALoopback(io::DACBase& dac, io::ADC& adc, float testVoltage, io::UART& uart) {
81+
uart.printf("DAC-ADC Loopback Test: %dmV\r\n", (int)(testVoltage * 1000));
8782
dac.setVoltage(testVoltage);
8883
time::wait(10); // Allow settling time
89-
return testADCReading(adc, testVoltage);
84+
return testADCReading(adc, testVoltage, uart);
9085
}
9186

9287
int main() {
@@ -95,15 +90,10 @@ int main() {
9590
// Initialize UART for logging
9691
io::UART& uart = io::getUART<io::Pin::UART_TX, io::Pin::UART_RX>(9600);
9792

98-
// Set up the logger
99-
log::LOGGER.setUART(&uart);
100-
log::LOGGER.setLogLevel(log::Logger::LogLevel::INFO);
10193

10294
uart.printf("Starting DAC Comprehensive Test\r\n");
10395
uart.printf("Hardware: DAC_OUT(PA4) -> ADC_IN(PA0)\r\n");
104-
uart.printf("Tolerance: %.1fV\r\n\r\n", TOLERANCE);
105-
106-
time::wait(500);
96+
uart.printf("Tolerance: %dmV\r\n\r\n", (int)(TOLERANCE * 1000));
10797

10898
// Initialize DAC and ADC
10999
#ifdef STM32F3xx
@@ -115,39 +105,39 @@ int main() {
115105
#endif
116106

117107
// Test 1: DAC Initialization
118-
log::LOGGER.log(log::Logger::LogLevel::INFO, "--- Test 1: DAC Initialization ---");
119-
testDACInitialization(dac);
108+
uart.printf("--- Test 1: DAC Initialization ---\r\n");
109+
testDACInitialization(dac, uart);
120110

121111
// Test 2: DAC setValue() functionality
122-
log::LOGGER.log(log::Logger::LogLevel::INFO, "--- Test 2: DAC setValue() Functionality ---");
123-
testDACSetValue(dac, 0, 0.0f);
124-
testDACSetValue(dac, 1024, 0.82f); // ~0.82V
125-
testDACSetValue(dac, 2048, 1.65f); // ~1.65V
126-
testDACSetValue(dac, 3072, 2.47f); // ~2.47V
127-
testDACSetValue(dac, 4095, 3.3f); // ~3.3V
112+
uart.printf("--- Test 2: DAC setValue() Functionality ---\r\n");
113+
testDACSetValue(dac, 0, 0.0f, uart);
114+
testDACSetValue(dac, 1024, 0.82f, uart); // ~0.82V
115+
testDACSetValue(dac, 2048, 1.65f, uart); // ~1.65V
116+
testDACSetValue(dac, 3072, 2.47f, uart); // ~2.47V
117+
testDACSetValue(dac, 4095, 3.3f, uart); // ~3.3V
128118

129119
// Test 3: DAC setVoltage() functionality
130-
log::LOGGER.log(log::Logger::LogLevel::INFO, "--- Test 3: DAC setVoltage() Functionality ---");
131-
testDACSetVoltage(dac, 0.5f);
132-
testDACSetVoltage(dac, 1.0f);
133-
testDACSetVoltage(dac, 2.0f);
134-
testDACSetVoltage(dac, 3.0f);
120+
uart.printf("--- Test 3: DAC setVoltage() Functionality ---\r\n");
121+
testDACSetVoltage(dac, 0.5f, uart);
122+
testDACSetVoltage(dac, 1.0f, uart);
123+
testDACSetVoltage(dac, 2.0f, uart);
124+
testDACSetVoltage(dac, 3.0f, uart);
135125

136126
// Test 4: Input validation (should clamp to max)
137-
log::LOGGER.log(log::Logger::LogLevel::INFO, "--- Test 4: Input Validation ---");
127+
uart.printf("--- Test 4: Input Validation ---\r\n");
138128
dac.setValue(5000); // Should clamp to 4095
139-
log::LOGGER.log(log::Logger::LogLevel::INFO, "DAC setValue(5000): Clamped to %d", dac.getValue());
129+
uart.printf("DAC setValue(5000): Clamped to %d\r\n", dac.getValue());
140130
dac.setVoltage(5.0f); // Should clamp to 3.3V
141-
log::LOGGER.log(log::Logger::LogLevel::INFO, "DAC setVoltage(5.0V): Clamped to %.2fV", dac.getVoltage());
131+
uart.printf("DAC setVoltage(5.0V): Clamped to %dmV\r\n", (int)(dac.getVoltage() * 1000));
142132

143133
// Test 5: DAC-ADC Integration
144-
log::LOGGER.log(log::Logger::LogLevel::INFO, "--- Test 5: DAC-ADC Integration ---");
145-
testDALoopback(dac, adc, 0.5f);
146-
testDALoopback(dac, adc, 1.5f);
147-
testDALoopback(dac, adc, 2.5f);
134+
uart.printf("--- Test 5: DAC-ADC Integration ---\r\n");
135+
testDALoopback(dac, adc, 0.5f, uart);
136+
testDALoopback(dac, adc, 1.5f, uart);
137+
testDALoopback(dac, adc, 2.5f, uart);
148138

149-
log::LOGGER.log(log::Logger::LogLevel::INFO, "Starting Triangle Wave Pattern (Visual Test)");
150-
log::LOGGER.log(log::Logger::LogLevel::INFO, "Connect oscilloscope to PA4 for waveform observation");
139+
uart.printf("Starting Triangle Wave Pattern (Visual Test)\r\n");
140+
uart.printf("Connect oscilloscope to PA4 for waveform observation\r\n");
151141

152142
// Test 6: Triangle wave pattern (visual test)
153143
uint32_t value = 0;
@@ -163,25 +153,24 @@ int main() {
163153

164154
// Log every 100 cycles (every ~1 second)
165155
if (cycle_count % 100 == 0) {
166-
log::LOGGER.log(log::Logger::LogLevel::INFO,
167-
"Triangle Wave: DAC=%d (%.2fV), ADC=%.2fV",
168-
value,
169-
dac.getVoltage(),
170-
adcVoltage);
156+
uart.printf("Triangle Wave: DAC=%d (%dmV), ADC=%dmV\r\n",
157+
value,
158+
(int)(dac.getVoltage() * 1000),
159+
(int)(adcVoltage * 1000));
171160
}
172161

173162
// Update triangle wave
174163
if (increasing) {
175164
value += 50;
176165
if (value >= 4000) {
177166
increasing = false;
178-
log::LOGGER.log(log::Logger::LogLevel::INFO, "Triangle Wave: Peak reached, reversing");
167+
uart.printf("Triangle Wave: Peak reached, reversing\r\n");
179168
}
180169
} else {
181170
value -= 50;
182171
if (value <= 50) {
183172
increasing = true;
184-
log::LOGGER.log(log::Logger::LogLevel::INFO, "Triangle Wave: Valley reached, reversing");
173+
uart.printf("Triangle Wave: Valley reached, reversing\r\n");
185174
}
186175
}
187176

0 commit comments

Comments
 (0)