demo, add text and string

Dependencies:   NeoStrip mbed

Committer:
zchen311
Date:
Fri Apr 25 14:32:24 2014 +0000
Revision:
0:4c7b2475af40
add text support.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
zchen311 0:4c7b2475af40 1 /*
zchen311 0:4c7b2475af40 2 * Adafruit NeoPixel 8x8 matrix example
zchen311 0:4c7b2475af40 3 *
zchen311 0:4c7b2475af40 4 * This program displays a couple simple patterns on an 8x8 NeoPixel matrix.
zchen311 0:4c7b2475af40 5 *
zchen311 0:4c7b2475af40 6 * 3 buttons are used for DigitalIns, 2 control the brightness up and down,
zchen311 0:4c7b2475af40 7 * and the third switches patterns
zchen311 0:4c7b2475af40 8 */
zchen311 0:4c7b2475af40 9
zchen311 0:4c7b2475af40 10 #include "mbed.h"
zchen311 0:4c7b2475af40 11 #include "NeoStrip.h"
zchen311 0:4c7b2475af40 12 #include "text.h"
zchen311 0:4c7b2475af40 13 #define N 128
zchen311 0:4c7b2475af40 14 #define PATTERNS 4
zchen311 0:4c7b2475af40 15 #include <string>
zchen311 0:4c7b2475af40 16 int hueToRGB(float h);
zchen311 0:4c7b2475af40 17 void pattern0();
zchen311 0:4c7b2475af40 18 void pattern1();
zchen311 0:4c7b2475af40 19 void pattern2();
zchen311 0:4c7b2475af40 20 void fillBlue();
zchen311 0:4c7b2475af40 21 void progressBar(bool);
zchen311 0:4c7b2475af40 22 int getIndex(int, int);
zchen311 0:4c7b2475af40 23 void putChar(int row, int col, char ch, int color);
zchen311 0:4c7b2475af40 24 void putString(std::string);
zchen311 0:4c7b2475af40 25 void loopAscii(int count);
zchen311 0:4c7b2475af40 26 void MarioBox();
zchen311 0:4c7b2475af40 27 void drawBox(int row);void emptyBox(int row);
zchen311 0:4c7b2475af40 28 void putCharV(int row, int col, char ch, int color);
zchen311 0:4c7b2475af40 29 // array of function pointers to the various patterns
zchen311 0:4c7b2475af40 30 //void (*patterns[])(void) = {&pattern0, &pattern1, &pattern2};
zchen311 0:4c7b2475af40 31
zchen311 0:4c7b2475af40 32 NeoStrip strip(p18, N);
zchen311 0:4c7b2475af40 33 DigitalIn b1(p20); // brightness up
zchen311 0:4c7b2475af40 34 DigitalIn b2(p19); // brightness down
zchen311 0:4c7b2475af40 35 DigitalIn b3(p21); // next pattern
zchen311 0:4c7b2475af40 36
zchen311 0:4c7b2475af40 37 // timer used for debugging
zchen311 0:4c7b2475af40 38 Timer timer;
zchen311 0:4c7b2475af40 39 int chars[128];
zchen311 0:4c7b2475af40 40 char ch = 'A';
zchen311 0:4c7b2475af40 41
zchen311 0:4c7b2475af40 42 int main()
zchen311 0:4c7b2475af40 43 {
zchen311 0:4c7b2475af40 44
zchen311 0:4c7b2475af40 45 std::string emoji[9] = {"*_*", "^_^", "$_$", ">_<", "=_=", "+_=", ":)", ":(", ":3"};
zchen311 0:4c7b2475af40 46 float bright = 0.2; // 20% is plenty for indoor use
zchen311 0:4c7b2475af40 47 strip.setBrightness(bright); // set default brightness
zchen311 0:4c7b2475af40 48 int count = 0;
zchen311 0:4c7b2475af40 49 while (true)
zchen311 0:4c7b2475af40 50 {
zchen311 0:4c7b2475af40 51 timer.reset(); // use a timer to measure loop execution time for debugging purposes
zchen311 0:4c7b2475af40 52 timer.start(); // for this application, the main loop takes approximately 3ms to run
zchen311 0:4c7b2475af40 53 progressBar(true);
zchen311 0:4c7b2475af40 54 wait(5);
zchen311 0:4c7b2475af40 55 progressBar(false);
zchen311 0:4c7b2475af40 56
zchen311 0:4c7b2475af40 57 // fillBlue();
zchen311 0:4c7b2475af40 58
zchen311 0:4c7b2475af40 59 // write character
zchen311 0:4c7b2475af40 60 // MarioBox();
zchen311 0:4c7b2475af40 61 // displayEmoji();
zchen311 0:4c7b2475af40 62 // loopAscii(count);
zchen311 0:4c7b2475af40 63 // ch+=2;
zchen311 0:4c7b2475af40 64 // putString(emoji[count%9]);
zchen311 0:4c7b2475af40 65 // count++;
zchen311 0:4c7b2475af40 66 wait(2);
zchen311 0:4c7b2475af40 67 timer.stop();
zchen311 0:4c7b2475af40 68 // print loop time if b3 is pressed
zchen311 0:4c7b2475af40 69 wait_ms(10);
zchen311 0:4c7b2475af40 70 }
zchen311 0:4c7b2475af40 71 }
zchen311 0:4c7b2475af40 72
zchen311 0:4c7b2475af40 73 // pattern0 displays a static image
zchen311 0:4c7b2475af40 74 void pattern0()
zchen311 0:4c7b2475af40 75 {
zchen311 0:4c7b2475af40 76 fillBlue();
zchen311 0:4c7b2475af40 77 // write character
zchen311 0:4c7b2475af40 78 int color = 0xffff00;
zchen311 0:4c7b2475af40 79 putChar(0,0,'G',color);
zchen311 0:4c7b2475af40 80 putChar(0,6,'O',color);
zchen311 0:4c7b2475af40 81 strip.setPixels(0, N, chars);
zchen311 0:4c7b2475af40 82 }
zchen311 0:4c7b2475af40 83
zchen311 0:4c7b2475af40 84 // display a shifting rainbow, all colors have maximum
zchen311 0:4c7b2475af40 85 // saturation and value, with evenly spaced hue
zchen311 0:4c7b2475af40 86 void pattern1()
zchen311 0:4c7b2475af40 87 {
zchen311 0:4c7b2475af40 88 static float dh = 360.0 / N;
zchen311 0:4c7b2475af40 89 static float x = 0;
zchen311 0:4c7b2475af40 90
zchen311 0:4c7b2475af40 91 for (int i = 0; i < N; i++)
zchen311 0:4c7b2475af40 92 strip.setPixel(i, hueToRGB((dh * i) - x));
zchen311 0:4c7b2475af40 93
zchen311 0:4c7b2475af40 94 x += 1;
zchen311 0:4c7b2475af40 95 if (x > 360)
zchen311 0:4c7b2475af40 96 x = 0;
zchen311 0:4c7b2475af40 97 }
zchen311 0:4c7b2475af40 98
zchen311 0:4c7b2475af40 99
zchen311 0:4c7b2475af40 100
zchen311 0:4c7b2475af40 101 // Converts HSV to RGB with the given hue, assuming
zchen311 0:4c7b2475af40 102 // maximum saturation and value
zchen311 0:4c7b2475af40 103 int hueToRGB(float h)
zchen311 0:4c7b2475af40 104 {
zchen311 0:4c7b2475af40 105 // lots of floating point magic from the internet and scratching my head
zchen311 0:4c7b2475af40 106 float r, g, b;
zchen311 0:4c7b2475af40 107 if (h > 360)
zchen311 0:4c7b2475af40 108 h -= 360;
zchen311 0:4c7b2475af40 109 if (h < 0)
zchen311 0:4c7b2475af40 110 h += 360;
zchen311 0:4c7b2475af40 111 int i = (int)(h / 60.0);
zchen311 0:4c7b2475af40 112 float f = (h / 60.0) - i;
zchen311 0:4c7b2475af40 113 float q = 1 - f;
zchen311 0:4c7b2475af40 114
zchen311 0:4c7b2475af40 115 switch (i % 6)
zchen311 0:4c7b2475af40 116 {
zchen311 0:4c7b2475af40 117 case 0: r = 1; g = f; b = 0; break;
zchen311 0:4c7b2475af40 118 case 1: r = q; g = 1; b = 0; break;
zchen311 0:4c7b2475af40 119 case 2: r = 0; g = 1; b = f; break;
zchen311 0:4c7b2475af40 120 case 3: r = 0; g = q; b = 1; break;
zchen311 0:4c7b2475af40 121 case 4: r = f; g = 0; b = 1; break;
zchen311 0:4c7b2475af40 122 case 5: r = 1; g = 0; b = q; break;
zchen311 0:4c7b2475af40 123 default: r = 0; g = 0; b = 0; break;
zchen311 0:4c7b2475af40 124 }
zchen311 0:4c7b2475af40 125
zchen311 0:4c7b2475af40 126 // scale to integers and return the packed value
zchen311 0:4c7b2475af40 127 uint8_t R = (uint8_t)(r * 255);
zchen311 0:4c7b2475af40 128 uint8_t G = (uint8_t)(g * 255);
zchen311 0:4c7b2475af40 129 uint8_t B = (uint8_t)(b * 255);
zchen311 0:4c7b2475af40 130
zchen311 0:4c7b2475af40 131 return (R << 16) | (G << 8) | B;
zchen311 0:4c7b2475af40 132 }
zchen311 0:4c7b2475af40 133
zchen311 0:4c7b2475af40 134 void fillBlue(){
zchen311 0:4c7b2475af40 135 for(int i = 0; i < N; i++){
zchen311 0:4c7b2475af40 136 chars[i] = 0x122446;
zchen311 0:4c7b2475af40 137 }
zchen311 0:4c7b2475af40 138 }
zchen311 0:4c7b2475af40 139
zchen311 0:4c7b2475af40 140 int getIndex(int r, int c){
zchen311 0:4c7b2475af40 141 if(c < 8){
zchen311 0:4c7b2475af40 142 return (r * 8 + c);
zchen311 0:4c7b2475af40 143 }else if(c > 15){
zchen311 0:4c7b2475af40 144 return -1;
zchen311 0:4c7b2475af40 145 }
zchen311 0:4c7b2475af40 146 else{
zchen311 0:4c7b2475af40 147 return (64 + r * 8 + (c-8));
zchen311 0:4c7b2475af40 148 }
zchen311 0:4c7b2475af40 149 }
zchen311 0:4c7b2475af40 150
zchen311 0:4c7b2475af40 151
zchen311 0:4c7b2475af40 152 void putChar(int row, int col, char ch, int color){
zchen311 0:4c7b2475af40 153 for(int r = 0; r < 8; r++){
zchen311 0:4c7b2475af40 154 for(int c = 0; c < 6; c++){
zchen311 0:4c7b2475af40 155 if(fontdata_6x8[ch * 48 + r * 6 +c]){
zchen311 0:4c7b2475af40 156 int idx = getIndex(row+r, col+c);
zchen311 0:4c7b2475af40 157 if(idx != -1)
zchen311 0:4c7b2475af40 158 chars[idx] = color;
zchen311 0:4c7b2475af40 159 }
zchen311 0:4c7b2475af40 160 }
zchen311 0:4c7b2475af40 161 }
zchen311 0:4c7b2475af40 162 }
zchen311 0:4c7b2475af40 163 void putCharV(int row, int col, char ch, int color){
zchen311 0:4c7b2475af40 164 for(int r = 0; r < 8; r++){
zchen311 0:4c7b2475af40 165 for(int c = 0; c < 6; c++){
zchen311 0:4c7b2475af40 166 if(fontdata_6x8[ch * 48 + r * 6 +c]){
zchen311 0:4c7b2475af40 167 int idx = (row + r) *8 +( col +c);
zchen311 0:4c7b2475af40 168 if(idx != -1)
zchen311 0:4c7b2475af40 169 chars[idx] = color;
zchen311 0:4c7b2475af40 170 }
zchen311 0:4c7b2475af40 171 }
zchen311 0:4c7b2475af40 172 }
zchen311 0:4c7b2475af40 173 }
zchen311 0:4c7b2475af40 174
zchen311 0:4c7b2475af40 175 void MarioBox(){
zchen311 0:4c7b2475af40 176 int color = 0xffff00;
zchen311 0:4c7b2475af40 177 int red = 0xff0000;
zchen311 0:4c7b2475af40 178
zchen311 0:4c7b2475af40 179 drawBox(8);
zchen311 0:4c7b2475af40 180 strip.setPixels(0, N, chars);
zchen311 0:4c7b2475af40 181 strip.write();
zchen311 0:4c7b2475af40 182 wait(5);
zchen311 0:4c7b2475af40 183
zchen311 0:4c7b2475af40 184 for(int i = 1; i < 3; i++){
zchen311 0:4c7b2475af40 185 fillBlue();
zchen311 0:4c7b2475af40 186 drawBox(8-i);
zchen311 0:4c7b2475af40 187 strip.setPixels(0, N, chars);
zchen311 0:4c7b2475af40 188 strip.write();
zchen311 0:4c7b2475af40 189 wait(0.3);
zchen311 0:4c7b2475af40 190 }
zchen311 0:4c7b2475af40 191 for(int i = 0; i < 3; i++){
zchen311 0:4c7b2475af40 192 fillBlue();
zchen311 0:4c7b2475af40 193 drawBox(6 + i);
zchen311 0:4c7b2475af40 194 strip.setPixels(0, N, chars);
zchen311 0:4c7b2475af40 195 strip.write();
zchen311 0:4c7b2475af40 196 wait(0.3);
zchen311 0:4c7b2475af40 197 }
zchen311 0:4c7b2475af40 198 fillBlue();
zchen311 0:4c7b2475af40 199 emptyBox(8);
zchen311 0:4c7b2475af40 200 putChar(0,0,'0',color);
zchen311 0:4c7b2475af40 201 strip.setPixels(0, N, chars);
zchen311 0:4c7b2475af40 202 strip.write();
zchen311 0:4c7b2475af40 203 wait(30);
zchen311 0:4c7b2475af40 204 }
zchen311 0:4c7b2475af40 205 void putString(std::string str){
zchen311 0:4c7b2475af40 206 int color = 0xffff00;
zchen311 0:4c7b2475af40 207 for(int i = 0; i < str.length(); ++i){
zchen311 0:4c7b2475af40 208 putChar(0,5*i,str[i], color);
zchen311 0:4c7b2475af40 209 }
zchen311 0:4c7b2475af40 210 strip.setPixels(0, N, chars);
zchen311 0:4c7b2475af40 211 strip.write();
zchen311 0:4c7b2475af40 212
zchen311 0:4c7b2475af40 213 }
zchen311 0:4c7b2475af40 214
zchen311 0:4c7b2475af40 215 void loopAscii(int count){
zchen311 0:4c7b2475af40 216 int color = 0xffff00;
zchen311 0:4c7b2475af40 217 putChar(0,0,ch+count,color);
zchen311 0:4c7b2475af40 218 putChar(0,6,ch+count+1,color);
zchen311 0:4c7b2475af40 219 strip.setPixels(0, N, chars);
zchen311 0:4c7b2475af40 220 strip.write();
zchen311 0:4c7b2475af40 221 }
zchen311 0:4c7b2475af40 222
zchen311 0:4c7b2475af40 223 void drawBox(int row){
zchen311 0:4c7b2475af40 224 for(int i = 0; i < 64; ++i)
zchen311 0:4c7b2475af40 225 chars[row*8 + i] = 0x000000;
zchen311 0:4c7b2475af40 226 putCharV(row,0,'?',0xffff00);
zchen311 0:4c7b2475af40 227 }
zchen311 0:4c7b2475af40 228 void emptyBox(int row){
zchen311 0:4c7b2475af40 229 for(int i = 0; i < 64; ++i)
zchen311 0:4c7b2475af40 230 chars[row*8 + i] = 0x000000;
zchen311 0:4c7b2475af40 231 }
zchen311 0:4c7b2475af40 232 void progressBar(bool up){
zchen311 0:4c7b2475af40 233 int index;
zchen311 0:4c7b2475af40 234 int init;
zchen311 0:4c7b2475af40 235 int increment;
zchen311 0:4c7b2475af40 236 int color;
zchen311 0:4c7b2475af40 237 if(up == true){
zchen311 0:4c7b2475af40 238 init = 0;
zchen311 0:4c7b2475af40 239 increment = 1;
zchen311 0:4c7b2475af40 240 color = 0x122446;
zchen311 0:4c7b2475af40 241 }else {
zchen311 0:4c7b2475af40 242 init = 15;
zchen311 0:4c7b2475af40 243 increment = -1;
zchen311 0:4c7b2475af40 244 fillBlue();
zchen311 0:4c7b2475af40 245 color = 0x000000;
zchen311 0:4c7b2475af40 246 }
zchen311 0:4c7b2475af40 247
zchen311 0:4c7b2475af40 248 for(int x = 0; x < 16 ; x++){
zchen311 0:4c7b2475af40 249
zchen311 0:4c7b2475af40 250 for (int j = 0; j < 8; j++){
zchen311 0:4c7b2475af40 251 index = getIndex(j,init + increment * x);
zchen311 0:4c7b2475af40 252 chars[index] = color;
zchen311 0:4c7b2475af40 253 }
zchen311 0:4c7b2475af40 254 strip.setPixels(0, N, chars);
zchen311 0:4c7b2475af40 255 strip.write();
zchen311 0:4c7b2475af40 256 wait(1);
zchen311 0:4c7b2475af40 257 }
zchen311 0:4c7b2475af40 258 }