/
main.cpp
205 lines (171 loc) · 6.18 KB
/
main.cpp
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
196
197
198
199
200
201
202
203
#include <esp_now.h>
#include <WiFi.h>
// ==========================================================================
// REPLACE WITH YOUR ESP RECEIVER'S MAC ADDRESS
// you the program find_mac_address in this repo to find your ESP32's MAC address
uint8_t broadcastAddress1[] = {0x??, 0x??, 0x??, 0x??, 0x??, 0x??};
// if you have other receivers you can define in the following
// uint8_t broadcastAddress2[] = {0xFF, , , , , };
// uint8_t broadcastAddress3[] = {0xFF, , , , , };
// ==========================================================================
// your data structure (the data block you send to receiver)
// your reciever should use the same data structure
typedef struct data_structure {
int newPosition;
bool isButtonPressed;
} data_structure;
data_structure sending_data;
//=============================================================================
// some global variables
int newPosition = 15;
int oldPosition = 0;
bool isButtonPressed = false;
//=============================================================================
// Sender Rotary Encoder related
#include "AiEsp32RotaryEncoder.h"
#include "Arduino.h"
/*
connecting Rotary encoder
Rotary encoder side ESP32 side
------------------- ---------------------------------------------------------------------
CLK (A pin) any microcontroler intput pin with interrupt -> 32
DT (B pin) any microcontroler intput pin with interrupt -> 39
SW (button pin) any microcontroler intput pin with interrupt -> 17
GND ESP32 GND
VCC ESP32 3V
*/
#define ROTARY_ENCODER_A_PIN 32
#define ROTARY_ENCODER_B_PIN 39
#define ROTARY_ENCODER_BUTTON_PIN 17
#define ROTARY_ENCODER_VCC_PIN -1
//=============================================================================
#define ROTARY_ENCODER_STEPS 5
//
AiEsp32RotaryEncoder rotaryEncoder = AiEsp32RotaryEncoder(ROTARY_ENCODER_A_PIN, ROTARY_ENCODER_B_PIN, ROTARY_ENCODER_BUTTON_PIN, ROTARY_ENCODER_VCC_PIN, ROTARY_ENCODER_STEPS);
//=============================================================================
// FastLED related
#include <FastLED.h>
// LED VCC --> Arduino 5V you LED need power ;-)
const int DATA_PIN = 19;
const int NUM_LEDS = 16;
CRGB leds[NUM_LEDS];
void IRAM_ATTR readEncoderISR() {
rotaryEncoder.readEncoder_ISR();
}
void rotary_onButtonClick()
{
static unsigned long lastTimePressed = 0;
//ignore multiple press in that time milliseconds
if (millis() - lastTimePressed < 500)
{
return;
}
lastTimePressed = millis();
Serial.print("button pressed at ");
Serial.println(millis());
isButtonPressed = true;
sending_data.newPosition = newPosition;
sending_data.isButtonPressed = isButtonPressed;
esp_err_t result = esp_now_send(0, (uint8_t *) &sending_data, sizeof(data_structure));
if (result == ESP_OK) {
Serial.println("Sent with success");
}
else {
Serial.println("Error sending the data");
}
delay(100);
}
// callback when data is sent
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
char macStr[18];
Serial.print("Packet to: ");
// Copies the sender mac address to a string
snprintf(macStr, sizeof(macStr), "%02x:%02x:%02x:%02x:%02x:%02x",
mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
Serial.print(macStr);
Serial.print(" send status:\t");
Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");
}
void setup() {
Serial.begin(115200);
WiFi.mode(WIFI_STA);
if (esp_now_init() != ESP_OK) {
Serial.println("Error initializing ESP-NOW");
return;
}
esp_now_register_send_cb(OnDataSent);
// register peer
esp_now_peer_info_t peerInfo;
peerInfo.channel = 0;
peerInfo.encrypt = false;
// register first peer
memcpy(peerInfo.peer_addr, broadcastAddress1, 6);
if (esp_now_add_peer(&peerInfo) != ESP_OK) {
Serial.println("Failed to add peer");
return;
}
// ==========================================================================
// initialize rotary encoder
rotaryEncoder.begin();
attachInterrupt(digitalPinToInterrupt(ROTARY_ENCODER_A_PIN), readEncoderISR, CHANGE);
attachInterrupt(digitalPinToInterrupt(ROTARY_ENCODER_B_PIN), readEncoderISR, CHANGE);
attachInterrupt(digitalPinToInterrupt(ROTARY_ENCODER_BUTTON_PIN), rotary_onButtonClick, RISING);
// set boundaries and if values should cycle or not
// in this example we will set possible values between 0 and 1000;
bool circleValues = true;
// minValue, maxValue, circleValues true|false (when max go to min and vice versa)
rotaryEncoder.setBoundaries(0, 15, circleValues);
// set the value - larger number = more accelearation; 0 or 1 means disabled acceleration
rotaryEncoder.setAcceleration(0);
// ==========================================================================
// FastLED related configuration
FastLED.addLeds<WS2812B, DATA_PIN, RGB>(leds, NUM_LEDS);
FastLED.setBrightness(100);
set_max_power_in_volts_and_milliamps(5, 100);
FastLED.show();
}
// ==========================================================================
// should be the same with sender
void led_animation(){
for (int i = 0; i < NUM_LEDS; i++) {
if (i == newPosition) {
leds[i] = CHSV((newPosition) * 15, 16, 16);
} else {
leds[i] = CHSV((newPosition) * 15, 255, 255);
}
}
FastLED.show();
}
void rotary_loop()
{
// dont do anything unless value changed
if (!rotaryEncoder.encoderChanged())
{
return;
} else {
Serial.print("Value: ");
Serial.println(rotaryEncoder.readEncoder());
newPosition = rotaryEncoder.readEncoder();
Serial.println(newPosition);
sending_data.newPosition = newPosition;
sending_data.isButtonPressed = false;
esp_err_t result = esp_now_send(0, (uint8_t *) &sending_data, sizeof(data_structure));
if (result == ESP_OK) {
Serial.println("Sent with success");
}
else {
Serial.println("Error sending the data");
}
led_animation();
}
}
// MAIN LOOP
void loop() {
rotary_loop();
if (isButtonPressed == true) {
Serial.print(newPosition);
isButtonPressed = false;
}
}
//=============================================================================
// END