forked from thelastoutpostworkshop/gpio_viewer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpio_viewer.ino
195 lines (167 loc) · 4.49 KB
/
gpio_viewer.ino
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include "src/gpio_viewer.h"
#include <WiFi.h>
#include <SimpleRotary.h> // Install this library with the Arduino IDE Library Manager
#include "secrets.h"
GPIOViewer gpio_viewer;
struct PWM_PINS
{
int pin;
int channel;
uint16_t level;
};
#define TEST_ESP32_S3
#ifndef TEST_ESP32_S3
#define ROTARY_PIN_A 23
#define ROTARY_PIN_B 22
#define ROTARY_PUSH_BUTTON 22 // Not used
SimpleRotary rotary(ROTARY_PIN_A, ROTARY_PIN_B, ROTARY_PUSH_BUTTON);
int test_digital_pins[] = {33, 25, 26};
const int testDigitalPinsCount = sizeof(test_digital_pins) / sizeof(test_digital_pins[0]);
int currentLed = 0; // Start with the first LED
const int analogPinsCount = 3;
int test_analog_pins[analogPinsCount] = {32, 19, 18};
byte analogValue = 0;
const int freq = 200;
const int resolution = 16;
PWM_PINS test_pwm_pins[] = {{15, 0}, {2, 1}, {0, 2}, {4, 3}};
const int testPWMPinsCount = sizeof(test_pwm_pins) / sizeof(test_pwm_pins[0]);
#else
// Test ESP32-S3
#define ROTARY_PIN_A 41
#define ROTARY_PIN_B 42
#define ROTARY_PUSH_BUTTON 42 // Not used
SimpleRotary rotary(ROTARY_PIN_A, ROTARY_PIN_B, ROTARY_PUSH_BUTTON);
int test_digital_pins[] = {15, 7, 6};
const int testDigitalPinsCount = sizeof(test_digital_pins) / sizeof(test_digital_pins[0]);
int currentLed = 0; // Start with the first LED
const int analogPinsCount = 3;
int test_analog_pins[analogPinsCount] = {4, 5, 6};
int analogValue = 0;
const int freq = 1000;
const int resolution = 10;
PWM_PINS test_pwm_pins[] = {{17, 0, 0}, {18, 1, 0}, {8, 2, 0}, {3, 3, 0}};
const int testPWMPinsCount = sizeof(test_pwm_pins) / sizeof(test_pwm_pins[0]);
#endif
void setup()
{
Serial.begin(115200);
gpio_viewer.connectToWifi(ssid, password);
test1_setup();
#ifdef TEST_ESP32_S3
if (psramFound())
{
uint8_t *largeMemoryBlock = (uint8_t *)malloc(4 * 1024 * 1024); // 4MB
if (largeMemoryBlock == nullptr)
{
Serial.println("Memory allocation failed!");
}
else
{
Serial.println("Memory allocation successful.");
}
}
#endif
gpio_viewer.setSamplingInterval(75);
gpio_viewer.begin();
}
void loop()
{
test1_loop();
}
uint32_t getMaxDutyCycle(int resolution)
{
return (1 << resolution) - 1;
}
void test1_setup()
{
uint16_t amount = 0;
for (int i = 0; i < testPWMPinsCount; i++)
{
amount += (getMaxDutyCycle(resolution) / testPWMPinsCount);
ledcSetup(test_pwm_pins[i].channel, freq, resolution);
ledcAttachPin(test_pwm_pins[i].pin, test_pwm_pins[i].channel);
test_pwm_pins[i].level = amount;
}
for (int i = 0; i < analogPinsCount; i++)
{
pinMode(test_analog_pins[i], OUTPUT);
}
for (int i = 0; i < testDigitalPinsCount; i++)
{
pinMode(test_digital_pins[i], OUTPUT);
digitalWrite(test_digital_pins[i], LOW);
}
xTaskCreate(readRotaryEncoderTask, // Task function
"ReadRotaryEncoder", // Name of the task (for debugging)
2048, // Stack size (bytes)
NULL, // Parameter to pass to the function
1, // Task priority
NULL);
}
void test1_loop()
{
for (int i = 0; i < analogPinsCount; i++)
{
analogValue += (i * 3);
if (analogValue > getMaxDutyCycle(8))
{
analogValue = 0;
}
analogWrite(test_analog_pins[i], analogValue++);
}
for (int i = 0; i < testPWMPinsCount; i++)
{
ledcWrite(test_pwm_pins[i].channel, test_pwm_pins[i].level);
delay(150);
test_pwm_pins[i].level += (getMaxDutyCycle(resolution) / 4);
if (test_pwm_pins[i].level > getMaxDutyCycle(resolution))
{
test_pwm_pins[i].level = 0;
}
}
delay(300);
for (int i = 0; i < testDigitalPinsCount; i++)
{
if (digitalRead(test_digital_pins[i]) == LOW)
{
digitalWrite(test_digital_pins[i], HIGH);
}
else
{
digitalWrite(test_digital_pins[i], LOW);
}
}
delay(300);
}
void updateLeds()
{
for (int i = 0; i < testDigitalPinsCount; i++)
{
digitalWrite(test_digital_pins[i], i == currentLed ? HIGH : LOW);
}
}
void readRotaryEncoderTask(void *pvParameters)
{
for (;;)
{ // Infinite loop
readRotaryEncoder();
vTaskDelay(pdMS_TO_TICKS(10)); // Delay for debouncing, adjust as needed
}
}
void readRotaryEncoder(void)
{
byte i;
i = rotary.rotate();
if (i == 1)
{
currentLed = (currentLed - 1 + testDigitalPinsCount) % testDigitalPinsCount;
updateLeds();
Serial.println("CounterClockwise");
}
if (i == 2)
{
currentLed = (currentLed + 1) % testDigitalPinsCount;
updateLeds();
Serial.println("Clockwise");
}
}