summaryrefslogtreecommitdiff
path: root/digital/io-hub/src/common/output.c
blob: 2e2c604bf75d4a2112a69617dec3d70d8516039b (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
/* output.c */
/* io-hub - Modular Input/Output. {{{
 *
 * Copyright (C) 2012 Nicolas Schodet
 *
 * APBTeam:
 *        Web: http://apbteam.org/
 *      Email: team AT apbteam DOT org
 *
 * 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.
 *
 * }}} */
#include "common.h"
#include "output.h"

#include "simu.host.h"

/** Output context. */
struct output_t
{
    /** If not zero, toggle outputs after this number of update. */
    uint16_t transient_duration;
    /** Mask for transient toggle. */
    uint32_t transient_mask;
};

/** Global context. */
static struct output_t output_global;
#define ctx output_global

void
output_init (void)
{
#define OUTPUT(io, init) do { \
    if (init) IO_SET_ (io); \
    IO_OUTPUT_ (io); \
} while (0); // <- do not copy this code unless you know why!
    OUTPUT_LIST
#undef OUTPUT
}

void
output_set (uint32_t set)
{
#define OUTPUT(io, init) do { \
    if (set & 1ul) IO_SET_ (io); \
    set >>= 1; \
} while (0); // <- do not copy this code unless you know why!
    OUTPUT_LIST
#undef OUTPUT
}

void
output_clear (uint32_t clear)
{
#define OUTPUT(io, init) do { \
    if (clear & 1ul) IO_CLR_ (io); \
    clear >>= 1; \
} while (0); // <- do not copy this code unless you know why!
    OUTPUT_LIST
#undef OUTPUT
}

void
output_toggle (uint32_t toggle)
{
#define OUTPUT(io, init) do { \
    if (toggle & 1ul) IO_TOGGLE_ (io); \
    toggle >>= 1; \
} while (0); // <- do not copy this code unless you know why!
    OUTPUT_LIST
#undef OUTPUT
}

void
output_toggle_transient (uint32_t toggle, uint16_t duration)
{
    output_toggle (toggle);
    ctx.transient_mask = toggle;
    ctx.transient_duration = duration;
}

void
output_update (void)
{
    if (ctx.transient_duration && !--ctx.transient_duration)
	output_toggle (ctx.transient_mask);
}