/*************************************************************************** * __________ __ ___. * Open \______ \ ____ ____ | | _\_ |__ _______ ___ * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ * \/ \/ \/ \/ \/ * $Id: matrix.c,v 1.1 2005/06/24 16:22:58 rasher Exp $ * * Copyright (C) 1999 Chris Allegretta * Copyright (C) 2005 Alastair S - ported to podzilla * Copyright (C) 2005 Jonas Häggqvist - ported to rockbox * * * All files in this archive are subject to the GNU General Public License. * See the file COPYING in the source tree root for full license agreement. * * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY * KIND, either express or implied. * ****************************************************************************/ /* * TODO: * - The font is a bit large, create smaller one * - For colour/greyscale displays, the font from the xscreensaver xmatrix * should be converted and used * BUGS: * - The animation "dies" after a few seconds, not sure why. Works in sim. */ #include "plugin.h" #include "matrixfont1bit.h" #define COLS LCD_WIDTH/COL_W #define ROWS LCD_HEIGHT/COL_H #define BLUE_PILL BUTTON_OFF #define SLEEP HZ/15 /* Codec api pointer */ static struct plugin_api* rb; /* Each position is of this type */ typedef struct cmatrix { int val; int bold; } cmatrix; static cmatrix matrix[ROWS][COLS]; static int length[COLS]; static int spaces[COLS]; static int updates[COLS]; static void matrix_init(void) { int i,j; /* Seed rand */ rb->srand(*rb->current_tick); /* Make the matrix */ for (i = 0; i <= ROWS; i++) { for (j = 0; j <= COLS - 1; j++ ) { matrix[i][j].val = -1; matrix[i][j].bold = 0; } } for (j = 0; j <= COLS - 1; j++) { /* Set up spaces[] array of how many spaces to skip */ spaces[j] = rb->rand() % ROWS + 1; /* And length of the stream */ length[j] = rb->rand() % (ROWS - 3) + 3; /* Sentinel value for creation of new objects */ matrix[1][j].val = 129; /* And set updates[] array for update speed. */ updates[j] = rb->rand() % 3 + 1; } } static void matrix_blit_char(const int row, const int col, int cha) { if (cha == 129) cha = 0; /* put char cha on screen */ rb->lcd_bitmap(matrix_code_font[cha], col*COL_W, row*COL_H, COL_W, COL_H); if (matrix[row][col].bold == 1) { rb->lcd_bitmap(matrix_code_font[cha], col*COL_W, row*COL_H+1, COL_W, COL_H); } } static void matrix_loop(void) { int i, j = 0, y, z, firstcoldone = 0; static int count = 0; /* GR_COLOR fg; */ count++; if (count > 4) count = 1; for (j = 0; j <= COLS - 1; j++) { if (count > updates[j]) { /* New style scrolling */ if (matrix[0][j].val == -1 && matrix[1][j].val == 129 && spaces[j] > 0) { matrix[0][j].val = -1; spaces[j]--; } else if (matrix[0][j].val == -1 && matrix[1][j].val == 129){ length[j] = rb->rand() % (ROWS - 3) + 3; matrix[0][j].val = rb->rand() % (MAXCHARS-1) + 1; if (rb->rand() % 2 == 1) matrix[0][j].bold = 2; spaces[j] = rb->rand() % ROWS + 1; } i = 0; y = 0; firstcoldone = 0; while (i <= ROWS) { /* Skip over spaces */ while (i <= ROWS && (matrix[i][j].val == 129 || matrix[i][j].val == -1)) i++; if (i > ROWS) break; /* Go to the head of this collumn */ z = i; y = 0; while (i <= ROWS && (matrix[i][j].val != 129 && matrix[i][j].val != -1)) { i++; y++; } if (i > ROWS) { matrix[z][j].val = 129; matrix[ROWS][j].bold = 1; matrix_blit_char(z - 1, j, matrix[z][j].val); continue; } matrix[i][j].val = rb->rand() % (MAXCHARS-1) + 1; if (matrix[i - 1][j].bold == 2) { matrix[i - 1][j].bold = 1; matrix[i][j].bold = 2; } /* If we're at the top of the collumn and it's reached its * full length (about to start moving down), we do this * to get it moving. This is also how we keep segments * not already growing from growing accidentally => */ if (y > length[j] || firstcoldone) { matrix[z][j].val = 129; matrix[0][j].val = -1; } firstcoldone = 1; i++; } for (i = 1; i <= ROWS; i++) { if (matrix[i][j].val == 0 || matrix[i][j].bold == 2) { /* fg = inverted ? BLACK : WHITE; GrSetGCForeground(matrix_gc, fg); */ if (matrix[i][j].val == 0) matrix_blit_char(i - 1, j, 20); else matrix_blit_char(i - 1, j, matrix[i][j].val); } else { /* if (photo) fg = inverted ? GR_RGB(0, rb->rand() % 35 + 220, 0) : GR_RGB(0, rb->rand() % 100 + 120, 0); else fg = inverted ? LTGRAY : GRAY; GrSetGCForeground(matrix_gc, fg); if (matrix[i][j].val % 2 == 0) { if (photo) fg = inverted ? GR_RGB(0, rb->rand() % 35 + 220, 0) : GR_RGB(0, rb->rand() % 100 + 120, 0); else fg = inverted ? GRAY : LTGRAY; GrSetGCForeground(matrix_gc, fg); } */ if (matrix[i][j].val == 1) matrix_blit_char(i - 1, j, 2); else if (matrix[i][j].val == -1) matrix_blit_char(i - 1, j, 129); else matrix_blit_char(i - 1, j, matrix[i][j].val); } } } } } enum plugin_status plugin_start(struct plugin_api* api, void* parameter) { int button; TEST_PLUGIN_API(api); (void)parameter; rb = api; rb->lcd_clear_display(); // rb->logf("matrix started"); // rb->logf("grid: %dx%d ", COLS, ROWS); // rb->logf("chars: %dx%d", COL_W, COL_H); matrix_init(); while (1) { matrix_loop(); rb->lcd_update(); rb->sleep(SLEEP); button = rb->button_get(false); switch(button) { case BLUE_PILL: // rb->logf("matrix stopped"); return PLUGIN_OK; break; default: if (rb->default_event_handler(button) == SYS_USB_CONNECTED) { return PLUGIN_USB_CONNECTED; } break; } } return PLUGIN_OK; }