µracoli Manual Version foo

- µracoli
- User Guide
- Reference Guide

ledps.h
1 /* Copyright (c) 2013 - 2014 Axel Wachtler
2 All rights reserved.
3
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions
6 are met:
7
8 * Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
10 * Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in the
12 documentation and/or other materials provided with the distribution.
13 * Neither the name of the authors nor the names of its contributors
14 may be used to endorse or promote products derived from this software
15 without specific prior written permission.
16
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 POSSIBILITY OF SUCH DAMAGE. */
28
29 #ifndef LEDPS_H
30 #define LEDPS_H
31
41 /* === includes ============================================================ */
42 #include "sensor_defs.h"
43 #include <avr/io.h>
44
45 /* === macros ============================================================== */
47 #define LEDPS_DEBUG (0)
48 #if LEDPS_DEBUG == 1
49 # include "hif.h"
50 # define DBG_LEDPS(...) PRINTF(__VA_ARGS__)
51 #else
52 # define DBG_LEDPS(...)
53 #endif
54
55 #define DO_LED_PS(port, ddr, pin, anode, cathode, ledps_cnt) \
56 do { \
57 ddr |= (_BV(anode) | _BV(cathode)); \
58 port &= ~_BV(anode); \
59 port |= _BV(cathode); \
60 _delay_us(5); \
61 cli(); \
62 ddr &= ~_BV(cathode); \
63 port &= ~_BV(cathode); \
64 ledps_cnt = 0x7fff; \
65 do { \
66 _delay_us(20); \
67 } while ((pin & _BV(cathode)) && --ledps_cnt);\
68 sei(); \
69 ddr |= (_BV(anode) | _BV(cathode));\
70 } while(0)
71
72
73 /* === types =============================================================== */
74
76 typedef struct
77 {
78 sensor_driver_t g;
79 char portid;
80 uint8_t panode;
81 uint8_t pcathode;
82 } ledps_ctx_t;
83
84 /* === prototypes ========================================================== */
85 #ifdef __cplusplus
86 extern "C" {
87 #endif
88 /* === low level functions ================================================== */
89
90
104 static inline uint16_t sample_port(char portid, uint8_t anode_pin, uint8_t cathode_pin);
105 static inline uint16_t sample_port(char portid, uint8_t anode_pin, uint8_t cathode_pin)
106 {
107 uint16_t rv = 0xffff;
108 /* transform portid to upper case letter */
109 if (portid >= 97)
110 {
111 portid -= 32;
112 }
113 switch (portid)
114 {
115 #if defined(LEDPS_PORTA) && defined(PORTA)
116 case 'A':
117 DO_LED_PS(PORTA, DDRA, PINA, anode_pin, cathode_pin, rv);
118 break;
119 #endif
120 #if defined(LEDPS_PORTB) && defined(PORTB)
121 case 'B':
122 DO_LED_PS(PORTB, DDRB, PINB, anode_pin, cathode_pin, rv);
123 break;
124 #endif
125 #if defined(LEDPS_PORTC) && defined(PORTC)
126 case 'C':
127 DO_LED_PS(PORTC, DDRC, PINC, anode_pin, cathode_pin, rv);
128 break;
129 #endif
130 #if defined(LEDPS_PORTD) && defined(PORTD)
131 case 'D':
132 DO_LED_PS(PORTD, DDRD, PIND, anode_pin, cathode_pin, rv);
133 break;
134 #endif
135 #if defined(LEDPS_PORTE) && defined(PORTE)
136 case 'E':
137 DO_LED_PS(PORTE, DDRE, PINE, anode_pin, cathode_pin, rv);
138 break;
139 #endif
140 #if defined(LEDPS_PORTF) && defined(PORTF)
141 case 'F':
142 DO_LED_PS(PORTF, DDRF, PINF, anode_pin, cathode_pin, rv);
143 break;
144 #endif
145 default:
146 rv = 0xaaaa;
147 break;
148 }
149 return rv;
150 }
151
152 /* === high level sensor functions ========================================= */
154 static inline void ledps_trigger(void *pctx, bool one_shot)
155 {
156 }
157
159 static inline uint8_t ledps_get_val(void *pctx, uint8_t *pdata)
160 {
161 sensor_light_t *p = (sensor_light_t *)pdata;
162 ledps_ctx_t *pcfg = (ledps_ctx_t *)pctx;
163 if( p )
164 {
165 p->type = SENSOR_DATA_LIGHT;
166 p->sensor = SENSOR_LEDPS;
167 p->light = sample_port(pcfg->portid, pcfg->panode, pcfg->pcathode);
168 }
169 return sizeof(sensor_light_t);
170 }
171
173 static inline uint8_t ledps_get_raw(void *pctx, uint8_t *pdata)
174 {
175 ledps_ctx_t *pcfg = (ledps_ctx_t*)pctx;
176 uint16_t rv;
177 if (pdata)
178 {
179 rv = sample_port(pcfg->portid, pcfg->panode, pcfg->pcathode);
180 pdata[0] = 0x81;
181 pdata[1] = SENSOR_LEDPS;
182 pdata[2] = rv/256;
183 pdata[3] = rv&255;
184 }
185 return 4;
186 }
187
189 static inline void ledps_sleep(void *pctx)
190 {
191 }
192
199 static inline uint8_t sensor_create_ledps(void *pdata, bool raw,
200 char portid, uint8_t panode, uint8_t pcathode)
201 {
202 uint8_t rv = sizeof(ledps_ctx_t);
203 ledps_ctx_t *pcfg;
204
205 if (pdata != NULL)
206 {
207 /* init generic sensor data */
208 pcfg = (ledps_ctx_t *)pdata;
209 pcfg->g.id = SENSOR_LEDPS;
210 pcfg->g.f_trigger = ledps_trigger;
211 pcfg->g.f_get_val = raw ? ledps_get_raw : ledps_get_val;
212 pcfg->g.f_sleep = ledps_sleep;
213 // ... more functions to setup
214
215 /* init private sensor data */
216 pcfg->portid = portid;
217 pcfg->panode = panode;
218 pcfg->pcathode = pcathode;
219 DBG_LEDPS("ledps: port: %c ano: %d, cath: %d\n",
220 pcfg->portid, pcfg->panode, pcfg->pcathode);
221 /* initialize sensor hardware */
222
223 }
224 return rv;
225 }
226
227 #ifdef __cplusplus
228 } /* extern "C" */
229 #endif
230
231 #endif /* #ifndef LEDPS_H */
ledps_trigger
static void ledps_trigger(void *pctx, bool one_shot)
Definition: ledps.h:154
ledps_get_val
static uint8_t ledps_get_val(void *pctx, uint8_t *pdata)
Definition: ledps.h:159
DO_LED_PS
#define DO_LED_PS(port, ddr, pin, anode, cathode, ledps_cnt)
Definition: ledps.h:55
sensor_light_t
Definition: sensor_defs.h:77
ledps_ctx_t::portid
char portid
Definition: ledps.h:79
ledps_sleep
static void ledps_sleep(void *pctx)
Definition: ledps.h:189
sample_port
static uint16_t sample_port(char portid, uint8_t anode_pin, uint8_t cathode_pin)
sample port Note: The following macros needs to be defined in board_.h
Definition: ledps.h:105
ledps_get_raw
static uint8_t ledps_get_raw(void *pctx, uint8_t *pdata)
Definition: ledps.h:173
ledps_ctx_t
Definition: ledps.h:76
sensor_driver_t
Definition: sensor_defs.h:59
sensor_create_ledps
static uint8_t sensor_create_ledps(void *pdata, bool raw, char portid, uint8_t panode, uint8_t pcathode)
Definition: ledps.h:199