int main(int argc, char **argv) { int result,i; int i2c_fd; i2c_fd=init_i2c("/dev/i2c-1"); if (i2c_fd < 0) { fprintf(stderr,"Error opening device!\n"); return -1; } /* Init display */ if (init_display(i2c_fd,HT16K33_ADDRESS0,10)) { fprintf(stderr,"Error opening display\n"); return -1; } result=load_font("tbfont.tb1"); while(1) { for(i=32;i<127;i++) { result=put_letter(i,i2c_fd); usleep(500000); } } return result; }
static void draw_date(struct tm* t, GContext* ctx) { static char txt[] = "XXX 00XXX"; strftime(txt, sizeof(txt), "%a %e%b", t); int width = 0, x; const Character *ch; char* c = txt; while (*c != '\0') { if (*c >= 'A' && *c <= 'Z') { ch = LETTER_TABLE[*c - 65]; } else if (*c >= 'a' && *c <= 'z') { ch = LETTER_TABLE[*c - 97]; } else if (*c >= '0' && *c <= '9') { ch = NUMBER_TABLE[*c - 48]; } else if (*c == ' ') { width += WEEK_DATE_SPACING; c++; continue; } else { continue; } width += ch->width + DATE_CHAR_SPACING; c++; } x = (SCREEN_WIDTH - width + DATE_CHAR_SPACING) / 2; c = txt; while (*c != '\0') { if (*c >= 'A' && *c <= 'Z') { ch = LETTER_TABLE[*c - 65]; } else if (*c >= 'a' && *c <= 'z') { ch = LETTER_TABLE[*c - 97]; } else if (*c >= '0' && *c <= '9') { ch = NUMBER_TABLE[*c - 48]; } else if (*c == ' ') { x += WEEK_DATE_SPACING; c++; continue; } else { continue; } put_letter(ch, ctx, x, DATE_TOP_MARGIN); x += ch->width + DATE_CHAR_SPACING; c++; } }
void render_screen(int fd, std::string str) { // Clear the screen memset(frontstore, 0, sizeof(frontstore)); auto render_string = [](std::string str, int start_line) { // The start line, and start x coordinate int line = start_line; int char_x = 1; // Run the entire string, char by char for(char& c : str) { // Find the pixel letter corresponding to the char Letter l = find_letter(c); // If we overflow this line, by adding it, do a line break if(char_x + l.letter_width >= YSIZE) { char_x = 1; line++; // If we overflow the screen, skip the rest of the string if(line > 2) { break; } } // Calculate our y coord, based upon the line, we're in int char_y = 0 + line * (LETTER_HEIGHT + 1); // Do the actual printing of the character put_letter(char_y, char_x, l); // Move the size of this letter, and 1 character, // before rendering the next character char_x += l.letter_width; char_x += 1; } // People who start on the line after us return line+1; }; // Write the new string int end_line = render_string(str, 0); // If the new string did not take up all the space, // lets reload some of the old stuff, and draw that for(std::string old : old_lines) { // Only draw, if we're inside the screen if(end_line > 2) { break; } end_line = render_string(old, end_line); } // The string we just rendered, is now an old line old_lines.push_front(str); // Garbage collect old_lines while(old_lines.size() > 3) { old_lines.pop_back(); } // Render! scr_frontmap(fd); }