-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathLedHwIntf.c
122 lines (109 loc) · 3.3 KB
/
LedHwIntf.c
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
#include "espressif/esp_common.h"
#include <selib.h>
#include <ledctrl.h>
#define D0 16 // Bultin LED on Geekcreit ESP8266
#define D6 12 // Wire LED between D6 and ground
#define D7 13 // Wire LED between D7 and ground
#define D8 15 // Wire LED between D8 and ground
static const LedInfo ledInfo[] = {
{
"Bultin LED",
LedColor_blue,
1
},
{
"LED D6",
LedColor_red,
2
},
{
"LED D7",
LedColor_red,
3
},
{
"LED D8",
LedColor_red,
4
}
};
#define LEDSLEN (sizeof(ledInfo) / sizeof(ledInfo[0]))
static int leds[LEDSLEN];
/* Returns the LED on/off state for led with ID 'ledId'.
*/
int getLedState(int ledId)
{
if(ledId >= 1 && ledId <= LEDSLEN)
return leds[ledId-1];
return 0;
}
/*
Return an array of LedInfo (struct). Each element in the array
provides information for one LED. The 'len' argument must be set by
function getLedInfo. The out argument 'en' specifies the length of
the returned array, that is, number of LEDs in the device. Each LED
has a name, color, and ID. The ID, which provides information about
which LED to turn on/off, is used by control messages sent between
device code and UI clients. The IDs for a four LED device can for
example be 1,2,3,4.
*/
const LedInfo* getLedInfo(int* len)
{
*len = LEDSLEN;
return ledInfo;
}
/* Command sent by UI client to turn LED with ID on or off. This
function must set the LED to on if 'on' is TRUE and off if 'on' is FALSE.
The parameter 'LedId' is the value of the field 'id' in the
corresponding 'LedInfo' structure. Code might be required to match
the 'id' value to the physical LED and entry in the LED array. In
this example, 'id' field is numbered from 1 through 4, with 'id' 1
referring to the first entry in the array of LedInfo
structures. The expression (LedId - 1), results in the correct
index.
*/
int _setLed(int ledId, int on)
{
if(ledId >= 1 && ledId <= LEDSLEN)
{
leds[ledId-1] = on;
switch(ledId)
{
case 1: gpio_write(D0, on ? 0 : 1); break;
case 2: gpio_write(D6, on ? 1 : 0); break;
case 3: gpio_write(D7, on ? 1 : 0); break;
case 4: gpio_write(D8, on ? 1 : 0); break;
}
return 0;
}
return -1;
}
int setLed(int ledId, int on)
{
printf("Set led %d %s\n", ledId, on ? "on" : "off");
return _setLed(ledId, on);
}
/*
An optional function that enables LEDs to be set directly by the
device. This function is typically used by devices that include one
or more buttons. A button click may for example turn on a specific
LED. The function is called at intervals (polled) by the LED device
code. The function may for example detect a button click and return
the information to the caller. Arguments 'ledId' and 'on' are out
arguments, where 'ledId' is set to the LED ID and 'on' is set to
TRUE for on and FALSE for off. The function must return TRUE (a non
zero value) if the LED is to be set on/off and zero on no
change. Create an empty function returning zero if you do not plan
on implementing this feature.
*/
int setLedFromDevice(int* ledId, int* on)
{
return FALSE;
}
void initLED(void)
{
gpio_enable(D0, GPIO_OUTPUT);
gpio_enable(D6, GPIO_OUTPUT);
gpio_enable(D7, GPIO_OUTPUT);
gpio_enable(D8, GPIO_OUTPUT);
}