Displays text on a WS2812 (NeoPixel) matrix display
Dependencies: mbed miniFont wsDrive
Displays text on an array of WS2812 LEDs (NeoPixels)
Text is 5 rows tall so any grid larger than that can be used. The font supports A-Z (capitals only, any lowercase input will be capitalised) numbers and some basic punctuation. Letters are 5 LEDs wide, some numbers and punctuation are smaller but generally you need 6 pixels wide per character you wish to fit. If displaying a string a 1 row space is left between characters, for other spacings either edit the code or print one letter at a time and adjust the offset for the next letter.
LEDs must be connected in horizontal rows, top row first. Rows can be either direction or alternating directions.
NOTE: The testing on this has been fairly minimal. It works with my one physical configuration, I think the logic for other configurations is correct but haven't tested it. If you find a problem please let me know.
Revision 2:0006602e8713, committed 2014-11-07
- Comitter:
- AndyA
- Date:
- Fri Nov 07 10:42:01 2014 +0000
- Parent:
- 1:2014f027ed6f
- Commit message:
- Moved font to being a library
Changed in this revision
diff -r 2014f027ed6f -r 0006602e8713 miniFont.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/miniFont.lib Fri Nov 07 10:42:01 2014 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/AndyA/code/miniFont/#46e0549c2bfb
diff -r 2014f027ed6f -r 0006602e8713 miniFont/miniFont.cpp --- a/miniFont/miniFont.cpp Thu Nov 06 17:32:12 2014 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,167 +0,0 @@ -#include "miniFont.h" - -const char miniFont::dot[5] = { 0x00,0x00,0x00,0x03,0x03}; -const char miniFont::space[5] = { 0x00,0x00,0x00,0x00,0x00}; -const char miniFont::exclam[5] = { 0x01,0x01,0x01,0x00,0x01}; -const char miniFont::quest[5] = { 0x0f,0x09,0x02,0x00,0x02}; - -const char miniFont::numbers[10][5] = { - { 0x0e, 0x11, 0x11, 0x11, 0x08 }, // 0 - { 0x06, 0x02, 0x02, 0x02, 0x07 }, // 1 - { 0x1f, 0x01, 0x06, 0x18, 0x1f }, // 2 - { 0x1f, 0x01, 0x0f, 0x01, 0x1f }, // 3 - { 0x10, 0x10, 0x14, 0x1f, 0x04 }, // 4 - { 0x1f, 0x10, 0x1e, 0x01, 0x1e }, // 5 - { 0x1f, 0x10, 0x1f, 0x11, 0x1f }, // 6 - { 0x1f, 0x01, 0x01, 0x02, 0x02 }, // 7 - { 0x1f, 0x11, 0x08, 0x11, 0x1f }, // 8 - { 0x1f, 0x11, 0x1f, 0x01, 0x1f } // 9 -}; - -const char miniFont::letters[26][5] = { - { 0x1f, 0x11, 0x1f, 0x11, 0x11 }, // A - { 0x1e, 0x11, 0x1e, 0x11, 0x1e }, // B - { 0x1f, 0x10, 0x10, 0x10, 0x1f }, // C - { 0x1e, 0x11, 0x11, 0x11, 0x1e }, // D - { 0x1f, 0x10, 0x1e, 0x10, 0x1f }, // E - { 0x1f, 0x10, 0x1e, 0x10, 0x10 }, // F - { 0x1f, 0x10, 0x13, 0x11, 0x1f }, // G - { 0x11, 0x11, 0x1f, 0x11, 0x11 }, // H - { 0x07, 0x02, 0x02, 0x02, 0x07 }, // I - { 0x01, 0x01, 0x01, 0x11, 0x1f }, // J - { 0x11, 0x12, 0x1c, 0x12, 0x11 }, // K - { 0x10, 0x10, 0x10, 0x10, 0x1f }, // L - { 0x11, 0x1b, 0x15, 0x11, 0x11 }, // M - { 0x11, 0x19, 0x15, 0x13, 0x11 }, // N - { 0x1f, 0x11, 0x11, 0x11, 0x1f }, // O - { 0x1f, 0x11, 0x1f, 0x10, 0x10 }, // P - { 0x1f, 0x11, 0x15, 0x1f, 0x04 }, // Q - { 0x1f, 0x11, 0x1f, 0x12, 0x11 }, // R - { 0x1f, 0x10, 0x1f, 0x01, 0x1F }, // S - { 0x1f, 0x04, 0x04, 0x04, 0x04 }, // T - { 0x11, 0x11, 0x11, 0x11, 0x1F }, // U - { 0x11, 0x11, 0x11, 0x0a, 0x04 }, // V - { 0x11, 0x11, 0x15, 0x1b, 0x11 }, // W - { 0x11, 0x0a, 0x04, 0x0a, 0x11 }, // X - { 0x11, 0x11, 0x1f, 0x04, 0x04 }, // Y - { 0x1f, 0x02, 0x04, 0x08, 0x1f } // Z -}; - - -miniFont::miniFont() { - - rotate90 = false; - fixedWidth = false; - } - - -uint8_t miniFont::getMinPixWidth(char letter) -{ - switch(letter) { - case '.': - return 2; - case ' ': - case '1': - case 'I': - case 'i': - return 3; - case '!': - return 1; - case '?': - return 4; - default: - return maxWidth; - } -} - - -uint8_t miniFont::getPixWidth(char letter) -{ - if (fixedWidth) - return maxWidth; - else - return getMinPixWidth(letter); -} - -uint8_t miniFont::getPixHeight(char letter) -{ - return 5; -} - -// gets the proportional letter -bool miniFont::getRawChar(char letter, const char **rowArray) -{ - - if ((letter>='A') && (letter<='Z')) { // upper - *rowArray = letters[letter-'A']; - return true; - } - if ((letter>='a') && (letter<='z')) { // lower - *rowArray = letters[letter-'a']; - return true; - } - if ((letter>='0') && (letter <='9')) { - *rowArray = numbers[letter-'0']; - return true; - } - if (letter == '.') { - *rowArray = dot; - return true; - } - if (letter == '!') { - *rowArray = exclam; - return true; - } - if (letter == '?') { - *rowArray = quest; - return true; - } - if (letter == ' ') { - *rowArray = space; - return true; - } - return false; -} - -// converts to fixed width if needed. -bool miniFont::getVerticalChar(char letter, const char **rowArray) -{ - if (!fixedWidth) - return getRawChar(letter,rowArray); - - uint8_t shift = (maxWidth - getMinPixWidth(letter)) / 2; - if (shift == 0) - return getRawChar(letter,rowArray); - - const char *rawletter; - if (getRawChar(letter,&rawletter)) { - for (int i = 0; i < maxHeight; i++) { - letterBuffer[i] = rawletter[i]<<shift; - } - *rowArray = (const char *)letterBuffer; - return true; - } - return false; -} - -// rotates if needed. -bool miniFont::getChar(char letter, const char **rowArray) -{ - if (!rotate90) return getVerticalChar(letter,rowArray); - - const char *vertical; - if (!getVerticalChar(letter,&vertical)) - return false; - - for (int i = 0; i < maxWidth; i++) { - rotateBuffer[i] = 0; - } - - for (int i = 0; i < maxHeight; i++) { - for (int j = 0; j < maxWidth; j++) { - rotateBuffer[i] = vertical[j] << j; - } - } - *rowArray = vertical; - return true; -}
diff -r 2014f027ed6f -r 0006602e8713 miniFont/miniFont.h --- a/miniFont/miniFont.h Thu Nov 06 17:32:12 2014 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,49 +0,0 @@ -#ifndef __miniFont_h__ -#define __miniFont_h__ - -#include "mbed.h" - -class miniFont -{ -public: - - miniFont(); - uint8_t getPixWidth(char letter); - uint8_t getPixHeight(char letter); - bool getChar(char letter, const char **rowArray); - - bool isFixedWidth() {return fixedWidth;}; - void setFixedWidth(bool newValue) {fixedWidth = newValue;}; - - bool isRotated() {return rotate90;}; - void setRotated(bool newValue) {rotate90 = newValue;}; - -private: - - - uint8_t getMinPixWidth(char letter); - bool getRawChar(char letter, const char **rowArray); - bool getVerticalChar(char letter, const char **rowArray); - - static const uint8_t maxWidth = 5; - static const uint8_t maxHeight = 5; - - bool rotate90; - bool fixedWidth; - char letterBuffer[5]; - char rotateBuffer[5]; - - - static const char letters[26][5]; - - static const char numbers[10][5]; - - static const char dot[5]; - static const char space[5]; - static const char exclam[5]; - static const char quest[5]; - -}; - - -#endif
diff -r 2014f027ed6f -r 0006602e8713 wsDrive.lib --- a/wsDrive.lib Thu Nov 06 17:32:12 2014 +0000 +++ b/wsDrive.lib Fri Nov 07 10:42:01 2014 +0000 @@ -1,1 +1,1 @@ -http://developer.mbed.org/users/AndyA/code/wsDrive/#741864ea11d4 +http://developer.mbed.org/users/AndyA/code/wsDrive/#1f20efb81649