src/led7.c
author Tero Marttila <terom@paivola.fi>
Thu, 29 Jan 2015 23:11:44 +0200
changeset 107 05707929ff6f
parent 57 ee412c5be8b1
permissions -rw-r--r--
qmsk.web.async: handle HTTPExceptions
57
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
     1
#define LED7_DDR    DDRB
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
     2
#define LED7_PORT   PORTB
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
     3
#define LED7_OE     PORTB1
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
     4
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
     5
// common anode?
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
     6
#define LED7_ANODE  0
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
     7
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
     8
#define LED7_OFFSET 0
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
     9
#define LED7_COUNT  2
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    10
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    11
static const uint8_t LED7SEG_FONT[] = {
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    12
    0b00111111,	// 0
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    13
    0b00000110,	// 1
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    14
    0b01011011,	// 2
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    15
    0b01001111,	// 3
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    16
    0b01100110,	// 4
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    17
    0b01101101,	// 5
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    18
    0b01111101,	// 6
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    19
    0b00000111,	// 7
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    20
    0b01111111,	// 8
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    21
    0b01100111,	// 9
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    22
    0b01110111,	// A
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    23
    0b01111100,	// B
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    24
    0b00111001,	// C
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    25
    0b01011110,	// D
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    26
    0b01111001,	// E
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    27
    0b01110001,	// F
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    28
};
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    29
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    30
enum {
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    31
    LED7SEG_DOT     = 0b10000000,
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    32
};
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    33
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    34
void led7_init (void)
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    35
{
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    36
    sbi(&LED7_PORT, LED7_OE); // disabled (low)
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    37
    sbi(&LED7_DDR, LED7_OE); // out
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    38
}
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    39
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    40
/*
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    41
 * Update external IO.
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    42
 */
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    43
void led7_update_raw (uint8_t *led7, char count)
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    44
{
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    45
    int i;
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    46
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    47
    for (i = 0; i < count; i++) {
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    48
        char raw = LED7SEG_FONT[led7[i]];
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    49
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    50
        spi_tx[LED7_OFFSET + i] = LED7_ANODE ? ~raw  : raw;
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    51
    } 
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    52
}
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    53
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    54
void led7_update_hex (unsigned char c)
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    55
{
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    56
    uint8_t led7[2];
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    57
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    58
    led7[0] = c >> 4;
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    59
    led7[1] = c & 0xF;
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    60
    
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    61
    return led7_update_raw(led7, sizeof(led7));
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    62
}
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    63
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    64
void led7_update (char state)
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    65
{
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    66
    static unsigned char i = 0;
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    67
    
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    68
    if (i) {
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    69
        // enable display
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    70
        cbi(&LED7_PORT, LED7_OE);
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    71
    }
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    72
    
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    73
    led7_update_hex(state);
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    74
 
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    75
    i++;
ee412c5be8b1 hello: split led7, inc/dec commands; read to read in dip state
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    76
}