1
+ /* *
2
+ *
3
+ * This Source Code Form is subject to the terms of the Mozilla Public
4
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
5
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
+ */
7
+ #include < MD_Parola.h>
8
+ #include < MD_MAX72xx.h>
9
+ #include < SPI.h>
10
+ #include < Arduino.h>
11
+ #include < Thing.h>
12
+ #include < WebThingAdapter.h>
13
+
14
+ #define LARGE_JSON_BUFFERS 1
15
+
16
+ #define HARDWARE_TYPE MD_MAX72XX::FC16_HW
17
+ #define MAX_DEVICES 4
18
+ #define CS_PIN 5
19
+
20
+ MD_Parola myDisplay = MD_Parola(HARDWARE_TYPE, CS_PIN, MAX_DEVICES);
21
+
22
+ // TODO: Hardcode your wifi credentials here (and keep it private)
23
+ const char *ssid = " " ;
24
+ const char *password = " " ;
25
+
26
+ #if defined(LED_BUILTIN)
27
+ const int ledPin = LED_BUILTIN;
28
+ #else
29
+ const int ledPin = 13 ; // manually configure LED pin
30
+ #endif
31
+
32
+ ThingActionObject *action_generator (DynamicJsonDocument *);
33
+ WebThingAdapter *adapter;
34
+
35
+ const char *displayTypes[] = {" Matrix Display" , nullptr };
36
+ ThingDevice displayThing (" matrixDisplay" , " Matrix Display" , displayTypes);
37
+
38
+ StaticJsonDocument<256 > displayInput;
39
+ JsonObject displayInputObj = displayInput.to<JsonObject>();
40
+ ThingAction displayAction (" display" , " Display text" , " display a text on a Matrix display" ,
41
+ " displayAction" , &displayInputObj, action_generator);
42
+
43
+ void setup () {
44
+ Serial.begin (115200 );
45
+ Serial.println (" Initialize..." );
46
+ myDisplay.begin ();
47
+ myDisplay.setIntensity (0 );
48
+ Serial.println (" Clear display..." );
49
+ myDisplay.displayClear ();
50
+
51
+ pinMode (ledPin, OUTPUT);
52
+ digitalWrite (ledPin, HIGH);
53
+ Serial.println (" " );
54
+ Serial.print (" Connecting to \" " );
55
+ Serial.print (ssid);
56
+ Serial.println (" \" " );
57
+ #if defined(ESP8266) || defined(ESP32)
58
+ WiFi.mode (WIFI_STA);
59
+ #endif
60
+ WiFi.begin (ssid, password);
61
+
62
+ bool blink = true ;
63
+ while (WiFi.status () != WL_CONNECTED) {
64
+ delay (500 );
65
+ Serial.print (" ." );
66
+ digitalWrite (ledPin, blink ? LOW : HIGH);
67
+ blink = !blink;
68
+ }
69
+ digitalWrite (ledPin, HIGH);
70
+
71
+ Serial.println (" " );
72
+ Serial.print (" Connected to " );
73
+ Serial.println (ssid);
74
+ Serial.print (" IP address: " );
75
+ Serial.println (WiFi.localIP ());
76
+
77
+ adapter = new WebThingAdapter (" matrix-display" , WiFi.localIP ());
78
+ displayThing.description = " A web connected matrix display" ;
79
+
80
+
81
+ displayInputObj[" type" ] = " object" ;
82
+ JsonObject displayInputProperties =
83
+ displayInputObj.createNestedObject (" properties" );
84
+
85
+
86
+ JsonObject bodyInput =
87
+ displayInputProperties.createNestedObject (" body" );
88
+ bodyInput[" type" ] = " string" ;
89
+
90
+ displayThing.addAction (&displayAction);
91
+
92
+ adapter->addDevice (&displayThing);
93
+ adapter->begin ();
94
+ }
95
+
96
+ #define BODY_MAX_LENGTH 200
97
+ #define MAX_WO_SCROLLING 6
98
+
99
+ char body[BODY_MAX_LENGTH];
100
+ bool scrolling = false ;
101
+
102
+ void clear_buffer (){
103
+ memset (body, 0 , BODY_MAX_LENGTH);
104
+ scrolling = false ;
105
+ }
106
+
107
+ int rounds = 0 ;
108
+
109
+ void loop ()
110
+ {
111
+ if (strlen (body) > 0 ){
112
+ myDisplay.displayClear ();
113
+ myDisplay.setTextAlignment (PA_LEFT);
114
+
115
+ myDisplay.print (body);
116
+ rounds = 1 ;
117
+ clear_buffer ();
118
+ }else if (rounds > 0 ){
119
+ // To avoid burning, clear after 30 seconds.
120
+ if (rounds == 300 ){
121
+ myDisplay.displayClear ();
122
+ rounds = 0 ;
123
+ }else {
124
+ delay (100 );
125
+ rounds++;
126
+ }
127
+ }
128
+ }
129
+
130
+ void do_display (const JsonVariant &input) {
131
+ Serial.println (" do display..." );
132
+ clear_buffer ();
133
+
134
+ JsonObject inputObj = input.as <JsonObject>();
135
+ myDisplay.displayClear ();
136
+ String body_tmp = inputObj[" body" ];
137
+
138
+ size_t length = strlen (body_tmp.c_str ());
139
+ if (length > BODY_MAX_LENGTH){
140
+ Serial.println (" Body is too long" );
141
+ return ;
142
+ } else {
143
+ memcpy (body, body_tmp.c_str (), length);
144
+ }
145
+ }
146
+
147
+ ThingActionObject *action_generator (DynamicJsonDocument *input) {
148
+ return new ThingActionObject (" display" , input, do_display, nullptr );
149
+ }
0 commit comments