summaryrefslogtreecommitdiff
path: root/src/common/led.c
blob: e1393cc811e668312e118bcf8ddae1b3752bf444 (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/* 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"
#include "button.h"
#include "power.h"

/** Led display loop overhead estimation. */
#define LED_OVERHEAD_MS (40 * 1e3 / AC_FREQ)
/** Time a led is lit. */
#define LED_UP_MS 0.05
#define LED_DOWN_MS (0.5 - LED_UP_MS - LED_OVERHEAD_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 */
};

uint16_t led_circle[] =
{
    0b0001000000,
    0b0010000000,
    0b0100000000,
    0b1000000000,
    0b0000100000,
    0b0000010000,
    0b0000001000,
    0b0000000100,
    0b0000000010,
    0b0000000001,
};

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, mask;
    portb = PORTB & ~LED_MASK;
    ddrb = DDRB & ~LED_MASK;
    for (i = 0; i < duration; i++)
      {
	for (l = 0, mask = 1; l < UTILS_COUNT (led_tab); l++, mask <<= 1)
	  {
	    /* Turn on if selected. */
	    if (leds & mask)
	      {
		PORTB = portb | led_tab[l].port;
		DDRB = ddrb | led_tab[l].ddr;
		/* RESET pin is weaker, use a longer delay. */
		if (led_tab[l].port & 0b100000)
		    power_delay_ms (2 * LED_UP_MS);
		else
		    power_delay_ms (LED_UP_MS);
	      }
	    else
		power_delay_ms (LED_UP_MS);
	    /* Turn off, leds are too bright. */
	    DDRB = ddrb;
	    PORTB = portb | LED_MASK;
	    power_delay_ms (LED_DOWN_MS);
	    /* Stop now if button is pressed, it disturbs leds. */
	    if (button_pressed ())
		return;
	  }
      }
}

void
led_no_pull_up (void)
{
    PORTB = (PORTB & ~LED_MASK) | IO_BV (BUTTON_IO);
}

static void
led_animate_trace_ccw (uint16_t leds, uint16_t speed)
{
    uint8_t l;
    uint16_t trace = 0;
    for (l = 0; l < UTILS_COUNT (led_tab); l++)
      {
	led_display (trace | led_circle[l], speed);
	trace = trace | (leds & led_circle[l]);
      }
}

void
led_animate (uint16_t leds, uint8_t animation, uint16_t speed)
{
    switch (animation)
      {
      case LED_ANIMATION_TRACE_CCW:
	led_animate_trace_ccw (leds, speed);
	break;
      }
}