|
| 1 | +/* |
| 2 | + Figure drawing library |
| 3 | +
|
| 4 | + Copyright 2009/2010 Benjamin Sonntag <[email protected]> http://benjamin.sonntag.fr/ |
| 5 | + |
| 6 | + History: |
| 7 | + 2010-01-01 - V0.0 Initial code at Berlin after 26C3 |
| 8 | +
|
| 9 | + This library is free software; you can redistribute it and/or |
| 10 | + modify it under the terms of the GNU Lesser General Public |
| 11 | + License as published by the Free Software Foundation; either |
| 12 | + version 2.1 of the License, or (at your option) any later version. |
| 13 | +
|
| 14 | + This library is distributed in the hope that it will be useful, |
| 15 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 | + Lesser General Public License for more details. |
| 18 | +
|
| 19 | + You should have received a copy of the GNU Lesser General Public License |
| 20 | + along with this program; if not, write to the Free Software |
| 21 | + Foundation, Inc., 59 Temple Place - Suite 330, |
| 22 | + Boston, MA 02111-1307, USA. |
| 23 | +*/ |
| 24 | + |
| 25 | +#include "Figure.h" |
| 26 | +#include "Charliplexing.h" |
| 27 | +#include <Arduino.h> |
| 28 | +#include <inttypes.h> |
| 29 | +#include <avr/pgmspace.h> |
| 30 | + |
| 31 | +#define C(c,r) ((c << 4) | r) |
| 32 | + |
| 33 | +PROGMEM const uint8_t figuresData[][14] = { |
| 34 | +{ C(0,0), C(1,0), C(2,0), C(0,1), C(2,1), C(0,2), C(2,2), C(0,3), C(2,3), C(0,4), C(1,4), C(2,4), 255, 255 }, |
| 35 | +{ C(1,0), C(0,1), C(1,1), C(1,2), C(1,3), C(0,4), C(1,4), C(2,4), 255, 255, 255, 255, 255, 255 }, |
| 36 | +{ C(0,0), C(1,0), C(2,0), C(2,1), C(1,2), C(0,3), C(0,4), C(1,4), C(2,4), 255, 255, 255, 255, 255 }, |
| 37 | +{ C(0,0), C(1,0), C(2,0), C(2,1), C(0,2), C(1,2), C(2,3), C(0,4), C(1,4), C(2,4), 255, 255, 255, 255 }, |
| 38 | +{ C(0,0), C(2,0), C(0,1), C(2,1), C(0,2), C(1,2), C(2,2), C(2,3), C(2,4), 255, 255, 255, 255, 255 }, |
| 39 | +{ C(0,0), C(1,0), C(2,0), C(0,1), C(0,2), C(1,2), C(2,2), C(2,3), C(0,4), C(1,4), C(2,4), 255, 255, 255 }, |
| 40 | +{ C(0,0), C(1,0), C(2,0), C(0,1), C(0,2), C(1,2), C(2,2), C(0,3), C(2,3), C(0,4), C(1,4), C(2,4), 255, 255 }, |
| 41 | +{ C(0,0), C(1,0), C(2,0), C(2,1), C(2,2), C(1,3), C(1,4), 255, 255, 255, 255, 255, 255, 255 }, |
| 42 | +{ C(0,0), C(1,0), C(2,0), C(0,1), C(2,1), C(0,2), C(1,2), C(2,2), C(0,3), C(2,3), C(0,4), C(1,4), C(2,4), 255 }, |
| 43 | +{ C(0,0), C(1,0), C(2,0), C(0,1), C(2,1), C(0,2), C(1,2), C(2,2), C(2,3), C(0,4), C(1,4), 255, 255, 255 } |
| 44 | +}; |
| 45 | + |
| 46 | + |
| 47 | +/* ----------------------------------------------------------------- */ |
| 48 | +/** Draws a figure (0-9). You should call it with set=1, |
| 49 | + * wait a little them call it again with set=0 |
| 50 | + * @param figure is the figure [0-9] |
| 51 | + * @param x,y coordinates, |
| 52 | + * @param set is 1 or 0 to draw or clear it |
| 53 | + */ |
| 54 | +void Figure::Draw(uint8_t figure, int x, int y, uint8_t c) { |
| 55 | + const uint8_t* character = figuresData[figure]; |
| 56 | + for (;;) { |
| 57 | + uint8_t data = pgm_read_byte_near(character++); |
| 58 | + if (data == 255) |
| 59 | + break; |
| 60 | + uint8_t charCol = data >> 4, charRow = data & 15; |
| 61 | + if ( |
| 62 | + charCol+x<DISPLAY_COLS && |
| 63 | + charCol+x>=0 && |
| 64 | + charRow+y<DISPLAY_ROWS && |
| 65 | + charRow+y>=0 |
| 66 | + ) { |
| 67 | + LedSign::Set(charCol+x, charRow+y, c); |
| 68 | + } |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | + |
| 73 | +/* ----------------------------------------------------------------- */ |
| 74 | +/** Draw a figure in the other direction (rotated 90°) |
| 75 | + * You should call it with set=1, |
| 76 | + * wait a little them call it again with set=0 |
| 77 | + * @param figure is the figure [0-9] |
| 78 | + * @param x,y coordinates, |
| 79 | + * @param set is 1 or 0 to draw or clear it |
| 80 | +*/ |
| 81 | +void Figure::Draw90(uint8_t figure, int x, int y, uint8_t c) { |
| 82 | + const uint8_t* character = figuresData[figure]; |
| 83 | + for (;;) { |
| 84 | + uint8_t data = pgm_read_byte_near(character++); |
| 85 | + if (data == 255) |
| 86 | + break; |
| 87 | + uint8_t charCol = data >> 4, charRow = data & 15; |
| 88 | + if ( |
| 89 | + (5-charRow)+x<DISPLAY_COLS && |
| 90 | + (5-charRow)+x>=0 && |
| 91 | + charCol+y<DISPLAY_ROWS && |
| 92 | + charCol+y>=0 |
| 93 | + ) { |
| 94 | + LedSign::Set((5-charRow)+x, charCol+y, c); |
| 95 | + } |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | + |
| 100 | +/* ----------------------------------------------------------------- */ |
| 101 | +/** Scroll a number from right to left |
| 102 | + * remove unused figures (0 at the left) |
| 103 | + * valid for up to 7 figures. |
| 104 | + * @param value is the value to draw and scroll |
| 105 | + * @param x is the coordinate where we put the top of the figure [0-13] |
| 106 | +*/ |
| 107 | +void Figure::Scroll90(unsigned long value,uint8_t x) { |
| 108 | + int8_t i,j,k; |
| 109 | + uint8_t figures[10]; |
| 110 | + |
| 111 | + j = 0; |
| 112 | + do { |
| 113 | + figures[j++] = value%10; |
| 114 | + value/=10; |
| 115 | + } while (value); |
| 116 | + |
| 117 | + for (i=9+4*j; i>=0; i--) { |
| 118 | + for (k=j; k>0; k--) |
| 119 | + Figure::Draw90(figures[k-1], x, i-4*k, SHADES-1); |
| 120 | + delay(100); |
| 121 | + for (k=j; k>0; k--) |
| 122 | + Figure::Draw90(figures[k-1], x, i-4*k, 0); |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | + |
0 commit comments