SHARE
TWEET

LED_Digits

a guest Apr 13th, 2015 259 Never
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #define DIGIT_HEIGHT   3
  6. #define DIGIT_WIDTH    3
  7. #define CONSOLE_WIDTH  80
  8. #define MAX_LINE       (CONSOLE_WIDTH-CONSOLE_WIDTH%DIGIT_WIDTH)
  9.  
  10. #define DEBUG
  11.  
  12.  
  13. typedef struct {
  14.     char top[MAX_LINE + sizeof("")];
  15.     char mid[MAX_LINE + sizeof("")];
  16.     char btm[MAX_LINE + sizeof("")];
  17.    
  18.     unsigned int offset;
  19. } LED_Display;
  20.  
  21. LED_Display *new_LED_Display() {
  22.     LED_Display *display = malloc(sizeof(LED_Display));
  23.     display->top[0] = '\0';
  24.     display->mid[0] = '\0';
  25.     display->btm[0] = '\0';
  26.     display->offset = 0;
  27.    
  28.     return display;
  29. }
  30.  
  31. /* Generates "graphical" digits for an LED_Display structure.
  32.  * Parameter 1 is a string containing ascii digits.
  33.  * If parameter 2 is NULL, a new *LED_Display is created, otherwise digits will
  34.  * be appended to an existing LED_Display* passed as parameter 2.
  35.  *
  36.  * Returns the generated LED_Display */
  37. LED_Display *generate_LED_digits(char *digit_string, LED_Display *display) {
  38.     if (display == NULL) {
  39.         display = new_LED_Display();
  40.     }
  41.    
  42.     enum {
  43.         TOP_GRAPHIC,
  44.         MID_GRAPHIC,
  45.         BTM_GRAPHIC
  46.     };
  47.     const char *graphic[][DIGIT_HEIGHT] = {
  48.         {
  49.             " _ ",
  50.             "| |",
  51.             "|_|"
  52.         },{
  53.             "   ",
  54.             " | ",
  55.             " | "
  56.         },{
  57.             " _ ",
  58.             " _|",
  59.             "|_ "
  60.         },{
  61.             " _ ",
  62.             " _|",
  63.             " _|"
  64.         },{
  65.             "   ",
  66.             "|_|",
  67.             "  |"
  68.         },{
  69.             " _ ",
  70.             "|_ ",
  71.             " _|"
  72.         },{
  73.             " _ ",
  74.             "|_ ",
  75.             "|_|"
  76.         },{
  77.             " _ ",
  78.             "  |",
  79.             "  |"
  80.         },{
  81.             " _ ",
  82.             "|_|",
  83.             "|_|"
  84.         },{
  85.             " _ ",
  86.             "|_|",
  87.             " _|"
  88.         }
  89.     };
  90.    
  91.     for (int i = 0; digit_string[i] != '\0'; ++i) {
  92.         if (display->offset < MAX_LINE && digit_string[i] >= '0' && digit_string[i] <= '9') {
  93.             int digit_select = digit_string[i]-'0';
  94.            
  95.             strncat(display->top,graphic[digit_select][TOP_GRAPHIC],(MAX_LINE - display->offset));
  96.             strncat(display->mid,graphic[digit_select][MID_GRAPHIC],(MAX_LINE - display->offset));
  97.             strncat(display->btm,graphic[digit_select][BTM_GRAPHIC],(MAX_LINE - display->offset));
  98.            
  99.             display->offset += DIGIT_WIDTH;
  100.         }
  101.     }
  102.    
  103.     #ifdef DEBUG
  104.     printf("DBG DISPLAY PRINT TEST: {\n%s\n%s\n%s\n}\n",display->top,display->mid,display->btm);
  105.     #endif
  106.  
  107.     return display;
  108. }
  109.  
  110. int main(int argc, char *argv[]) {
  111.     if (argc>1) {
  112.         LED_Display *arg_digits = new_LED_Display();
  113.         for (int i = 1; i < argc; ++i) {
  114.             generate_LED_digits(argv[i],arg_digits);
  115.         }
  116.         free(arg_digits);
  117.     }
  118.     #ifdef DEBUG
  119.     else {
  120.         char dirty_string[256];
  121.         for (int i = 0; i < 256; ++i) {
  122.             dirty_string[i] = 1+i;
  123.         }
  124.         free(generate_LED_digits(dirty_string, NULL)); // prints "0123456789"
  125.  
  126.         LED_Display *appendtest = generate_LED_digits("12 (None of this is printed) 34", NULL);
  127.         free(generate_LED_digits("56789012345678901234567",appendtest)); // last character should not be printed
  128.     }
  129.     #endif
  130.    
  131.     return EXIT_SUCCESS;
  132. }
  133.  
  134.  
  135. #undef CONSOLE_WIDTH
  136. #undef DIGIT_WIDTH
  137. #undef DIGIT_HEIGHT
  138. #undef MAX_LINE
RAW Paste Data
Top