-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathBrucieBonus.ino
210 lines (171 loc) · 4.93 KB
/
BrucieBonus.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
//
// Will need an Arduino IDE, esp8266, an 8x8 ws2312b matrix and a bit of solder to hand
//
#include <gamma.h>
#include <Adafruit_NeoMatrix.h>
#include <Adafruit_NeoPixel.h>
#include <WiFiManager.h>
#include <PubSubClient.h>
const char* mqtt_server = "a.b.c.d";
const char* mqtt_username = "username";
const char* mqtt_password = "password";
const char*cudacam_topic = "CudaCam";
#define DISPLAY_PIN D7
struct RGB {
byte r;
byte g;
byte b;
};
// Define some colors we'll use frequently
RGB white = { 255, 255, 255 };
RGB red = { 255, 0, 0 };
RGB green = { 0, 255, 0 };
RGB blue = { 0, 0, 255 };
RGB off = { 0, 0, 0 };
Adafruit_NeoMatrix matrix = Adafruit_NeoMatrix(8, 8, 1, 1, DISPLAY_PIN,
NEO_MATRIX_TOP + NEO_MATRIX_LEFT + NEO_MATRIX_COLUMNS + NEO_MATRIX_ZIGZAG,
NEO_GRB + NEO_KHZ800);
WiFiClient espClient;
PubSubClient client(espClient);
#define MAX_MESSAGES 5
#define BUFFER_SIZE 256
typedef struct message_in_q
{
byte message[BUFFER_SIZE];
};
typedef struct message_q
{
int messages_in_queue;
int index_in;
int index_out;
struct message_in_q messages[MAX_MESSAGES];
};
struct message_q* queue;
struct message_q* list_initialise(void);
struct message_in_q* manage_list(struct message_q* list);
void add_to_list(struct message_q* list, byte* data, uint64_t flags, unsigned int length);
void callback(char* topic, byte* payload, unsigned int length)
{
payload[length] = 0;
add_to_list(queue, payload, length);
}
struct message_q* list_initialise(void)
{
struct message_q* retval = NULL;
retval = (message_q*)malloc(sizeof(struct message_q));
retval->messages_in_queue = 0;
retval->index_in = 0;
retval->index_out = 0;
for (int index = 0; index < MAX_MESSAGES; index++)
{
struct message_in_q* item = &retval->messages[index];
item->message[0] = 0;
}
return retval;
}
void add_to_list(struct message_q* list, byte* data, unsigned int length)
{
list->messages_in_queue++;
if (list->messages_in_queue >= MAX_MESSAGES)
{
list->messages_in_queue = MAX_MESSAGES;
}
if (length > BUFFER_SIZE)
{
length = BUFFER_SIZE - 1;
}
memcpy(&list->messages[list->index_in].message[0], data, length);
list->messages[list->index_in].message[length] = 0;
list->index_in++;
if (list->index_in >= MAX_MESSAGES)
{
list->index_in = 0;
}
}
struct message_in_q* manage_list(struct message_q* list)
{
struct message_in_q* retval = NULL;
if (list->messages_in_queue > 0)
{
retval = &list->messages[list->index_out];
list->messages_in_queue--;
list->index_out++;
if (list->index_out >= MAX_MESSAGES)
{
list->index_out = 0;
}
}
return retval;
}
void reconnect()
{
while (!client.connected())
{
Serial.printf("Attempting MQTT connection to %s as %s\n", mqtt_server, mqtt_username);
String clientId = "ESP8266Client-";
clientId += String(random(0xffff), HEX);
if (client.connect(clientId.c_str(), mqtt_username, mqtt_password))
{
Serial.printf("MQTT connected\n");
client.subscribe(cudacam_topic);
}
else
{
Serial.printf("MQTT connection failed, sleeping for 5 seconds\n");
delay(5000);
}
}
}
void scrollText(String textToDisplay, uint16_t colour)
{
int x = matrix.width();
textToDisplay = "-> " + textToDisplay + " ";
int pixelsInText = textToDisplay.length() * 7;
matrix.clear();
matrix.setTextColor(colour);
matrix.setCursor(x, 0);
matrix.print(textToDisplay);
matrix.show();
while (x > (matrix.width() - pixelsInText))
{
matrix.fillScreen(matrix.Color(off.r, off.g, off.b));
matrix.setCursor(--x, 0);
matrix.print(textToDisplay);
matrix.show();
delay(100);
client.loop();
}
matrix.clear();
}
void setup()
{
Serial.begin(115200);
delay(10);
Serial.println("");
WiFiManager wifiManager;
wifiManager.autoConnect();
Serial.printf("Connected with IP %s\n", WiFi.localIP().toString().c_str());
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
matrix.begin();
matrix.setRotation(2);
matrix.setTextWrap(false);
matrix.setBrightness(40);
matrix.fillScreen(0);
queue = list_initialise();
}
void loop()
{
struct message_in_q* message = NULL;
if (!client.connected())
{
reconnect();
}
client.loop();
message = manage_list(queue);
if (message != NULL)
{
Serial.printf("Have a MQTT message to display -> %s\n", &message->message[0]);
scrollText((char*)&message->message[0], matrix.Color(0, 0, 255));
}
}