Clemens Valens
/
LED_panel
32x64 3-color message board http://elektorembedded.blogspot.com/
main.cpp@0:7a63bd42cf24, 2010-05-05 (annotated)
- Committer:
- Clemo
- Date:
- Wed May 05 12:04:34 2010 +0000
- Revision:
- 0:7a63bd42cf24
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Clemo | 0:7a63bd42cf24 | 1 | #include "mbed.h" |
Clemo | 0:7a63bd42cf24 | 2 | #include "font.h" |
Clemo | 0:7a63bd42cf24 | 3 | #include "arial_8pt.h" |
Clemo | 0:7a63bd42cf24 | 4 | #include "ini.h" |
Clemo | 0:7a63bd42cf24 | 5 | #include <ctype.h> |
Clemo | 0:7a63bd42cf24 | 6 | |
Clemo | 0:7a63bd42cf24 | 7 | |
Clemo | 0:7a63bd42cf24 | 8 | #define ROWS 16 /* Two rows are drawn "in parallel". */ |
Clemo | 0:7a63bd42cf24 | 9 | #define COLUMNS 64 |
Clemo | 0:7a63bd42cf24 | 10 | |
Clemo | 0:7a63bd42cf24 | 11 | typedef enum |
Clemo | 0:7a63bd42cf24 | 12 | { |
Clemo | 0:7a63bd42cf24 | 13 | kOrange = 0, |
Clemo | 0:7a63bd42cf24 | 14 | kGreen = 1, |
Clemo | 0:7a63bd42cf24 | 15 | kRed = 2, |
Clemo | 0:7a63bd42cf24 | 16 | kBlack = 3 |
Clemo | 0:7a63bd42cf24 | 17 | } |
Clemo | 0:7a63bd42cf24 | 18 | color_t; |
Clemo | 0:7a63bd42cf24 | 19 | |
Clemo | 0:7a63bd42cf24 | 20 | typedef struct |
Clemo | 0:7a63bd42cf24 | 21 | { |
Clemo | 0:7a63bd42cf24 | 22 | unsigned char pixel[COLUMNS]; // Contains data for 2 rows! |
Clemo | 0:7a63bd42cf24 | 23 | } |
Clemo | 0:7a63bd42cf24 | 24 | row_t; |
Clemo | 0:7a63bd42cf24 | 25 | |
Clemo | 0:7a63bd42cf24 | 26 | typedef struct |
Clemo | 0:7a63bd42cf24 | 27 | { |
Clemo | 0:7a63bd42cf24 | 28 | int rows; |
Clemo | 0:7a63bd42cf24 | 29 | int columns; |
Clemo | 0:7a63bd42cf24 | 30 | row_t row[ROWS]; |
Clemo | 0:7a63bd42cf24 | 31 | } |
Clemo | 0:7a63bd42cf24 | 32 | matrix_t; |
Clemo | 0:7a63bd42cf24 | 33 | |
Clemo | 0:7a63bd42cf24 | 34 | matrix_t Panel; |
Clemo | 0:7a63bd42cf24 | 35 | |
Clemo | 0:7a63bd42cf24 | 36 | DigitalOut led1(LED1); |
Clemo | 0:7a63bd42cf24 | 37 | DigitalOut led2(LED2); |
Clemo | 0:7a63bd42cf24 | 38 | DigitalOut led3(LED3); |
Clemo | 0:7a63bd42cf24 | 39 | DigitalOut led4(LED4); |
Clemo | 0:7a63bd42cf24 | 40 | |
Clemo | 0:7a63bd42cf24 | 41 | DigitalOut Enable(p5); // enable |
Clemo | 0:7a63bd42cf24 | 42 | DigitalOut Clock(p15); // clock per bit |
Clemo | 0:7a63bd42cf24 | 43 | DigitalOut Latch(p14); // latch per line |
Clemo | 0:7a63bd42cf24 | 44 | DigitalOut R1(p6); // Red upper half |
Clemo | 0:7a63bd42cf24 | 45 | DigitalOut R2(p7); // Red lower half |
Clemo | 0:7a63bd42cf24 | 46 | DigitalOut G1(p12); // Green upper half |
Clemo | 0:7a63bd42cf24 | 47 | DigitalOut G2(p13); // Green lower half |
Clemo | 0:7a63bd42cf24 | 48 | BusOut Row(p8,p9,p10,p11); |
Clemo | 0:7a63bd42cf24 | 49 | |
Clemo | 0:7a63bd42cf24 | 50 | DigitalIn Switch(p20); // Day selection switch. |
Clemo | 0:7a63bd42cf24 | 51 | |
Clemo | 0:7a63bd42cf24 | 52 | #define PANEL_ON Enable = 0 |
Clemo | 0:7a63bd42cf24 | 53 | #define PANEL_OFF Enable = 1 |
Clemo | 0:7a63bd42cf24 | 54 | #define CLOCK_DATA Clock=0; Clock=1 |
Clemo | 0:7a63bd42cf24 | 55 | #define LATCH_DATA Latch=0; Latch=1 |
Clemo | 0:7a63bd42cf24 | 56 | |
Clemo | 0:7a63bd42cf24 | 57 | Serial PC(USBTX,USBRX); // USB serial port. |
Clemo | 0:7a63bd42cf24 | 58 | LocalFileSystem local("local"); // File system. |
Clemo | 0:7a63bd42cf24 | 59 | const char *ini_file = "/local/pages.ini"; |
Clemo | 0:7a63bd42cf24 | 60 | |
Clemo | 0:7a63bd42cf24 | 61 | #define MAX_LINES (4) |
Clemo | 0:7a63bd42cf24 | 62 | #define MAX_LINE_LENGTH (128) /* Use very long lines for easy scrolling, we have plenty of memory. */ |
Clemo | 0:7a63bd42cf24 | 63 | #define MAX_PAGES (7) |
Clemo | 0:7a63bd42cf24 | 64 | #define SCROLL_GAP (24) /* in pixels */ |
Clemo | 0:7a63bd42cf24 | 65 | |
Clemo | 0:7a63bd42cf24 | 66 | |
Clemo | 0:7a63bd42cf24 | 67 | typedef struct |
Clemo | 0:7a63bd42cf24 | 68 | { |
Clemo | 0:7a63bd42cf24 | 69 | char str[MAX_LINE_LENGTH]; |
Clemo | 0:7a63bd42cf24 | 70 | color_t color; |
Clemo | 0:7a63bd42cf24 | 71 | int x; |
Clemo | 0:7a63bd42cf24 | 72 | int y; |
Clemo | 0:7a63bd42cf24 | 73 | int scroll; // <0=left, 0=no scroll, >0=right |
Clemo | 0:7a63bd42cf24 | 74 | int strlen_pixels; // String length in pixels. |
Clemo | 0:7a63bd42cf24 | 75 | } |
Clemo | 0:7a63bd42cf24 | 76 | line_t; |
Clemo | 0:7a63bd42cf24 | 77 | |
Clemo | 0:7a63bd42cf24 | 78 | typedef struct |
Clemo | 0:7a63bd42cf24 | 79 | { |
Clemo | 0:7a63bd42cf24 | 80 | line_t line[MAX_LINES]; |
Clemo | 0:7a63bd42cf24 | 81 | int hide; |
Clemo | 0:7a63bd42cf24 | 82 | } |
Clemo | 0:7a63bd42cf24 | 83 | page_entry_t; |
Clemo | 0:7a63bd42cf24 | 84 | |
Clemo | 0:7a63bd42cf24 | 85 | page_entry_t pages[MAX_PAGES]; |
Clemo | 0:7a63bd42cf24 | 86 | |
Clemo | 0:7a63bd42cf24 | 87 | |
Clemo | 0:7a63bd42cf24 | 88 | struct |
Clemo | 0:7a63bd42cf24 | 89 | { |
Clemo | 0:7a63bd42cf24 | 90 | float scroll_speed; |
Clemo | 0:7a63bd42cf24 | 91 | float brightness; |
Clemo | 0:7a63bd42cf24 | 92 | } |
Clemo | 0:7a63bd42cf24 | 93 | app_data; |
Clemo | 0:7a63bd42cf24 | 94 | |
Clemo | 0:7a63bd42cf24 | 95 | |
Clemo | 0:7a63bd42cf24 | 96 | void led_sweep(); |
Clemo | 0:7a63bd42cf24 | 97 | void panel_load(unsigned char *p_pixel, int nr_of_pixels); |
Clemo | 0:7a63bd42cf24 | 98 | void panel_refresh_row(int nr, row_t *p_pixel_data, int nr_of_pixels); |
Clemo | 0:7a63bd42cf24 | 99 | void panel_refresh(matrix_t *p_panel); |
Clemo | 0:7a63bd42cf24 | 100 | void panel_init(matrix_t& panel, int rows, int columns); |
Clemo | 0:7a63bd42cf24 | 101 | void panel_fill_row(row_t *p_row, unsigned char value, int count); |
Clemo | 0:7a63bd42cf24 | 102 | void panel_fill_matrix(matrix_t& panel, unsigned char value); |
Clemo | 0:7a63bd42cf24 | 103 | void put_pixel(matrix_t& panel, unsigned int x, unsigned int y, color_t color); |
Clemo | 0:7a63bd42cf24 | 104 | int put_char_xy(matrix_t& panel, char ch, unsigned int x, unsigned int y, const FONT_INFO *p_font, color_t color); |
Clemo | 0:7a63bd42cf24 | 105 | int put_string_xy(matrix_t& panel, char *p_str, unsigned int x, unsigned int y, const FONT_INFO *p_font, color_t color); |
Clemo | 0:7a63bd42cf24 | 106 | int measure_string(char *p_str, const FONT_INFO *p_font); |
Clemo | 0:7a63bd42cf24 | 107 | void clear_display(void); |
Clemo | 0:7a63bd42cf24 | 108 | int is_valid_char(char ch); |
Clemo | 0:7a63bd42cf24 | 109 | color_t str_to_color(const char *p_str); |
Clemo | 0:7a63bd42cf24 | 110 | int ini_file_handler(void* user, const char* section, const char* name, const char* value); |
Clemo | 0:7a63bd42cf24 | 111 | void page_pre_init(void); |
Clemo | 0:7a63bd42cf24 | 112 | void page_post_init(const FONT_INFO *p_font); |
Clemo | 0:7a63bd42cf24 | 113 | int page_next(int page); |
Clemo | 0:7a63bd42cf24 | 114 | int page_show(matrix_t& panel, int p); |
Clemo | 0:7a63bd42cf24 | 115 | |
Clemo | 0:7a63bd42cf24 | 116 | |
Clemo | 0:7a63bd42cf24 | 117 | void led_sweep() |
Clemo | 0:7a63bd42cf24 | 118 | { |
Clemo | 0:7a63bd42cf24 | 119 | led1 = 0; |
Clemo | 0:7a63bd42cf24 | 120 | led2 = 0; |
Clemo | 0:7a63bd42cf24 | 121 | led3 = 0; |
Clemo | 0:7a63bd42cf24 | 122 | led4 = 0; |
Clemo | 0:7a63bd42cf24 | 123 | led1 = 1; |
Clemo | 0:7a63bd42cf24 | 124 | wait(0.05); |
Clemo | 0:7a63bd42cf24 | 125 | led1 = 0; |
Clemo | 0:7a63bd42cf24 | 126 | led2 = 1; |
Clemo | 0:7a63bd42cf24 | 127 | wait(0.05); |
Clemo | 0:7a63bd42cf24 | 128 | led2 = 0; |
Clemo | 0:7a63bd42cf24 | 129 | led3 = 1; |
Clemo | 0:7a63bd42cf24 | 130 | wait(0.05); |
Clemo | 0:7a63bd42cf24 | 131 | led3 = 0; |
Clemo | 0:7a63bd42cf24 | 132 | led4 = 1; |
Clemo | 0:7a63bd42cf24 | 133 | wait(0.05); |
Clemo | 0:7a63bd42cf24 | 134 | led4 = 0; |
Clemo | 0:7a63bd42cf24 | 135 | } |
Clemo | 0:7a63bd42cf24 | 136 | |
Clemo | 0:7a63bd42cf24 | 137 | |
Clemo | 0:7a63bd42cf24 | 138 | // Clock the data into the display. |
Clemo | 0:7a63bd42cf24 | 139 | // This is where the bit-banging happens. |
Clemo | 0:7a63bd42cf24 | 140 | void panel_load(unsigned char *p_pixel, int nr_of_pixels) |
Clemo | 0:7a63bd42cf24 | 141 | { |
Clemo | 0:7a63bd42cf24 | 142 | int i; |
Clemo | 0:7a63bd42cf24 | 143 | for (i=0; i<nr_of_pixels; i++) |
Clemo | 0:7a63bd42cf24 | 144 | { |
Clemo | 0:7a63bd42cf24 | 145 | unsigned char pixel = p_pixel[i]; |
Clemo | 0:7a63bd42cf24 | 146 | R1 = pixel & 0x01; |
Clemo | 0:7a63bd42cf24 | 147 | pixel >>= 1; |
Clemo | 0:7a63bd42cf24 | 148 | G1 = pixel & 0x01; |
Clemo | 0:7a63bd42cf24 | 149 | pixel >>= 1; |
Clemo | 0:7a63bd42cf24 | 150 | R2 = pixel & 0x01; |
Clemo | 0:7a63bd42cf24 | 151 | pixel >>= 1; |
Clemo | 0:7a63bd42cf24 | 152 | G2 = pixel & 0x01; |
Clemo | 0:7a63bd42cf24 | 153 | CLOCK_DATA; |
Clemo | 0:7a63bd42cf24 | 154 | } |
Clemo | 0:7a63bd42cf24 | 155 | LATCH_DATA; |
Clemo | 0:7a63bd42cf24 | 156 | } |
Clemo | 0:7a63bd42cf24 | 157 | |
Clemo | 0:7a63bd42cf24 | 158 | |
Clemo | 0:7a63bd42cf24 | 159 | void panel_refresh_row(int nr, row_t *p_pixel_data, int nr_of_pixels) |
Clemo | 0:7a63bd42cf24 | 160 | { |
Clemo | 0:7a63bd42cf24 | 161 | Row = nr; |
Clemo | 0:7a63bd42cf24 | 162 | panel_load(p_pixel_data->pixel,nr_of_pixels); |
Clemo | 0:7a63bd42cf24 | 163 | PANEL_ON; |
Clemo | 0:7a63bd42cf24 | 164 | wait(app_data.brightness); |
Clemo | 0:7a63bd42cf24 | 165 | PANEL_OFF; |
Clemo | 0:7a63bd42cf24 | 166 | } |
Clemo | 0:7a63bd42cf24 | 167 | |
Clemo | 0:7a63bd42cf24 | 168 | |
Clemo | 0:7a63bd42cf24 | 169 | void panel_refresh(matrix_t *p_panel) |
Clemo | 0:7a63bd42cf24 | 170 | { |
Clemo | 0:7a63bd42cf24 | 171 | int i; |
Clemo | 0:7a63bd42cf24 | 172 | for (i=0; i<p_panel->rows; i++) |
Clemo | 0:7a63bd42cf24 | 173 | { |
Clemo | 0:7a63bd42cf24 | 174 | panel_refresh_row(i,&p_panel->row[i],p_panel->columns); |
Clemo | 0:7a63bd42cf24 | 175 | } |
Clemo | 0:7a63bd42cf24 | 176 | } |
Clemo | 0:7a63bd42cf24 | 177 | |
Clemo | 0:7a63bd42cf24 | 178 | |
Clemo | 0:7a63bd42cf24 | 179 | void panel_init(matrix_t& panel, int rows, int columns) |
Clemo | 0:7a63bd42cf24 | 180 | { |
Clemo | 0:7a63bd42cf24 | 181 | CLOCK_DATA; // Init the clock line |
Clemo | 0:7a63bd42cf24 | 182 | LATCH_DATA; // Reset the shift registers |
Clemo | 0:7a63bd42cf24 | 183 | panel.rows = rows; |
Clemo | 0:7a63bd42cf24 | 184 | panel.columns = columns; |
Clemo | 0:7a63bd42cf24 | 185 | panel_fill_matrix(panel,kBlack); // Clear display. |
Clemo | 0:7a63bd42cf24 | 186 | } |
Clemo | 0:7a63bd42cf24 | 187 | |
Clemo | 0:7a63bd42cf24 | 188 | |
Clemo | 0:7a63bd42cf24 | 189 | void panel_fill_row(row_t *p_row, unsigned char value, int count) |
Clemo | 0:7a63bd42cf24 | 190 | { |
Clemo | 0:7a63bd42cf24 | 191 | int i; |
Clemo | 0:7a63bd42cf24 | 192 | for (i=0; i<count; i++) |
Clemo | 0:7a63bd42cf24 | 193 | { |
Clemo | 0:7a63bd42cf24 | 194 | p_row->pixel[i] = value; |
Clemo | 0:7a63bd42cf24 | 195 | } |
Clemo | 0:7a63bd42cf24 | 196 | } |
Clemo | 0:7a63bd42cf24 | 197 | |
Clemo | 0:7a63bd42cf24 | 198 | |
Clemo | 0:7a63bd42cf24 | 199 | void panel_fill_matrix(matrix_t& panel, unsigned char value) |
Clemo | 0:7a63bd42cf24 | 200 | { |
Clemo | 0:7a63bd42cf24 | 201 | int i; |
Clemo | 0:7a63bd42cf24 | 202 | for (i=0; i<panel.rows; i++) |
Clemo | 0:7a63bd42cf24 | 203 | { |
Clemo | 0:7a63bd42cf24 | 204 | // Two rows are stored in one row... |
Clemo | 0:7a63bd42cf24 | 205 | panel_fill_row(&panel.row[i],(value<<2)+value,panel.columns); |
Clemo | 0:7a63bd42cf24 | 206 | } |
Clemo | 0:7a63bd42cf24 | 207 | } |
Clemo | 0:7a63bd42cf24 | 208 | |
Clemo | 0:7a63bd42cf24 | 209 | |
Clemo | 0:7a63bd42cf24 | 210 | void put_pixel(matrix_t& panel, unsigned int x, unsigned int y, color_t color) |
Clemo | 0:7a63bd42cf24 | 211 | { |
Clemo | 0:7a63bd42cf24 | 212 | unsigned char mask; |
Clemo | 0:7a63bd42cf24 | 213 | unsigned char c; |
Clemo | 0:7a63bd42cf24 | 214 | |
Clemo | 0:7a63bd42cf24 | 215 | if (x<panel.columns && y<2*panel.rows) |
Clemo | 0:7a63bd42cf24 | 216 | { |
Clemo | 0:7a63bd42cf24 | 217 | mask = 0x03; |
Clemo | 0:7a63bd42cf24 | 218 | c = color; |
Clemo | 0:7a63bd42cf24 | 219 | if (y>=panel.rows) |
Clemo | 0:7a63bd42cf24 | 220 | { |
Clemo | 0:7a63bd42cf24 | 221 | // Remember: two rows are stored in one. |
Clemo | 0:7a63bd42cf24 | 222 | y -= panel.rows; |
Clemo | 0:7a63bd42cf24 | 223 | c <<= 2; |
Clemo | 0:7a63bd42cf24 | 224 | mask <<= 2; |
Clemo | 0:7a63bd42cf24 | 225 | } |
Clemo | 0:7a63bd42cf24 | 226 | c |= ~mask; // Do not overwrite the other pixel. |
Clemo | 0:7a63bd42cf24 | 227 | panel.row[y].pixel[x] |= mask; // Clear pixel (pixels are active low!). |
Clemo | 0:7a63bd42cf24 | 228 | panel.row[y].pixel[x] &= c; // Add in the zeroes. |
Clemo | 0:7a63bd42cf24 | 229 | } |
Clemo | 0:7a63bd42cf24 | 230 | } |
Clemo | 0:7a63bd42cf24 | 231 | |
Clemo | 0:7a63bd42cf24 | 232 | |
Clemo | 0:7a63bd42cf24 | 233 | int put_char_xy(matrix_t& panel, char ch, unsigned int x, unsigned int y, const FONT_INFO *p_font, color_t color) |
Clemo | 0:7a63bd42cf24 | 234 | { |
Clemo | 0:7a63bd42cf24 | 235 | int i, h, w; |
Clemo | 0:7a63bd42cf24 | 236 | if (p_font!=NULL) |
Clemo | 0:7a63bd42cf24 | 237 | { |
Clemo | 0:7a63bd42cf24 | 238 | i = ch - p_font->start_char; |
Clemo | 0:7a63bd42cf24 | 239 | int width = p_font->p_character_descriptor[i].width; |
Clemo | 0:7a63bd42cf24 | 240 | int second_byte = 0; |
Clemo | 0:7a63bd42cf24 | 241 | if (width>8) |
Clemo | 0:7a63bd42cf24 | 242 | { |
Clemo | 0:7a63bd42cf24 | 243 | // Some wide characters are coded in two bytes. |
Clemo | 0:7a63bd42cf24 | 244 | second_byte = width - 8; |
Clemo | 0:7a63bd42cf24 | 245 | width = 8; |
Clemo | 0:7a63bd42cf24 | 246 | } |
Clemo | 0:7a63bd42cf24 | 247 | int offset = p_font->p_character_descriptor[i].offset; |
Clemo | 0:7a63bd42cf24 | 248 | const uint8_t *p_char = p_font->p_character_bitmaps + offset; |
Clemo | 0:7a63bd42cf24 | 249 | uint8_t mask; |
Clemo | 0:7a63bd42cf24 | 250 | for (h=0; h<p_font->height; h++) |
Clemo | 0:7a63bd42cf24 | 251 | { |
Clemo | 0:7a63bd42cf24 | 252 | // Plot pixels for first byte. |
Clemo | 0:7a63bd42cf24 | 253 | mask = 0x80; |
Clemo | 0:7a63bd42cf24 | 254 | for (w=0; w<width; w++) |
Clemo | 0:7a63bd42cf24 | 255 | { |
Clemo | 0:7a63bd42cf24 | 256 | if ((*p_char&mask)!=0) put_pixel(panel,x+w,y+h,color); |
Clemo | 0:7a63bd42cf24 | 257 | mask >>= 1; |
Clemo | 0:7a63bd42cf24 | 258 | } |
Clemo | 0:7a63bd42cf24 | 259 | if (second_byte>0) |
Clemo | 0:7a63bd42cf24 | 260 | { |
Clemo | 0:7a63bd42cf24 | 261 | // Handle 2nd byte of extra wide characters. |
Clemo | 0:7a63bd42cf24 | 262 | p_char += 1; |
Clemo | 0:7a63bd42cf24 | 263 | mask = 0x80; |
Clemo | 0:7a63bd42cf24 | 264 | for (w=0; w<second_byte; w++) |
Clemo | 0:7a63bd42cf24 | 265 | { |
Clemo | 0:7a63bd42cf24 | 266 | if ((*p_char&mask)!=0) put_pixel(panel,x+w+8,y+h,color); |
Clemo | 0:7a63bd42cf24 | 267 | mask >>= 1; |
Clemo | 0:7a63bd42cf24 | 268 | } |
Clemo | 0:7a63bd42cf24 | 269 | } |
Clemo | 0:7a63bd42cf24 | 270 | p_char += 1; |
Clemo | 0:7a63bd42cf24 | 271 | } |
Clemo | 0:7a63bd42cf24 | 272 | return p_font->p_character_descriptor[i].width; |
Clemo | 0:7a63bd42cf24 | 273 | } |
Clemo | 0:7a63bd42cf24 | 274 | return 0; |
Clemo | 0:7a63bd42cf24 | 275 | } |
Clemo | 0:7a63bd42cf24 | 276 | |
Clemo | 0:7a63bd42cf24 | 277 | |
Clemo | 0:7a63bd42cf24 | 278 | int put_string_xy(matrix_t& panel, char *p_str, unsigned int x, unsigned int y, const FONT_INFO *p_font, color_t color) |
Clemo | 0:7a63bd42cf24 | 279 | { |
Clemo | 0:7a63bd42cf24 | 280 | int _x = 0; |
Clemo | 0:7a63bd42cf24 | 281 | while (*p_str!=0) |
Clemo | 0:7a63bd42cf24 | 282 | { |
Clemo | 0:7a63bd42cf24 | 283 | _x += put_char_xy(panel,*p_str,x+_x,y,p_font,color); |
Clemo | 0:7a63bd42cf24 | 284 | _x += 1; |
Clemo | 0:7a63bd42cf24 | 285 | p_str += 1; |
Clemo | 0:7a63bd42cf24 | 286 | } |
Clemo | 0:7a63bd42cf24 | 287 | return _x>0? _x-1 : 0; |
Clemo | 0:7a63bd42cf24 | 288 | } |
Clemo | 0:7a63bd42cf24 | 289 | |
Clemo | 0:7a63bd42cf24 | 290 | |
Clemo | 0:7a63bd42cf24 | 291 | // Return the length of a string in pixels when printed using the provided font. |
Clemo | 0:7a63bd42cf24 | 292 | int measure_string(char *p_str, const FONT_INFO *p_font) |
Clemo | 0:7a63bd42cf24 | 293 | { |
Clemo | 0:7a63bd42cf24 | 294 | int i; |
Clemo | 0:7a63bd42cf24 | 295 | int strlen_pixels = 0; |
Clemo | 0:7a63bd42cf24 | 296 | if (p_font!=NULL) |
Clemo | 0:7a63bd42cf24 | 297 | { |
Clemo | 0:7a63bd42cf24 | 298 | while (*p_str!=0) |
Clemo | 0:7a63bd42cf24 | 299 | { |
Clemo | 0:7a63bd42cf24 | 300 | i = *p_str - p_font->start_char; |
Clemo | 0:7a63bd42cf24 | 301 | strlen_pixels += p_font->p_character_descriptor[i].width; |
Clemo | 0:7a63bd42cf24 | 302 | strlen_pixels += 1; |
Clemo | 0:7a63bd42cf24 | 303 | p_str += 1; |
Clemo | 0:7a63bd42cf24 | 304 | } |
Clemo | 0:7a63bd42cf24 | 305 | } |
Clemo | 0:7a63bd42cf24 | 306 | return strlen_pixels; |
Clemo | 0:7a63bd42cf24 | 307 | } |
Clemo | 0:7a63bd42cf24 | 308 | |
Clemo | 0:7a63bd42cf24 | 309 | |
Clemo | 0:7a63bd42cf24 | 310 | void clear_display(void) |
Clemo | 0:7a63bd42cf24 | 311 | { |
Clemo | 0:7a63bd42cf24 | 312 | panel_fill_matrix(Panel,kBlack); |
Clemo | 0:7a63bd42cf24 | 313 | } |
Clemo | 0:7a63bd42cf24 | 314 | |
Clemo | 0:7a63bd42cf24 | 315 | |
Clemo | 0:7a63bd42cf24 | 316 | int is_valid_char(char ch) |
Clemo | 0:7a63bd42cf24 | 317 | { |
Clemo | 0:7a63bd42cf24 | 318 | return (ch>=' ' && ch<='~'); |
Clemo | 0:7a63bd42cf24 | 319 | } |
Clemo | 0:7a63bd42cf24 | 320 | |
Clemo | 0:7a63bd42cf24 | 321 | #define IS_SECTION(a) if (strcmp(p_section,(a))==0) |
Clemo | 0:7a63bd42cf24 | 322 | #define IS_NAME(a) if (strcmp(p_name,(a))==0) |
Clemo | 0:7a63bd42cf24 | 323 | #define IS_VALUE(a) if (strcmp(p_value,(a))==0) |
Clemo | 0:7a63bd42cf24 | 324 | |
Clemo | 0:7a63bd42cf24 | 325 | color_t str_to_color(const char *p_str) |
Clemo | 0:7a63bd42cf24 | 326 | { |
Clemo | 0:7a63bd42cf24 | 327 | color_t color = kGreen; |
Clemo | 0:7a63bd42cf24 | 328 | if (strcmp(p_str,"green")==0) color = kGreen; |
Clemo | 0:7a63bd42cf24 | 329 | else if (strcmp(p_str,"orange")==0) color = kOrange; |
Clemo | 0:7a63bd42cf24 | 330 | else if (strcmp(p_str,"red")==0) color = kRed; |
Clemo | 0:7a63bd42cf24 | 331 | else if (strcmp(p_str,"black")==0) color = kBlack; |
Clemo | 0:7a63bd42cf24 | 332 | return color; |
Clemo | 0:7a63bd42cf24 | 333 | } |
Clemo | 0:7a63bd42cf24 | 334 | |
Clemo | 0:7a63bd42cf24 | 335 | |
Clemo | 0:7a63bd42cf24 | 336 | int ini_file_handler(void *p_user, const char *p_section, const char *p_name, const char *p_value) |
Clemo | 0:7a63bd42cf24 | 337 | { |
Clemo | 0:7a63bd42cf24 | 338 | // Called from ini.c |
Clemo | 0:7a63bd42cf24 | 339 | |
Clemo | 0:7a63bd42cf24 | 340 | int p = 0; |
Clemo | 0:7a63bd42cf24 | 341 | |
Clemo | 0:7a63bd42cf24 | 342 | IS_SECTION("global") |
Clemo | 0:7a63bd42cf24 | 343 | { |
Clemo | 0:7a63bd42cf24 | 344 | IS_NAME("brightness") app_data.brightness = atof(p_value)/10000.0; |
Clemo | 0:7a63bd42cf24 | 345 | else IS_NAME("speed") app_data.scroll_speed = atof(p_value)/1000.0; |
Clemo | 0:7a63bd42cf24 | 346 | } |
Clemo | 0:7a63bd42cf24 | 347 | else IS_SECTION("page1") p = 0; |
Clemo | 0:7a63bd42cf24 | 348 | else IS_SECTION("page2") p = 1; |
Clemo | 0:7a63bd42cf24 | 349 | else IS_SECTION("page3") p = 2; |
Clemo | 0:7a63bd42cf24 | 350 | else IS_SECTION("page4") p = 3; |
Clemo | 0:7a63bd42cf24 | 351 | else IS_SECTION("page5") p = 4; |
Clemo | 0:7a63bd42cf24 | 352 | else IS_SECTION("page6") p = 5; |
Clemo | 0:7a63bd42cf24 | 353 | else IS_SECTION("page7") p = 6; |
Clemo | 0:7a63bd42cf24 | 354 | |
Clemo | 0:7a63bd42cf24 | 355 | IS_NAME("hide") pages[p].hide = atoi(p_value); |
Clemo | 0:7a63bd42cf24 | 356 | else IS_NAME("text1") strcpy(pages[p].line[0].str,p_value); |
Clemo | 0:7a63bd42cf24 | 357 | else IS_NAME("text2") strcpy(pages[p].line[1].str,p_value); |
Clemo | 0:7a63bd42cf24 | 358 | else IS_NAME("text3") strcpy(pages[p].line[2].str,p_value); |
Clemo | 0:7a63bd42cf24 | 359 | else IS_NAME("text4") strcpy(pages[p].line[3].str,p_value); |
Clemo | 0:7a63bd42cf24 | 360 | else IS_NAME("color1") pages[p].line[0].color = str_to_color(p_value); |
Clemo | 0:7a63bd42cf24 | 361 | else IS_NAME("color2") pages[p].line[1].color = str_to_color(p_value); |
Clemo | 0:7a63bd42cf24 | 362 | else IS_NAME("color3") pages[p].line[2].color = str_to_color(p_value); |
Clemo | 0:7a63bd42cf24 | 363 | else IS_NAME("color4") pages[p].line[3].color = str_to_color(p_value); |
Clemo | 0:7a63bd42cf24 | 364 | else IS_NAME("x1") pages[p].line[0].x = atoi(p_value); |
Clemo | 0:7a63bd42cf24 | 365 | else IS_NAME("x2") pages[p].line[1].x = atoi(p_value); |
Clemo | 0:7a63bd42cf24 | 366 | else IS_NAME("x3") pages[p].line[2].x = atoi(p_value); |
Clemo | 0:7a63bd42cf24 | 367 | else IS_NAME("x4") pages[p].line[3].x = atoi(p_value); |
Clemo | 0:7a63bd42cf24 | 368 | else IS_NAME("y1") pages[p].line[0].y = atoi(p_value); |
Clemo | 0:7a63bd42cf24 | 369 | else IS_NAME("y2") pages[p].line[1].y = atoi(p_value); |
Clemo | 0:7a63bd42cf24 | 370 | else IS_NAME("y3") pages[p].line[2].y = atoi(p_value); |
Clemo | 0:7a63bd42cf24 | 371 | else IS_NAME("y4") pages[p].line[3].y = atoi(p_value); |
Clemo | 0:7a63bd42cf24 | 372 | else IS_NAME("scroll1") pages[p].line[0].scroll = atoi(p_value); |
Clemo | 0:7a63bd42cf24 | 373 | else IS_NAME("scroll2") pages[p].line[1].scroll = atoi(p_value); |
Clemo | 0:7a63bd42cf24 | 374 | else IS_NAME("scroll3") pages[p].line[2].scroll = atoi(p_value); |
Clemo | 0:7a63bd42cf24 | 375 | else IS_NAME("scroll4") pages[p].line[3].scroll = atoi(p_value); |
Clemo | 0:7a63bd42cf24 | 376 | |
Clemo | 0:7a63bd42cf24 | 377 | // Return 0 on error. |
Clemo | 0:7a63bd42cf24 | 378 | return 1; |
Clemo | 0:7a63bd42cf24 | 379 | } |
Clemo | 0:7a63bd42cf24 | 380 | |
Clemo | 0:7a63bd42cf24 | 381 | |
Clemo | 0:7a63bd42cf24 | 382 | void page_pre_init(void) |
Clemo | 0:7a63bd42cf24 | 383 | { |
Clemo | 0:7a63bd42cf24 | 384 | int i, j; |
Clemo | 0:7a63bd42cf24 | 385 | int y; |
Clemo | 0:7a63bd42cf24 | 386 | // Set zero default values. |
Clemo | 0:7a63bd42cf24 | 387 | memset(&pages,0,sizeof(pages)); |
Clemo | 0:7a63bd42cf24 | 388 | for (j=0; j<MAX_PAGES; j++) |
Clemo | 0:7a63bd42cf24 | 389 | { |
Clemo | 0:7a63bd42cf24 | 390 | y = 0; |
Clemo | 0:7a63bd42cf24 | 391 | for (i=0; i<MAX_LINES; i++) |
Clemo | 0:7a63bd42cf24 | 392 | { |
Clemo | 0:7a63bd42cf24 | 393 | // Set non-zero default values. |
Clemo | 0:7a63bd42cf24 | 394 | pages[j].line[i].color = kBlack; |
Clemo | 0:7a63bd42cf24 | 395 | pages[j].line[i].y = y; |
Clemo | 0:7a63bd42cf24 | 396 | y += 8; |
Clemo | 0:7a63bd42cf24 | 397 | } |
Clemo | 0:7a63bd42cf24 | 398 | } |
Clemo | 0:7a63bd42cf24 | 399 | } |
Clemo | 0:7a63bd42cf24 | 400 | |
Clemo | 0:7a63bd42cf24 | 401 | |
Clemo | 0:7a63bd42cf24 | 402 | void page_post_init(const FONT_INFO *p_font) |
Clemo | 0:7a63bd42cf24 | 403 | { |
Clemo | 0:7a63bd42cf24 | 404 | int i, j; |
Clemo | 0:7a63bd42cf24 | 405 | for (j=0; j<MAX_PAGES; j++) |
Clemo | 0:7a63bd42cf24 | 406 | { |
Clemo | 0:7a63bd42cf24 | 407 | for (i=0; i<MAX_LINES; i++) |
Clemo | 0:7a63bd42cf24 | 408 | { |
Clemo | 0:7a63bd42cf24 | 409 | // We need to know the length in pixels of each string. |
Clemo | 0:7a63bd42cf24 | 410 | pages[j].line[i].strlen_pixels = measure_string(pages[j].line[i].str,p_font); |
Clemo | 0:7a63bd42cf24 | 411 | } |
Clemo | 0:7a63bd42cf24 | 412 | } |
Clemo | 0:7a63bd42cf24 | 413 | } |
Clemo | 0:7a63bd42cf24 | 414 | |
Clemo | 0:7a63bd42cf24 | 415 | |
Clemo | 0:7a63bd42cf24 | 416 | int page_next(int page) |
Clemo | 0:7a63bd42cf24 | 417 | { |
Clemo | 0:7a63bd42cf24 | 418 | page += 1; |
Clemo | 0:7a63bd42cf24 | 419 | if (page<0) page = MAX_PAGES - 1; |
Clemo | 0:7a63bd42cf24 | 420 | else if (page>=MAX_PAGES) page = 0; |
Clemo | 0:7a63bd42cf24 | 421 | return page; |
Clemo | 0:7a63bd42cf24 | 422 | } |
Clemo | 0:7a63bd42cf24 | 423 | |
Clemo | 0:7a63bd42cf24 | 424 | |
Clemo | 0:7a63bd42cf24 | 425 | int page_show(matrix_t& panel, int p) |
Clemo | 0:7a63bd42cf24 | 426 | { |
Clemo | 0:7a63bd42cf24 | 427 | int i; |
Clemo | 0:7a63bd42cf24 | 428 | clear_display(); |
Clemo | 0:7a63bd42cf24 | 429 | // Show page if not hidden. |
Clemo | 0:7a63bd42cf24 | 430 | if (pages[p].hide==0) |
Clemo | 0:7a63bd42cf24 | 431 | { |
Clemo | 0:7a63bd42cf24 | 432 | for (i=0; i<MAX_LINES; i++) |
Clemo | 0:7a63bd42cf24 | 433 | { |
Clemo | 0:7a63bd42cf24 | 434 | int x = pages[p].line[i].x; |
Clemo | 0:7a63bd42cf24 | 435 | // Print the string. |
Clemo | 0:7a63bd42cf24 | 436 | put_string_xy(panel,pages[p].line[i].str,x,pages[p].line[i].y,&arial_8pt_font_info,pages[p].line[i].color); |
Clemo | 0:7a63bd42cf24 | 437 | // Handle scrolling. |
Clemo | 0:7a63bd42cf24 | 438 | if (pages[p].line[i].scroll!=0) |
Clemo | 0:7a63bd42cf24 | 439 | { |
Clemo | 0:7a63bd42cf24 | 440 | // Simply print the string a second time, display clipping will prevent |
Clemo | 0:7a63bd42cf24 | 441 | // artifacts when a string is partly printed off-screen. |
Clemo | 0:7a63bd42cf24 | 442 | if (pages[p].line[i].scroll<0) |
Clemo | 0:7a63bd42cf24 | 443 | { |
Clemo | 0:7a63bd42cf24 | 444 | // Scroll to the left. |
Clemo | 0:7a63bd42cf24 | 445 | x += (pages[p].line[i].strlen_pixels + SCROLL_GAP); |
Clemo | 0:7a63bd42cf24 | 446 | // Reset the starting point when the string is completely off screen. |
Clemo | 0:7a63bd42cf24 | 447 | if (pages[p].line[i].x+pages[p].line[i].strlen_pixels<0) pages[p].line[i].x = x; |
Clemo | 0:7a63bd42cf24 | 448 | } |
Clemo | 0:7a63bd42cf24 | 449 | else if (pages[p].line[i].scroll>0) |
Clemo | 0:7a63bd42cf24 | 450 | { |
Clemo | 0:7a63bd42cf24 | 451 | // Scroll to the right. |
Clemo | 0:7a63bd42cf24 | 452 | x -= (pages[p].line[i].strlen_pixels + SCROLL_GAP); |
Clemo | 0:7a63bd42cf24 | 453 | // Reset the starting point when the string is completely off screen. |
Clemo | 0:7a63bd42cf24 | 454 | if (pages[p].line[i].x>=COLUMNS) pages[p].line[i].x = x; |
Clemo | 0:7a63bd42cf24 | 455 | } |
Clemo | 0:7a63bd42cf24 | 456 | put_string_xy(panel,pages[p].line[i].str,x,pages[p].line[i].y,&arial_8pt_font_info,pages[p].line[i].color); |
Clemo | 0:7a63bd42cf24 | 457 | |
Clemo | 0:7a63bd42cf24 | 458 | // Update x position. |
Clemo | 0:7a63bd42cf24 | 459 | pages[p].line[i].x += pages[p].line[i].scroll; |
Clemo | 0:7a63bd42cf24 | 460 | } |
Clemo | 0:7a63bd42cf24 | 461 | } |
Clemo | 0:7a63bd42cf24 | 462 | } |
Clemo | 0:7a63bd42cf24 | 463 | else p = page_next(p); // Page is hidden, move on to the next page. |
Clemo | 0:7a63bd42cf24 | 464 | |
Clemo | 0:7a63bd42cf24 | 465 | return p; |
Clemo | 0:7a63bd42cf24 | 466 | } |
Clemo | 0:7a63bd42cf24 | 467 | |
Clemo | 0:7a63bd42cf24 | 468 | |
Clemo | 0:7a63bd42cf24 | 469 | int main() |
Clemo | 0:7a63bd42cf24 | 470 | { |
Clemo | 0:7a63bd42cf24 | 471 | int page = 0; |
Clemo | 0:7a63bd42cf24 | 472 | int debounce = 0; |
Clemo | 0:7a63bd42cf24 | 473 | const FONT_INFO *p_font = &arial_8pt_font_info; |
Clemo | 0:7a63bd42cf24 | 474 | |
Clemo | 0:7a63bd42cf24 | 475 | PC.printf("\nmbed LED panel experiments\n"); |
Clemo | 0:7a63bd42cf24 | 476 | led_sweep(); |
Clemo | 0:7a63bd42cf24 | 477 | |
Clemo | 0:7a63bd42cf24 | 478 | led1 = 0; |
Clemo | 0:7a63bd42cf24 | 479 | led2 = 0; |
Clemo | 0:7a63bd42cf24 | 480 | led3 = 0; |
Clemo | 0:7a63bd42cf24 | 481 | led4 = 0; |
Clemo | 0:7a63bd42cf24 | 482 | |
Clemo | 0:7a63bd42cf24 | 483 | // Set some default values. |
Clemo | 0:7a63bd42cf24 | 484 | app_data.brightness = 0.0005; |
Clemo | 0:7a63bd42cf24 | 485 | app_data.scroll_speed = 0.001; |
Clemo | 0:7a63bd42cf24 | 486 | |
Clemo | 0:7a63bd42cf24 | 487 | // Setup display. |
Clemo | 0:7a63bd42cf24 | 488 | PANEL_OFF; |
Clemo | 0:7a63bd42cf24 | 489 | panel_init(Panel,ROWS,COLUMNS); |
Clemo | 0:7a63bd42cf24 | 490 | |
Clemo | 0:7a63bd42cf24 | 491 | // Read page data. |
Clemo | 0:7a63bd42cf24 | 492 | page_pre_init(); |
Clemo | 0:7a63bd42cf24 | 493 | int error = false; |
Clemo | 0:7a63bd42cf24 | 494 | error = ini_parse(ini_file,ini_file_handler,0); |
Clemo | 0:7a63bd42cf24 | 495 | page_post_init(p_font); |
Clemo | 0:7a63bd42cf24 | 496 | |
Clemo | 0:7a63bd42cf24 | 497 | //sprintf(pages[0].line[0].str,"%0.3f",app_data.scroll_speed); |
Clemo | 0:7a63bd42cf24 | 498 | |
Clemo | 0:7a63bd42cf24 | 499 | while (1) |
Clemo | 0:7a63bd42cf24 | 500 | { |
Clemo | 0:7a63bd42cf24 | 501 | if (error==true) |
Clemo | 0:7a63bd42cf24 | 502 | { |
Clemo | 0:7a63bd42cf24 | 503 | put_string_xy(Panel,"file not found",0,0,p_font,kRed); |
Clemo | 0:7a63bd42cf24 | 504 | } |
Clemo | 0:7a63bd42cf24 | 505 | else |
Clemo | 0:7a63bd42cf24 | 506 | { |
Clemo | 0:7a63bd42cf24 | 507 | page = page_show(Panel,page); |
Clemo | 0:7a63bd42cf24 | 508 | } |
Clemo | 0:7a63bd42cf24 | 509 | |
Clemo | 0:7a63bd42cf24 | 510 | // Handle next-page switch. |
Clemo | 0:7a63bd42cf24 | 511 | if (Switch==1) |
Clemo | 0:7a63bd42cf24 | 512 | { |
Clemo | 0:7a63bd42cf24 | 513 | if (debounce<10) debounce += 1; |
Clemo | 0:7a63bd42cf24 | 514 | else if (debounce==10) |
Clemo | 0:7a63bd42cf24 | 515 | { |
Clemo | 0:7a63bd42cf24 | 516 | debounce += 1; |
Clemo | 0:7a63bd42cf24 | 517 | page = page_next(page); |
Clemo | 0:7a63bd42cf24 | 518 | clear_display(); |
Clemo | 0:7a63bd42cf24 | 519 | } |
Clemo | 0:7a63bd42cf24 | 520 | } |
Clemo | 0:7a63bd42cf24 | 521 | else debounce = 0; |
Clemo | 0:7a63bd42cf24 | 522 | |
Clemo | 0:7a63bd42cf24 | 523 | /*if (PC.readable()) |
Clemo | 0:7a63bd42cf24 | 524 | { |
Clemo | 0:7a63bd42cf24 | 525 | int ch; |
Clemo | 0:7a63bd42cf24 | 526 | ch = PC.getc(); |
Clemo | 0:7a63bd42cf24 | 527 | PC.putc(ch); |
Clemo | 0:7a63bd42cf24 | 528 | if (ch=='B') color = kBlack; |
Clemo | 0:7a63bd42cf24 | 529 | else if (ch=='R') color = kRed; |
Clemo | 0:7a63bd42cf24 | 530 | else if (ch=='O') color = kOrange; |
Clemo | 0:7a63bd42cf24 | 531 | else if (ch=='G') color = kGreen; |
Clemo | 0:7a63bd42cf24 | 532 | else if (ch=='x') x -= 1; |
Clemo | 0:7a63bd42cf24 | 533 | else if (ch=='X') x += 1; |
Clemo | 0:7a63bd42cf24 | 534 | else if (ch=='y') y -= 1; |
Clemo | 0:7a63bd42cf24 | 535 | else if (ch=='Y') y += 1; |
Clemo | 0:7a63bd42cf24 | 536 | PC.printf("\tx=%d, y=%d, color=%d\n",x,y,color); |
Clemo | 0:7a63bd42cf24 | 537 | clear_display(); |
Clemo | 0:7a63bd42cf24 | 538 | }*/ |
Clemo | 0:7a63bd42cf24 | 539 | |
Clemo | 0:7a63bd42cf24 | 540 | panel_refresh(&Panel); // Call regularly! |
Clemo | 0:7a63bd42cf24 | 541 | wait(app_data.scroll_speed); // Slow down, but not too much. |
Clemo | 0:7a63bd42cf24 | 542 | } |
Clemo | 0:7a63bd42cf24 | 543 | } |