src/hello-lkm.c
changeset 60 b9648067e9d7
parent 59 7090f61e5e17
child 61 a960cf5981f7
equal deleted inserted replaced
59:7090f61e5e17 60:b9648067e9d7
    59     0b01111001,	// E
    59     0b01111001,	// E
    60     0b01110001,	// F
    60     0b01110001,	// F
    61 };
    61 };
    62 
    62 
    63 enum {
    63 enum {
    64     LKM_DISPLAY_DOT     = 0b10000000,
    64     LKM_DISPLAY_DECIMAL = 0b10000000,
    65 };
    65 };
    66 
    66 
       
    67 /*
       
    68  * XXX: RED/GREEN somehow swapped vs reference; bug in our write code?
       
    69  */
    67 enum {
    70 enum {
    68     LKM_LED_OFF     = 0b00,
    71     LKM_LED_OFF     = 0b00,
    69     LKM_LED_RED     = 0b01,
    72     LKM_LED_GREEN   = 0b01,
    70     LKM_LED_GREEN   = 0b10,
    73     LKM_LED_RED     = 0b10,
    71     LKM_LED_ORANGE  = 0b11,
    74     LKM_LED_ORANGE  = 0b11,
    72 };
    75 };
    73 
    76 
    74 /*
    77 /*
    75  * Button bitfields.
    78  * Button bitfields.
   291 static inline void lkm_display_hex (byte display, byte hex)
   294 static inline void lkm_display_hex (byte display, byte hex)
   292 {
   295 {
   293     lkm_display(display, LKM_DISPLAY_FONT[hex]);
   296     lkm_display(display, LKM_DISPLAY_FONT[hex]);
   294 }
   297 }
   295 
   298 
       
   299 static inline void lkm_display_dec (byte display, byte dec, byte decimal)
       
   300 {
       
   301     byte raw;
       
   302 
       
   303     if (dec < 10)
       
   304         raw = LKM_DISPLAY_FONT[dec];
       
   305     else
       
   306         raw = 0x00;
       
   307 
       
   308     if (decimal)
       
   309         raw |= LKM_DISPLAY_DECIMAL;
       
   310 
       
   311     lkm_display(display, raw);
       
   312 }
       
   313 
   296 /*
   314 /*
   297  * Read the 8-bit key states
   315  * Read the 8-bit key states
   298  */
   316  */
   299 byte lkm_buttons ()
   317 byte lkm_buttons ()
   300 {
   318 {
   330 
   348 
   331     return k3;
   349     return k3;
   332 }
   350 }
   333 
   351 
   334 /*
   352 /*
       
   353  * Set 8-bit hexadecimal output on displays n..n+1
       
   354  */
       
   355 void lkm_display_hh (byte display, byte hex)
       
   356 {
       
   357     lkm_display_hex(display + 0, ((hex >> 4) & 0xF));
       
   358     lkm_display_hex(display + 1, ((hex >> 0) & 0xF));
       
   359 }
       
   360 
       
   361 /*
   335  * Set 16-bit hexadecimal output on displays 4..7
   362  * Set 16-bit hexadecimal output on displays 4..7
   336  */
   363  */
   337 void lkm_display_hex16 (short hex)
   364 void lkm_display_hhhh (short hex)
   338 {
   365 {
   339     lkm_display_hex(7, ((hex >> 0) & 0xF));
   366     lkm_display_hex(7, ((hex >> 0) & 0xF));
   340     lkm_display_hex(6, ((hex >> 4) & 0xF));
   367     lkm_display_hex(6, ((hex >> 4) & 0xF));
   341     lkm_display_hex(5, ((hex >> 8) & 0xF));
   368     lkm_display_hex(5, ((hex >> 8) & 0xF));
   342     lkm_display_hex(4, ((hex >> 12) & 0xF));
   369     lkm_display_hex(4, ((hex >> 12) & 0xF));
   343 }
   370 }
   344 
   371 
       
   372 /*
       
   373  * Set 2-digit decimal output on displays n..n+1
       
   374  */
       
   375 void lkm_display_dd (byte display, byte dec)
       
   376 {
       
   377     byte d = dec / 10;
       
   378     byte u = dec % 10;
       
   379 
       
   380     byte c = d / 10;
       
   381     d = d % 10;
       
   382 
       
   383     lkm_display_dec(display + 0, d, c > 1);
       
   384     lkm_display_dec(display + 1, u, c > 0);
       
   385 }
       
   386 
   345 // debug
   387 // debug
   346 #define DEBUG_DDR   DDRB
   388 #define DEBUG_DDR   DDRB
   347 #define DEBUG_PORT  PORTB
   389 #define DEBUG_PORT  PORTB
   348 #define DEBUG_LED   0
   390 #define DEBUG_LED   0
   349 
   391 
   359 
   401 
   360     // setup display
   402     // setup display
   361     lkm_clear();
   403     lkm_clear();
   362     lkm_control(LKM_CONTROL_DISPLAY_ON, LKM_CONTROL_INTENSITY_MAX);
   404     lkm_control(LKM_CONTROL_DISPLAY_ON, LKM_CONTROL_INTENSITY_MAX);
   363 
   405 
       
   406 /*
       
   407     // test
       
   408     lkm_led(0, LKM_LED_OFF);
       
   409     lkm_led(1, LKM_LED_RED);
       
   410     lkm_led(2, LKM_LED_GREEN);
       
   411     lkm_led(3, LKM_LED_ORANGE);
       
   412 
       
   413     return 0;
       
   414 */
       
   415 
   364     // start
   416     // start
   365     byte state[8] = { };
   417     byte channels[4] = { };
   366     char i;
   418     char c;
   367         
   419         
   368     while (true) {
   420     while (true) {
   369         // scan input
   421         // scan input
   370         byte buttons = lkm_buttons();
   422         byte buttons = lkm_buttons();
   371         
   423         
   372         for (i = 0; i < 8; i++) {
   424         for (c = 0; c < 4; c++) {
   373             if (!(buttons & (1 << i))) {
   425             if (buttons & 0b1) {
   374                 continue;
   426                 channels[c]++;
   375             } else if (state[i] >= 0xF) {
   427                 lkm_led(c * 2 + 0, LKM_LED_GREEN);
   376                 state[i] = 0;
   428                 lkm_led(c * 2 + 1, LKM_LED_OFF);
       
   429             } else if (buttons & 0b10) {
       
   430                 channels[c]--;
       
   431                 lkm_led(c * 2 + 0, LKM_LED_OFF);
       
   432                 lkm_led(c * 2 + 1, LKM_LED_RED);
   377             } else {
   433             } else {
   378                 state[i]++;
   434                 lkm_led(c * 2 + 0, LKM_LED_OFF);
       
   435                 lkm_led(c * 2 + 1, LKM_LED_OFF);
   379             }
   436             }
   380 
   437             
   381             lkm_display_hex(i, state[i]);
   438             buttons >>= 2;
   382             lkm_led(i, state[i]);
   439             
       
   440             lkm_display_hh(c * 2, channels[c]);
   383             
   441             
   384             xbi(&DEBUG_PORT, DEBUG_LED);
   442             xbi(&DEBUG_PORT, DEBUG_LED);
   385         }
   443         }
   386 
   444 
   387         timer_sleep(16000);
   445         timer_sleep(1000);
   388     }
   446     }
   389 }
   447 }