Pastebin PRO Accounts EASTER SPECIAL! For a limited time only get 40% discount on a LIFETIME PRO account! Offer Ends Soon!
SHARE
TWEET
LED_Digits
a guest
Apr 13th, 2015
259
Never
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define DIGIT_HEIGHT 3
- #define DIGIT_WIDTH 3
- #define CONSOLE_WIDTH 80
- #define MAX_LINE (CONSOLE_WIDTH-CONSOLE_WIDTH%DIGIT_WIDTH)
- #define DEBUG
- typedef struct {
- char top[MAX_LINE + sizeof("")];
- char mid[MAX_LINE + sizeof("")];
- char btm[MAX_LINE + sizeof("")];
- unsigned int offset;
- } LED_Display;
- LED_Display *new_LED_Display() {
- LED_Display *display = malloc(sizeof(LED_Display));
- display->top[0] = '\0';
- display->mid[0] = '\0';
- display->btm[0] = '\0';
- display->offset = 0;
- return display;
- }
- /* Generates "graphical" digits for an LED_Display structure.
- * Parameter 1 is a string containing ascii digits.
- * If parameter 2 is NULL, a new *LED_Display is created, otherwise digits will
- * be appended to an existing LED_Display* passed as parameter 2.
- *
- * Returns the generated LED_Display */
- LED_Display *generate_LED_digits(char *digit_string, LED_Display *display) {
- if (display == NULL) {
- display = new_LED_Display();
- }
- enum {
- TOP_GRAPHIC,
- MID_GRAPHIC,
- BTM_GRAPHIC
- };
- const char *graphic[][DIGIT_HEIGHT] = {
- {
- " _ ",
- "| |",
- "|_|"
- },{
- " ",
- " | ",
- " | "
- },{
- " _ ",
- " _|",
- "|_ "
- },{
- " _ ",
- " _|",
- " _|"
- },{
- " ",
- "|_|",
- " |"
- },{
- " _ ",
- "|_ ",
- " _|"
- },{
- " _ ",
- "|_ ",
- "|_|"
- },{
- " _ ",
- " |",
- " |"
- },{
- " _ ",
- "|_|",
- "|_|"
- },{
- " _ ",
- "|_|",
- " _|"
- }
- };
- for (int i = 0; digit_string[i] != '\0'; ++i) {
- if (display->offset < MAX_LINE && digit_string[i] >= '0' && digit_string[i] <= '9') {
- int digit_select = digit_string[i]-'0';
- strncat(display->top,graphic[digit_select][TOP_GRAPHIC],(MAX_LINE - display->offset));
- strncat(display->mid,graphic[digit_select][MID_GRAPHIC],(MAX_LINE - display->offset));
- strncat(display->btm,graphic[digit_select][BTM_GRAPHIC],(MAX_LINE - display->offset));
- display->offset += DIGIT_WIDTH;
- }
- }
- #ifdef DEBUG
- printf("DBG DISPLAY PRINT TEST: {\n%s\n%s\n%s\n}\n",display->top,display->mid,display->btm);
- #endif
- return display;
- }
- int main(int argc, char *argv[]) {
- if (argc>1) {
- LED_Display *arg_digits = new_LED_Display();
- for (int i = 1; i < argc; ++i) {
- generate_LED_digits(argv[i],arg_digits);
- }
- free(arg_digits);
- }
- #ifdef DEBUG
- else {
- char dirty_string[256];
- for (int i = 0; i < 256; ++i) {
- dirty_string[i] = 1+i;
- }
- free(generate_LED_digits(dirty_string, NULL)); // prints "0123456789"
- LED_Display *appendtest = generate_LED_digits("12 (None of this is printed) 34", NULL);
- free(generate_LED_digits("56789012345678901234567",appendtest)); // last character should not be printed
- }
- #endif
- return EXIT_SUCCESS;
- }
- #undef CONSOLE_WIDTH
- #undef DIGIT_WIDTH
- #undef DIGIT_HEIGHT
- #undef MAX_LINE
RAW Paste Data
