Smart Poi 3.0.0
WiFi Connected LED POV Poi
Loading...
Searching...
No Matches
udpHandler.ino File Reference

Functions

void handleUDP ()
 Handles incoming UDP packets.
 

Function Documentation

◆ handleUDP()

void handleUDP ( )

Handles incoming UDP packets.

This function processes UDP packets, checks for configuration messages, and updates the LED strip accordingly.

Parameters
None
Returns
None
Precondition
UDP connection is established and packets are being received.
Postcondition
LED strip is updated with new colors and settings.
Note
This function is called repeatedly to handle incoming UDP packets.
Uses global variables such as currentMillis2, previousMillis2, interval, checkit, and packetBuffer.
Calls other functions such as Udp.read(), EEPROM.write(), and FastLED.showColor().
18 {
19 if (currentMillis2 - previousMillis2 > interval * 2) { //message received after long wait, may be config message? check it
20 // save the last time you checked the time
22 //check the UDP packet for correct config message values later:
23 checkit = true;
24 }
26 state = 0; //udp good signal state
27
28 // read the packet into packetBufffer
29 len = Udp.read(packetBuffer, NUM_PX);
30 if (len > 0) packetBuffer[len] = 0;
31 ////////////////////////////////////FastLED Code://///////////
32 for (int i = 0; i < NUM_PX; i++)
33 {
34
35 ///////////////////////////////////////////////////////settings mode////////////////////////////////////////////////////////////////////////
36 if (checkit) { //need some sort of cylon thingy here to show that poi is doing something!!!
37 Y = packetBuffer[i];
38
39 switch (i) {
40 case 0:
41 if (Y == 0) { //Y is 0 at 1st packetBuffer array member
42 ////Serial.println("checked 0, signal received");
43 }
44 else {
45 checkit = false; //not on track, try again next time
46 }
47 break;
48 case 1:
49 if (Y == 1) {
50 ////Serial.println("checked 1, signal received");
51 }
52 else {
53 checkit = false; //not on track, try again next time
54 }
55 break;
56 case 2:
57 if (Y == 2) { //default 2
58 ////Serial.println("checked 2, Brightness change signal received");
59 } else if (Y == 3) {
60 ////Serial.println("checked 2, Channel change signal received");
61 channelChange = true; //todo: not needed - done with http server now
62 } //add another option for pattern change here?
63
64 else {
65 checkit = false; //not on track, try again next time
66 }
67 break;
68 case 3:
69 if (channelChange) { //todo: not needed, done with http server now
70 if ((int)Y > 11 || (int)Y < 1) {
71 //wrong channel information received - error checking
72 } else {
73 EEPROM.write(13, 1); //clearing
74 EEPROM.commit();
75 int newChannel2; //temp variable
76 newChannel2 = int(Y);
77 EEPROM.write(13, newChannel2);
78 EEPROM.commit();
79 // Serial.print("channel changed to :");
80 // Serial.println(newChannel2);
81 // ESP.restart(); //not using this right now but see https://github.com/esp8266/Arduino/issues/1722#issuecomment-192829825
82 FastLED.showColor(CRGB::Magenta); //visual indicator
83 channelChange = false;
84 checkit = false; //Finished settings, exit
85 }
86 } else { //todo: brightness change not needed here, done in http server now.
87 //set eeprom:
88 ////Serial.print("received Y: ");
89 ////Serial.println(Y);
90 EEPROM.write(15, int(Y)); //set for next time
91 EEPROM.commit();
92 newBrightness = int(Y);
93 FastLED.setBrightness( newBrightness );
94 ////Serial.println(" ");
95 ////Serial.print("Brightness now set to: ");
96 ////Serial.println(newBrightness);
97 FastLED.showColor(CRGB::Blue); //visual indicator
98 checkit = false; //Finished settings, exit
99 }
100 break;
101 default:
102
103 break;
104 //etc for 32, can write values according to signal received
105
106 }
107
108 }
109 ///////////////////////////////////////////////////////////////////////////////////////end settings mode///////////////////////////
110
111 ///////////////////////////////convert byte to signed byte:////
112 X = packetBuffer[i] - 127;
113 /////////////////////////////end convert byte to signed byte////
114
115 R1 = (X & 0xE0);
116 leds[i].r = R1; //
117 G1 = ((X << 3) & 0xE0);
118 leds[i].g = G1;
119 M1 = (X << 6);
120 leds[i].b = M1;
121 // FastLED.delay(1);
122 // Serial.print(R1); // Serial.print(", "); // Serial.print(G1); // Serial.print(", "); // Serial.println(M1);
123 }
124
125 //FastLED.delay(2); //not just for emulator!
126 LEDS.show();
127// delayMicroseconds(200);
128// FastLED.delay(1);
129 ///////////////////////////////////end FastLed Code//////////////
130
131 // send a reply, to the IP address and port that sent us the packet we received
132 // Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
133 // Udp.write(ReplyBuffer);
134 // Udp.endPacket();
135}
volatile int len
Definition main.ino:249
boolean channelChange
Definition main.ino:130
volatile unsigned long currentMillis2
Definition main.ino:247
volatile byte X
Definition main.ino:240
unsigned long previousMillis2
Definition main.ino:125
int newBrightness
Definition main.ino:39
volatile byte R1
Definition main.ino:242
byte packetBuffer[120]
Definition main.ino:111
long interval
Definition main.ino:127
CRGB leds[121]
Definition main.ino:60
unsigned long previousMillis
Definition main.ino:124
int state
Definition main.ino:139
volatile unsigned long currentMillis
Definition main.ino:246
boolean checkit
Definition main.ino:129
#define NUM_PX
Definition main.ino:64
volatile byte M1
Definition main.ino:244
volatile byte Y
Definition main.ino:241
WiFiUDP Udp
Definition main.ino:114
volatile byte G1
Definition main.ino:243

References channelChange, checkit, currentMillis, currentMillis2, G1, interval, leds, len, M1, newBrightness, NUM_PX, packetBuffer, previousMillis, previousMillis2, R1, state, Udp, X, and Y.