summaryrefslogtreecommitdiff
path: root/src/common/led.c
blob: 194e20e887df7fb546651c5eb62fdfccf0b213c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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
/* led.c */
/* binwatch - Tiny binary wristwatch. {{{
 *
 * Copyright (C) 2010 Nicolas Schodet
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * Contact :
 *        Web: http://ni.fr.eu.org/
 *      Email: <nico at ni.fr.eu.org>
 * }}} */
#include "common.h"
#include "modules/utils/utils.h"
#include "io.h"

#include "led.h"

/** Time a led is lit. */
#define LED_UP_MS 0.05
#define LED_DOWN_MS (0.5 - LED_UP_MS)

/** Mask of used bits. */
#define LED_MASK 0b111010

struct led_t
{
    uint8_t ddr;
    uint8_t port;
};

struct led_t led_tab[] =
{
    /*  432 1     432 1 */
    { 0b001010, 0b000010 }, /* M0 */
    { 0b001010, 0b001000 }, /* M1 */
    { 0b010010, 0b000010 }, /* M2 */
    { 0b010010, 0b010000 }, /* M3 */
    { 0b100010, 0b000010 }, /* M4 */
    { 0b100010, 0b100000 }, /* M5 */
    { 0b011000, 0b001000 }, /* H0 */
    { 0b011000, 0b010000 }, /* H1 */
    { 0b101000, 0b001000 }, /* H2 */
    { 0b101000, 0b100000 }, /* H3 */
};

void
led_init (void)
{
    /* Activate pull-up. */
    PORTB |= LED_MASK;
}

void
led_display (uint16_t leds, uint16_t duration)
{
    uint8_t portb, ddrb;
    uint8_t l;
    uint16_t i;
    portb = PORTB & ~LED_MASK;
    ddrb = DDRB & ~LED_MASK;
    for (i = 0; i < duration; i++)
      {
	for (l = 0; l < UTILS_COUNT (led_tab); l++)
	  {
	    /* Turn on if selected. */
	    if (leds & (1u << l))
	      {
		PORTB = portb | led_tab[l].port;
		DDRB = ddrb | led_tab[l].ddr;
	      }
	    utils_delay_ms (LED_UP_MS);
	    /* Turn off, leds are too bright. */
	    DDRB = ddrb;
	    PORTB = portb | LED_MASK;
	    utils_delay_ms (LED_DOWN_MS);
	  }
      }
}