|
| 1 | +/* |
| 2 | + * MaxMatrix |
| 3 | + * Version 1.0 Feb 2013 |
| 4 | + * Copyright 2013 Oscar Kin-Chung Au |
| 5 | + * For more information visit: https://code.google.com/archive/p/arudino-maxmatrix-library/ |
| 6 | + * |
| 7 | + * This code is released under the GNU GPL v3 license policy |
| 8 | + */ |
| 9 | + |
| 10 | + |
| 11 | +#include "Arduino.h" |
| 12 | +#include "MaxMatrix.h" |
| 13 | + |
| 14 | +MaxMatrix::MaxMatrix(byte _data, byte _load, byte _clock, byte _num) |
| 15 | +{ |
| 16 | + data = _data; |
| 17 | + load = _load; |
| 18 | + clock = _clock; |
| 19 | + num = _num; |
| 20 | + for (int i=0; i<80; i++) |
| 21 | + buffer[i] = 0; |
| 22 | +} |
| 23 | + |
| 24 | +void MaxMatrix::init(byte maxInUse) |
| 25 | +{ |
| 26 | + pinMode(data, OUTPUT); |
| 27 | + pinMode(clock, OUTPUT); |
| 28 | + pinMode(load, OUTPUT); |
| 29 | + digitalWrite(clock, HIGH); |
| 30 | + |
| 31 | + num = maxInUse; |
| 32 | + |
| 33 | + setCommand(max7219_reg_scanLimit, 0x07); |
| 34 | + setCommand(max7219_reg_decodeMode, 0x00); // using an led matrix (not digits) |
| 35 | + setCommand(max7219_reg_shutdown, 0x01); // not in shutdown mode |
| 36 | + setCommand(max7219_reg_displayTest, 0x00); // no display test |
| 37 | + |
| 38 | + // empty registers, turn all LEDs off |
| 39 | + clear(); |
| 40 | + |
| 41 | + setIntensity(0x0f); // the first 0x0f is the value you can set |
| 42 | +} |
| 43 | + |
| 44 | +void MaxMatrix::setIntensity(byte intensity) |
| 45 | +{ |
| 46 | + setCommand(max7219_reg_intensity, intensity); |
| 47 | +} |
| 48 | + |
| 49 | +void MaxMatrix::clear() |
| 50 | +{ |
| 51 | + for (int i=0; i<8; i++) |
| 52 | + setColumnAll(i,0); |
| 53 | + |
| 54 | + for (int i=0; i<80; i++) |
| 55 | + buffer[i] = 0; |
| 56 | +} |
| 57 | + |
| 58 | +void MaxMatrix::setCommand(byte command, byte value) |
| 59 | +{ |
| 60 | + digitalWrite(load, LOW); |
| 61 | + for (int i=0; i<num; i++) |
| 62 | + { |
| 63 | + shiftOut(data, clock, MSBFIRST, command); |
| 64 | + shiftOut(data, clock, MSBFIRST, value); |
| 65 | + } |
| 66 | + digitalWrite(load, LOW); |
| 67 | + digitalWrite(load, HIGH); |
| 68 | +} |
| 69 | + |
| 70 | + |
| 71 | +void MaxMatrix::setColumn(byte col, byte value) |
| 72 | +{ |
| 73 | + int n = col / 8; |
| 74 | + int c = col % 8; |
| 75 | + digitalWrite(load, LOW); |
| 76 | + for (int i=0; i<num; i++) |
| 77 | + { |
| 78 | + if (i == n) |
| 79 | + { |
| 80 | + shiftOut(data, clock, MSBFIRST, c + 1); |
| 81 | + shiftOut(data, clock, MSBFIRST, value); |
| 82 | + } |
| 83 | + else |
| 84 | + { |
| 85 | + shiftOut(data, clock, MSBFIRST, 0); |
| 86 | + shiftOut(data, clock, MSBFIRST, 0); |
| 87 | + } |
| 88 | + } |
| 89 | + digitalWrite(load, LOW); |
| 90 | + digitalWrite(load, HIGH); |
| 91 | + |
| 92 | + buffer[col] = value; |
| 93 | +} |
| 94 | + |
| 95 | +void MaxMatrix::setColumnAll(byte col, byte value) |
| 96 | +{ |
| 97 | + digitalWrite(load, LOW); |
| 98 | + for (int i=0; i<num; i++) |
| 99 | + { |
| 100 | + shiftOut(data, clock, MSBFIRST, col + 1); |
| 101 | + shiftOut(data, clock, MSBFIRST, value); |
| 102 | + buffer[col * i] = value; |
| 103 | + } |
| 104 | + digitalWrite(load, LOW); |
| 105 | + digitalWrite(load, HIGH); |
| 106 | +} |
| 107 | + |
| 108 | +void MaxMatrix::setDot(byte col, byte row, byte value) |
| 109 | +{ |
| 110 | + bitWrite(buffer[col], row, value); |
| 111 | + |
| 112 | + int n = col / 8; |
| 113 | + int c = col % 8; |
| 114 | + digitalWrite(load, LOW); |
| 115 | + for (int i=0; i<num; i++) |
| 116 | + { |
| 117 | + if (i == n) |
| 118 | + { |
| 119 | + shiftOut(data, clock, MSBFIRST, c + 1); |
| 120 | + shiftOut(data, clock, MSBFIRST, buffer[col]); |
| 121 | + } |
| 122 | + else |
| 123 | + { |
| 124 | + shiftOut(data, clock, MSBFIRST, 0); |
| 125 | + shiftOut(data, clock, MSBFIRST, 0); |
| 126 | + } |
| 127 | + } |
| 128 | + digitalWrite(load, LOW); |
| 129 | + digitalWrite(load, HIGH); |
| 130 | +} |
| 131 | + |
| 132 | +void MaxMatrix::writeSprite(int x, int y, const byte* sprite) |
| 133 | +{ |
| 134 | + int w = sprite[0]; |
| 135 | + int h = sprite[1]; |
| 136 | + |
| 137 | + if (h == 8 && y == 0) |
| 138 | + for (int i=0; i<w; i++) |
| 139 | + { |
| 140 | + int c = x + i; |
| 141 | + if (c>=0 && c<80) |
| 142 | + setColumn(c, sprite[i+2]); |
| 143 | + } |
| 144 | + else |
| 145 | + for (int i=0; i<w; i++) |
| 146 | + for (int j=0; j<h; j++) |
| 147 | + { |
| 148 | + int c = x + i; |
| 149 | + int r = y + j; |
| 150 | + if (c>=0 && c<80 && r>=0 && r<8) |
| 151 | + setDot(c, r, bitRead(sprite[i+2], j)); |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +void MaxMatrix::reload() |
| 156 | +{ |
| 157 | + for (int i=0; i<8; i++) |
| 158 | + { |
| 159 | + int col = i; |
| 160 | + digitalWrite(load, LOW); |
| 161 | + for (int j=0; j<num; j++) |
| 162 | + { |
| 163 | + shiftOut(data, clock, MSBFIRST, i + 1); |
| 164 | + shiftOut(data, clock, MSBFIRST, buffer[col]); |
| 165 | + col += 8; |
| 166 | + } |
| 167 | + digitalWrite(load, LOW); |
| 168 | + digitalWrite(load, HIGH); |
| 169 | + } |
| 170 | +} |
| 171 | + |
| 172 | +void MaxMatrix::shiftLeft(bool rotate, bool fill_zero) |
| 173 | +{ |
| 174 | + byte old = buffer[0]; |
| 175 | + int i; |
| 176 | + for (i=0; i<80; i++) |
| 177 | + buffer[i] = buffer[i+1]; |
| 178 | + if (rotate) buffer[num*8-1] = old; |
| 179 | + else if (fill_zero) buffer[num*8-1] = 0; |
| 180 | + |
| 181 | + reload(); |
| 182 | +} |
| 183 | + |
| 184 | +void MaxMatrix::shiftRight(bool rotate, bool fill_zero) |
| 185 | +{ |
| 186 | + int last = num*8-1; |
| 187 | + byte old = buffer[last]; |
| 188 | + int i; |
| 189 | + for (i=79; i>0; i--) |
| 190 | + buffer[i] = buffer[i-1]; |
| 191 | + if (rotate) buffer[0] = old; |
| 192 | + else if (fill_zero) buffer[0] = 0; |
| 193 | + |
| 194 | + reload(); |
| 195 | +} |
| 196 | + |
| 197 | +void MaxMatrix::shiftUp(bool rotate) |
| 198 | +{ |
| 199 | + for (int i=0; i<num*8; i++) |
| 200 | + { |
| 201 | + bool b = buffer[i] & 1; |
| 202 | + buffer[i] >>= 1; |
| 203 | + if (rotate) bitWrite(buffer[i], 7, b); |
| 204 | + } |
| 205 | + reload(); |
| 206 | +} |
| 207 | + |
| 208 | +void MaxMatrix::shiftDown(bool rotate) |
| 209 | +{ |
| 210 | + for (int i=0; i<num*8; i++) |
| 211 | + { |
| 212 | + bool b = buffer[i] & 128; |
| 213 | + buffer[i] <<= 1; |
| 214 | + if (rotate) bitWrite(buffer[i], 0, b); |
| 215 | + } |
| 216 | + reload(); |
| 217 | +} |
| 218 | + |
| 219 | +/** |
| 220 | + * Put extracted character on Display |
| 221 | + * |
| 222 | + */ |
| 223 | +void MaxMatrix::printCharWithShift(char c, int shift_speed){ |
| 224 | + if (c < 32) return; |
| 225 | + c -= 32; |
| 226 | + memcpy_P(buffer2, CH + 7*c, 7); |
| 227 | + writeSprite(num*8, 0, buffer2); |
| 228 | + setColumn(num*8 + buffer2[0], 0); |
| 229 | + |
| 230 | + for (int i=0; i<buffer2[0]+1; i++) |
| 231 | + { |
| 232 | + delay(shift_speed); |
| 233 | + shiftLeft(false, false); |
| 234 | + } |
| 235 | +} |
| 236 | + |
| 237 | +/** |
| 238 | + * Extract characters from Scrolling text |
| 239 | + * |
| 240 | + */ |
| 241 | +void MaxMatrix::printStringWithShift(char* s, int shift_speed){ |
| 242 | + while (*s != 0){ |
| 243 | + printCharWithShift(*s, shift_speed); |
| 244 | + s++; |
| 245 | + } |
| 246 | +} |
0 commit comments