Skip to content

Commit a58d399

Browse files
authored
Merge pull request #16 from Citrullin/matrix_display
Add matrixDisplay example
2 parents 3b12484 + e605704 commit a58d399

File tree

3 files changed

+236
-0
lines changed

3 files changed

+236
-0
lines changed

examples/matrixDisplay/Makefile

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#!/usr/bin/make -f
2+
# SPDX-License-Identifier: MPL-2.0
3+
#{
4+
# This Source Code Form is subject to the terms of the Mozilla Public
5+
# License, v. 2.0. If a copy of the MPL was not distributed with this
6+
# file, You can obtain one at http://mozilla.org/MPL/2.0/
7+
#}
8+
9+
default: all
10+
@echo "# $@: $^"
11+
12+
top_reldir?=../..
13+
topdir?=${CURDIR}/${top_reldir}
14+
15+
-include ${topdir}/Makefile
16+
17+
AVR_TOOLS_DIR?=${ARDUINO_DIR}/hardware/tools/avr
18+
ARDUINO_DIR?=/usr/share/arduino
19+
ARDMK_DIR?=${ARDUINO_DIR}
20+
arduino_mk?=${ARDMK_DIR}/Arduino.mk
21+
22+
#{ Config part
23+
VARIANT?=mega
24+
BOARD_TAG=mega2560
25+
BOARD_SUB=
26+
MCU?=at${BOARD_TAG}
27+
F_CPU?=16000000L
28+
MONITOR_PORT?=$(shell ls /dev/ttyUSB* 2> /dev/null \
29+
|| ls /dev/ttyACM* 2> /dev/null \
30+
|| echo "/dev/TODO/setup/monitor" \
31+
| sort | head -n1)
32+
#}
33+
34+
ARCHITECTURE=avr
35+
AVRDUDE_ARD_BAUDRATE?=115200
36+
MONITOR_BAUDRATE?=${AVRDUDE_ARD_BAUDRATE}
37+
AVRDUDE_ARD_PROGRAMMER?=wiring
38+
39+
CPPFLAGS+=-I${top_reldir}
40+
41+
ARDUINO_LIBS += Ethernet SPI
42+
ARDUINO_LIBS += ArduinoMDNS
43+
ARDUINO_LIBS += ArduinoJson
44+
ARDUINO_LIBS += WiFi101
45+
ARDUINO_LIBS += Wire
46+
ARDUINO_LIBS += Adafruit_GFX
47+
ARDUINO_LIBS += Adafruit_SSD1306
48+
49+
-include ${arduino_mk}
50+
51+
rule/setup:
52+
@echo "Please setup your env to use ${arduino_mk}"
53+
54+
%: rule/setup
55+
@echo "Please run \"make $@\" again"

examples/matrixDisplay/README.md

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
## Matrix Display example
2+
3+
You need to install the WebThings Arduino library in order to use this example.
4+
5+
Change the ssid and password to connect to your router
6+
7+
```c++
8+
const char *ssid = "";
9+
const char *password = "";
10+
```
11+
12+
If you want to use more than four matrix displays, you can just change the value of `MAX_DEVICES`
13+
14+
```c++
15+
#define MAX_DEVICES 4
16+
```
17+
18+
If you use another PIN than PIN 5 as CS, you have to change it in the code before
19+
20+
```c++
21+
#define CS_PIN 5
22+
```
23+
24+
The SPI matrix display has to be connected to VSPI of the ESP32.
25+
26+
| ESP32 Pin | | MAX7219 Matrix |
27+
|-------------|---|----------------|
28+
| VCC (3.3V) | → | VCC (VDD) |
29+
| GND | → | GND |
30+
| D23 (IO23) | → | DIN (MOSI) |
31+
| D5 (IO5) | → | CS (SS) |
32+
| D18 (IO18) | → | CLK |
+149
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
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

Comments
 (0)