1
- // JLed Unit tests (run on host).
2
- // Copyright 2017 Jan Delgado [email protected]
1
+ // JLed Unit tests for the ESP32 HAL (run on host).
2
+ // Copyright 2017-2020 Jan Delgado [email protected]
3
3
#include " esp32.h" // NOLINT
4
4
#include < esp32_hal.h> // NOLINT
5
5
#include " catch.hpp"
@@ -46,7 +46,7 @@ TEST_CASE("ledc ctor correctly initializes hardware", "[esp32_hal]") {
46
46
// attach channel 2 to pin 1
47
47
constexpr auto kChan = 5 ;
48
48
constexpr auto kPin = 10 ;
49
- auto aw [[gnu::unused]] = Esp32Hal (kPin , kChan );
49
+ auto hal [[gnu::unused]] = Esp32Hal (kPin , kChan );
50
50
51
51
// writer expected to used 5000Hz and 8 bits
52
52
REQUIRE (arduinoMockGetLedcSetup (kChan ).freq == 5000 );
@@ -60,40 +60,63 @@ TEST_CASE("ledc selects same channel for same pin", "[esp32_hal]") {
60
60
61
61
// note: we test a static property here (auto incremented next channel
62
62
// number). so test has side effects. TODO avoid?
63
- auto h1 = Esp32Hal (kPin );
64
- auto h2 = Esp32Hal (kPin );
63
+ auto hal1 = Esp32Hal (kPin );
64
+ auto hal2 = Esp32Hal (kPin );
65
65
66
66
// same channel is to be selected, since pin did not change
67
- REQUIRE (h1 .chan () == h2 .chan ());
67
+ REQUIRE (hal1 .chan () == hal2 .chan ());
68
68
}
69
69
70
70
TEST_CASE (" ledc selects different channels for different pins" , " [esp32_hal]" ) {
71
71
constexpr auto kPin = 10 ;
72
72
73
- auto h1 = Esp32Hal (kPin );
74
- auto h2 = Esp32Hal (kPin + 1 );
73
+ auto hal1 = Esp32Hal (kPin );
74
+ auto hal2 = Esp32Hal (kPin + 1 );
75
75
76
- REQUIRE (h1 .chan () != h2 .chan ());
76
+ REQUIRE (hal1 .chan () != hal2 .chan ());
77
77
}
78
78
79
- TEST_CASE (" ledc analogWrite() writes value using analogWrite" , " [esp32_hal]" ) {
79
+ TEST_CASE (" analogWrite() writes value using analogWrite" , " [esp32_hal]" ) {
80
80
esp32MockInit ();
81
81
82
82
// attach channel 2 to pin 1
83
83
constexpr auto kChan = 5 ;
84
84
constexpr auto kPin = 10 ;
85
- auto aw = Esp32Hal (kPin , kChan );
85
+ auto hal = Esp32Hal (kPin , kChan );
86
86
87
- aw .analogWrite (123 );
87
+ hal .analogWrite (123 );
88
88
89
89
// note: value is written to channel, not pin.
90
90
REQUIRE (arduinoMockGetLedcState (kChan ) == 123 );
91
91
}
92
92
93
+ TEST_CASE (" analogWrite() writes 0 as 0" , " [esp32_hal]" ) {
94
+ esp32MockInit ();
95
+
96
+ // attach channel 2 to pin 1
97
+ constexpr auto kChan = 5 ;
98
+ constexpr auto kPin = 10 ;
99
+ auto hal = Esp32Hal (kPin , kChan );
100
+
101
+ hal.analogWrite (0 );
102
+ REQUIRE (arduinoMockGetLedcState (kChan ) == 0 );
103
+ }
104
+
105
+ TEST_CASE (" analogWrite() writes 255 as 256" , " [esp32_hal]" ) {
106
+ esp32MockInit ();
107
+
108
+ constexpr auto kChan = 5 ;
109
+ constexpr auto kPin = 10 ;
110
+ auto hal = Esp32Hal (kPin , kChan );
111
+
112
+ hal.analogWrite (255 );
113
+ REQUIRE (arduinoMockGetLedcState (kChan ) == 256 );
114
+ }
115
+
93
116
TEST_CASE (" millis() returns correct time" , " [esp32_hal]" ) {
94
117
arduinoMockInit ();
95
- auto h = Esp32Hal (1 );
96
- REQUIRE (h .millis () == 0 );
118
+ auto hal = Esp32Hal (1 );
119
+ REQUIRE (hal .millis () == 0 );
97
120
arduinoMockSetMillis (99 );
98
- REQUIRE (h .millis () == 99 );
121
+ REQUIRE (hal .millis () == 99 );
99
122
}
0 commit comments