|
| 1 | +// Message Panel |
| 2 | +// Reads an Adafruit IO Feed, then formats and displays the message |
| 3 | +// Author: Melissa LeBlanc-Williams |
| 4 | + |
| 5 | +#include <RGBmatrixPanel.h> |
| 6 | +#include <SPI.h> |
| 7 | + |
| 8 | +#define BASE_CHAR_WIDTH 6 // 5 pixels + 1 space |
| 9 | +#define BASE_CHAR_HEIGHT 8 // 7 pixels + 1 space |
| 10 | + |
| 11 | +// Most of the signal pins are configurable, but the CLK pin has some |
| 12 | +// special constraints. On 8-bit AVR boards it must be on PORTB... |
| 13 | +// Pin 8 works on the Arduino Uno & compatibles (e.g. Adafruit Metro), |
| 14 | +// Pin 11 works on the Arduino Mega. On 32-bit SAMD boards it must be |
| 15 | +// on the same PORT as the RGB data pins (D2-D7)... |
| 16 | +// Pin 8 works on the Adafruit Metro M0 or Arduino Zero, |
| 17 | +// Pin A4 works on the Adafruit Metro M4 (if using the Adafruit RGB |
| 18 | +// Matrix Shield, cut trace between CLK pads and run a wire to A4). |
| 19 | + |
| 20 | +//#define CLK 8 // USE THIS ON ADAFRUIT METRO M0 ir adapting to use Airlift, etc. |
| 21 | +#define CLK A4 // USE THIS ON METRO M4 (not M0) |
| 22 | +#define OE 9 |
| 23 | +#define LAT 10 |
| 24 | +#define A A0 |
| 25 | +#define B A1 |
| 26 | +#define C A2 |
| 27 | +#define D A3 |
| 28 | + |
| 29 | +RGBmatrixPanel matrix(A, B, C, D, CLK, LAT, OE, false, 64); |
| 30 | + |
| 31 | +#include "config.h" |
| 32 | + |
| 33 | +// set up the 'counter' feed |
| 34 | +AdafruitIO_Feed *counter = io.feed("messagepanel"); |
| 35 | + |
| 36 | +void drawText(const char *text, bool resetPosition = true, uint16_t color = 0xffff, uint16_t textSize = 1) { |
| 37 | + matrix.setTextSize(textSize); // size 1 == 8 pixels high |
| 38 | + if (resetPosition) { |
| 39 | + matrix.setCursor(0, 0); // start at top left, with 8 pixel of spacing |
| 40 | + } |
| 41 | + matrix.setTextColor(color); |
| 42 | + matrix.print(text); |
| 43 | +} |
| 44 | + |
| 45 | +// This function is called whenever a 'messagepanel' message |
| 46 | +// is received from Adafruit IO. it was attached to |
| 47 | +// the counter feed in the setup() function above. |
| 48 | +void handleMessage(AdafruitIO_Data *data) { |
| 49 | + String message = data->toString(); |
| 50 | + String plainText = data->toString(); |
| 51 | + uint16_t color = matrix.Color333(7, 7, 7); |
| 52 | + uint16_t textSize = 1; |
| 53 | + uint16_t colorStartIndex = 0, colorEndIndex = 0; |
| 54 | + uint16_t sizeStartIndex = 0, sizeEndIndex = 0; |
| 55 | + uint16_t strpos = 0; |
| 56 | + byte lineLengths[] = {0, 0, 0, 0}; |
| 57 | + byte lineNum = 0; |
| 58 | + // Calculate line lengths |
| 59 | + boolean paramRead = false; |
| 60 | + boolean newLine = false; |
| 61 | + |
| 62 | + matrix.setCursor(0, 0); |
| 63 | + matrix.fillScreen(matrix.Color333(0, 0, 0)); |
| 64 | + |
| 65 | + // Strip out all color data first |
| 66 | + while(strpos < plainText.length()) { |
| 67 | + colorStartIndex = plainText.indexOf('{'); |
| 68 | + colorEndIndex = plainText.indexOf('}'); |
| 69 | + plainText.remove(colorStartIndex, colorEndIndex - colorStartIndex + 1); |
| 70 | + strpos++; |
| 71 | + } |
| 72 | + |
| 73 | + // Calculate the line lengths in pixels for fixed width text |
| 74 | + strpos = 0; |
| 75 | + while(strpos < plainText.length()) { |
| 76 | + sizeStartIndex = plainText.indexOf('<'); |
| 77 | + sizeEndIndex = plainText.indexOf('>'); |
| 78 | + |
| 79 | + if (strpos == sizeStartIndex) { |
| 80 | + textSize = atoi(plainText.substring(sizeStartIndex + 1, sizeEndIndex).c_str()); |
| 81 | + plainText.remove(sizeStartIndex, sizeEndIndex - sizeStartIndex + 1); |
| 82 | + } |
| 83 | + |
| 84 | + if (plainText.charAt(strpos) != '\n') { |
| 85 | + lineLengths[lineNum] += textSize * BASE_CHAR_WIDTH; |
| 86 | + } |
| 87 | + |
| 88 | + // We want to keep adding up the characters * textSize until we hit a newline character |
| 89 | + // or we reach the width of the message panel. Then we go down to the next line |
| 90 | + if (plainText.charAt(strpos) == '\n' || lineLengths[lineNum] >= matrix.width()) { |
| 91 | + lineNum++; |
| 92 | + } |
| 93 | + |
| 94 | + strpos++; |
| 95 | + } |
| 96 | + |
| 97 | + textSize = 1; |
| 98 | + lineNum = 0; |
| 99 | + for(uint16_t i=0; i<message.length(); i++) { |
| 100 | + if (message.charAt(i) == '{') { |
| 101 | + paramRead = true; |
| 102 | + colorStartIndex = i + 1; |
| 103 | + } else if (message.charAt(i) == '}') { |
| 104 | + paramRead = false; |
| 105 | + int wheelPos = atoi(message.substring(colorStartIndex, i).c_str()); |
| 106 | + if (wheelPos < 25) { |
| 107 | + color = Wheel(wheelPos); |
| 108 | + } else { |
| 109 | + color = matrix.Color333(7, 7, 7); |
| 110 | + } |
| 111 | + } else if (message.charAt(i) == '<') { |
| 112 | + paramRead = true; |
| 113 | + sizeStartIndex = i + 1; |
| 114 | + } else if (message.charAt(i) == '>') { |
| 115 | + paramRead = false; |
| 116 | + textSize = atoi(message.substring(sizeStartIndex, i).c_str()); |
| 117 | + } else { |
| 118 | + if (paramRead) continue; |
| 119 | + |
| 120 | + if (matrix.getCursorX() == 0 && matrix.getCursorY() == 0) { |
| 121 | + matrix.setCursor(floor((matrix.width() / 2) - (lineLengths[lineNum] / 2)), 0); |
| 122 | + } else if (newLine) { |
| 123 | + matrix.setCursor(floor((matrix.width() / 2) - (lineLengths[++lineNum] / 2)), matrix.getCursorY()); |
| 124 | + newLine = false; |
| 125 | + } |
| 126 | + drawText(message.substring(i, i+1).c_str(), false, color, textSize); |
| 127 | + if (message.charAt(i) == '\n' || matrix.getCursorX() >= matrix.width()) { |
| 128 | + newLine = true; |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +void setup() { |
| 135 | + matrix.begin(); |
| 136 | + |
| 137 | + // fill the screen with 'black' |
| 138 | + matrix.fillScreen(matrix.Color333(0, 0, 0)); |
| 139 | + // draw some text! |
| 140 | + matrix.setTextWrap(true); |
| 141 | + drawText("Connecting..."); |
| 142 | + Serial.begin(115200); |
| 143 | + io.connect(); |
| 144 | + |
| 145 | + counter->onMessage(handleMessage); |
| 146 | + |
| 147 | + while(io.mqttStatus() < AIO_CONNECTED) { |
| 148 | + drawText("."); |
| 149 | + delay(500); |
| 150 | + } |
| 151 | + |
| 152 | + counter->get(); |
| 153 | + |
| 154 | + Serial.println(); |
| 155 | + Serial.println(io.statusText()); |
| 156 | +} |
| 157 | + |
| 158 | +void loop() { |
| 159 | + io.run(); |
| 160 | +} |
| 161 | + |
| 162 | +// Input a value 0 to 24 to get a color value. |
| 163 | +// The colours are a transition r - g - b - back to r. |
| 164 | +uint16_t Wheel(byte WheelPos) { |
| 165 | + if(WheelPos < 8) { |
| 166 | + return matrix.Color333(7 - WheelPos, WheelPos, 0); |
| 167 | + } else if(WheelPos < 16) { |
| 168 | + WheelPos -= 8; |
| 169 | + return matrix.Color333(0, 7-WheelPos, WheelPos); |
| 170 | + } else { |
| 171 | + WheelPos -= 16; |
| 172 | + return matrix.Color333(0, WheelPos, 7 - WheelPos); |
| 173 | + } |
| 174 | +} |
0 commit comments