Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 744 lines (635 sloc) 25.738 kb
9c0edcc @jcw get rid of svn $Id keyword & update email address
authored
1 // 2009-02-13 <jc@wippler.nl> http://opensource.org/licenses/mit-license.php
fa906da @jcw files imported
authored
2
3 #ifndef Ports_h
4 #define Ports_h
5
f57da01 @jcw lots of little docstrings added
authored
6 /// @file
7 /// Ports library definitions.
8
17fd7ea @jcw adjust for latest revision of the arduino-tiny project
authored
9 #if ARDUINO >= 100
1d38469 Detect Arduino environment version to make library compatible with 0022 ...
unknown authored
10 #include <Arduino.h> // Arduino 1.0
11 #else
98fcac0 @CapnBry Fix compilation under Linux for Arduino < 100
CapnBry authored
12 #include <WProgram.h> // Arduino 0022
1d38469 Detect Arduino environment version to make library compatible with 0022 ...
unknown authored
13 #endif
fa906da @jcw files imported
authored
14 #include <stdint.h>
15 #include <avr/pgmspace.h>
681769e @jcw change defaults for convenient use without jumpers
authored
16 //#include <util/delay.h>
fa906da @jcw files imported
authored
17
5049164 @jcw add new syncRecv sketch
authored
18 // tweak this to switch ATtiny84 etc to new Arduino 1.0+ conventions
19 // see http://arduino.cc/forum/index.php/topic,51984.msg371307.html#msg371307
17fd7ea @jcw adjust for latest revision of the arduino-tiny project
authored
20 // and http://forum.jeelabs.net/node/1567
21 #if ARDUINO >= 100
3e08ca2 @jcw add the "tiny50hz" sample sketch for ATtiny85
authored
22 #define WRITE_RESULT size_t
23 #else
24 #define WRITE_RESULT void
25 #endif
26
f57da01 @jcw lots of little docstrings added
authored
27 /// Interface for JeeNode Ports - see the wiki docs for
28 /// [JeeNodes](http://jeelabs.net/projects/hardware/wiki/JeeNode) and
29 /// [pinouts](http://jeelabs.net/projects/hardware/wiki/Pinouts).
9e7f658 @jcw a bit more docs
authored
30 /// The Ports class is a thin wrapper around the Arduino's digitalRead(),
31 /// digitalWrite(), analogRead(), etc. functions. It was designed to simplify
32 /// the use of the four standard port headers on JeeNodes.
fa906da @jcw files imported
authored
33 class Port {
34 protected:
f57da01 @jcw lots of little docstrings added
authored
35 /// The port number is a small integer mathing the hardware port used.
36 /// Port 0 is special, it designates the I2C hardware pins on a JeeNode.
fa906da @jcw files imported
authored
37 uint8_t portNum;
38
8d132bf @jcw add support for ATtiny45 (thx Fredrik Olofsson)
authored
39 #if defined(__AVR_ATtiny85__) || defined(__AVR_ATtiny45__)
f57da01 @jcw lots of little docstrings added
authored
40 /// @return Arduino digital pin number of a Port's D pin (uint8_t).
3e08ca2 @jcw add the "tiny50hz" sample sketch for ATtiny85
authored
41 inline uint8_t digiPin() const
32fe56a @jcw small tweaks, PB0/PB1 swap for Ports use
authored
42 { return 0; }
f57da01 @jcw lots of little docstrings added
authored
43 /// @return Arduino digital pin number of a Port's A pin (uint8_t).
3e08ca2 @jcw add the "tiny50hz" sample sketch for ATtiny85
authored
44 inline uint8_t digiPin2() const
45 { return 2; }
f57da01 @jcw lots of little docstrings added
authored
46 /// @return Arduino digital pin number of the I pin on all Ports (uint8_t).
3e08ca2 @jcw add the "tiny50hz" sample sketch for ATtiny85
authored
47 static uint8_t digiPin3()
48 { return 1; }
f57da01 @jcw lots of little docstrings added
authored
49 /// @return Arduino analog pin number of a Port's A pin (uint8_t).
32fe56a @jcw small tweaks, PB0/PB1 swap for Ports use
authored
50 inline uint8_t anaPin() const
51 { return 0; }
7707139 @jcw fix port pin assignments for the JeeNode Micro (!)
authored
52 #elif defined(__AVR_ATtiny84__)
53 /// @return Arduino digital pin number of a Port's D pin (uint8_t).
54 inline uint8_t digiPin() const
55 { return 12 - 2 * portNum; }
56 /// @return Arduino digital pin number of a Port's A pin (uint8_t).
57 inline uint8_t digiPin2() const
58 { return 11 - 2 * portNum; }
59 /// @return Arduino digital pin number of the I pin on all Ports (uint8_t).
60 static uint8_t digiPin3()
61 { return 3; }
62 /// @return Arduino analog pin number of a Port's A pin (uint8_t).
63 inline uint8_t anaPin() const
64 { return 11 - 2 * portNum; }
3e08ca2 @jcw add the "tiny50hz" sample sketch for ATtiny85
authored
65 #else
f57da01 @jcw lots of little docstrings added
authored
66 /// @return Arduino digital pin number of a Port's D pin (uint8_t).
fa906da @jcw files imported
authored
67 inline uint8_t digiPin() const
68 { return portNum ? portNum + 3 : 18; }
f57da01 @jcw lots of little docstrings added
authored
69 /// @return Arduino digital pin number of a Port's A pin (uint8_t).
fa906da @jcw files imported
authored
70 inline uint8_t digiPin2() const
71 { return portNum ? portNum + 13 : 19; }
f57da01 @jcw lots of little docstrings added
authored
72 /// @return Arduino digital pin number of the I pin on all Ports (uint8_t).
3e08ca2 @jcw add the "tiny50hz" sample sketch for ATtiny85
authored
73 static uint8_t digiPin3()
74 { return 3; }
f57da01 @jcw lots of little docstrings added
authored
75 /// @return Arduino analog pin number of a Port's A pin (uint8_t).
fa906da @jcw files imported
authored
76 inline uint8_t anaPin() const
77 { return portNum - 1; }
3e08ca2 @jcw add the "tiny50hz" sample sketch for ATtiny85
authored
78 #endif
79
fa906da @jcw files imported
authored
80 public:
1235cd8 @Vliegendehuiskat Made Ports.h Documentation more compact and added some documentation to ...
Vliegendehuiskat authored
81 ///Contructor for a Port.
fa906da @jcw files imported
authored
82 inline Port (uint8_t num) : portNum (num) {}
83
84 // DIO pin
f57da01 @jcw lots of little docstrings added
authored
85
9e7f658 @jcw a bit more docs
authored
86 /// Set the pin mode of a Port's D pin. The mode() function member sets the
87 /// I/O data direction of the DIO pin associated with a specific port.
88 /// @param value INPUT or OUTPUT.
fa906da @jcw files imported
authored
89 inline void mode(uint8_t value) const
90 { pinMode(digiPin(), value); }
f57da01 @jcw lots of little docstrings added
authored
91 /// Reads the value of a Port's D pin.
92 /// @return High or Low.
fa906da @jcw files imported
authored
93 inline uint8_t digiRead() const
94 { return digitalRead(digiPin()); }
f57da01 @jcw lots of little docstrings added
authored
95 /// Write High or Low to a Port's D pin.
96 /// @param value High or Low.
fa906da @jcw files imported
authored
97 inline void digiWrite(uint8_t value) const
98 { return digitalWrite(digiPin(), value); }
f57da01 @jcw lots of little docstrings added
authored
99 /// Writes a PWM value to a Port's D pin.
fa906da @jcw files imported
authored
100 inline void anaWrite(uint8_t val) const
101 { analogWrite(digiPin(), val); }
f57da01 @jcw lots of little docstrings added
authored
102 /// Applies the Arduino pulseIn() function on a Port's D pin.
fa906da @jcw files imported
authored
103 inline uint32_t pulse(uint8_t state, uint32_t timeout =1000000L) const
104 { return pulseIn(digiPin(), state, timeout); }
105
106 // AIO pin
f57da01 @jcw lots of little docstrings added
authored
107
9e7f658 @jcw a bit more docs
authored
108 /// Set the pin mode of a Port's A pin. The mode2() function member sets
109 /// the I/O data direction of the AIO pin associated with a specific port.
110 /// @param value INPUT or OUTPUT.
fa906da @jcw files imported
authored
111 inline void mode2(uint8_t value) const
112 { pinMode(digiPin2(), value); }
f57da01 @jcw lots of little docstrings added
authored
113 /// Reads an analog value from a Port's A pin.
114 /// @return int [0..1023]
fa906da @jcw files imported
authored
115 inline uint16_t anaRead() const
116 { return analogRead(anaPin()); }
f57da01 @jcw lots of little docstrings added
authored
117 /// Reads the value of a Port's A pin.
118 /// @return High or Low.
fa906da @jcw files imported
authored
119 inline uint8_t digiRead2() const
120 { return digitalRead(digiPin2()); }
f57da01 @jcw lots of little docstrings added
authored
121 /// Write High or Low to a Port's A pin.
122 /// @param value High or Low.
fa906da @jcw files imported
authored
123 inline void digiWrite2(uint8_t value) const
124 { return digitalWrite(digiPin2(), value); }
f57da01 @jcw lots of little docstrings added
authored
125 /// Applies the Arduino pulseIn() function on a Port's A pin.
64f7c7c @jcw more docs from old wiki copied in
authored
126 /// @see http://arduino.cc/en/Reference/pulseIn for more details.
fa906da @jcw files imported
authored
127 inline uint32_t pulse2(uint8_t state, uint32_t timeout =1000000L) const
128 { return pulseIn(digiPin2(), state, timeout); }
129
130 // IRQ pin (INT1, shared across all ports)
f57da01 @jcw lots of little docstrings added
authored
131
9e7f658 @jcw a bit more docs
authored
132 /// Set the pin mode of the I pin on all Ports. The mode3() function member
133 /// sets the I/O direction of the IRQ pin associated with a specific port.
134 /// Note that this is the same pin on all ports.
135 /// @param value INPUT or OUTPUT.
fa906da @jcw files imported
authored
136 static void mode3(uint8_t value)
3e08ca2 @jcw add the "tiny50hz" sample sketch for ATtiny85
authored
137 { pinMode(digiPin3(), value); }
f57da01 @jcw lots of little docstrings added
authored
138 /// Reads the value of the I pin on all Ports.
139 /// @return High or Low.
fa906da @jcw files imported
authored
140 static uint8_t digiRead3()
3e08ca2 @jcw add the "tiny50hz" sample sketch for ATtiny85
authored
141 { return digitalRead(digiPin3()); }
f57da01 @jcw lots of little docstrings added
authored
142 /// Writes the value of the I pin on all Ports.
143 /// @param value High or Low.
fa906da @jcw files imported
authored
144 static void digiWrite3(uint8_t value)
3e08ca2 @jcw add the "tiny50hz" sample sketch for ATtiny85
authored
145 { return digitalWrite(digiPin3(), value); }
f57da01 @jcw lots of little docstrings added
authored
146 /// Writes a PWM value to the I pin of all Ports.
fa906da @jcw files imported
authored
147 static void anaWrite3(uint8_t val)
3e08ca2 @jcw add the "tiny50hz" sample sketch for ATtiny85
authored
148 { analogWrite(digiPin3(), val); }
a4fddf5 @Vliegendehuiskat Port Documentation.
Vliegendehuiskat authored
149
fa906da @jcw files imported
authored
150 // both pins: data on DIO, clock on AIO
f57da01 @jcw lots of little docstrings added
authored
151
64f7c7c @jcw more docs from old wiki copied in
authored
152 /// Does Arduino shiftOut() with data on D and clock on A pin of the Port.
fa906da @jcw files imported
authored
153 inline void shift(uint8_t bitOrder, uint8_t value) const
154 { shiftOut(digiPin(), digiPin2(), bitOrder, value); }
155 uint16_t shiftRead(uint8_t bitOrder, uint8_t count =8) const;
156 void shiftWrite(uint8_t bitOrder, uint16_t value, uint8_t count =8) const;
157 };
158
f57da01 @jcw lots of little docstrings added
authored
159 /// These objects represent remote nodes connected via wireless.
160 /// Requires the RemotePort and RemoteHandler classes.
fa906da @jcw files imported
authored
161 class RemoteNode {
162 public:
f57da01 @jcw lots of little docstrings added
authored
163 /// @struct Data
164 /// %Data structure exchanged to implement RemoteNode functionality.
fa906da @jcw files imported
authored
165 typedef struct {
166 uint8_t flags, modes, digiIO, anaOut[2];
167 uint16_t anaIn[4]; // only bits 0..11 used
168 } Data;
169
170 RemoteNode (char id, uint8_t band, uint8_t group =0);
171
172 void poll(uint16_t msecs);
173
174 friend class RemoteHandler;
175 friend class RemotePort;
176 private:
177 uint8_t nid;
178 uint32_t lastPoll;
179 Data data;
180 };
181
f57da01 @jcw lots of little docstrings added
authored
182 /// A remote handler is able to deal with information from remote nodes.
fa906da @jcw files imported
authored
183 class RemoteHandler {
184 public:
185 static void setup(uint8_t id, uint8_t band, uint8_t group =0);
186 static uint8_t poll(RemoteNode& node, uint8_t send);
187 };
188
f57da01 @jcw lots of little docstrings added
authored
189 /// A remote port is like a local port, bot connected to a remote node.
fa906da @jcw files imported
authored
190 class RemotePort : protected Port {
191 RemoteNode& node;
192
193 inline uint8_t pinBit() const
194 { return portNum - 1; }
195 inline uint8_t pinBit2() const
196 { return portNum + 3; }
197 public:
198 RemotePort (RemoteNode& remote, uint8_t num) : Port (num), node (remote) {}
199
200 void mode(uint8_t value) const;
201 uint8_t digiRead() const;
202 void digiWrite(uint8_t value) const;
203 void anaWrite(uint8_t val) const;
204
205 void mode2(uint8_t value) const;
206 uint16_t anaRead() const;
207 uint8_t digiRead2() const;
208 void digiWrite2(uint8_t value) const;
209 };
210
f57da01 @jcw lots of little docstrings added
authored
211 /// Can be used to drive a software (bit-banged) I2C bus via a Port interface.
64f7c7c @jcw more docs from old wiki copied in
authored
212 /// @todo Speed up the I2C bit I/O, it's far too slow right now.
fa906da @jcw files imported
authored
213 class PortI2C : public Port {
214 uint8_t uswait;
681769e @jcw change defaults for convenient use without jumpers
authored
215 #if 0
216 // speed test with fast hard-coded version for Port 1:
217 inline void hold() const
218 { _delay_us(1); }
219 inline void sdaOut(uint8_t value) const
220 { bitWrite(DDRD, 4, !value); bitWrite(PORTD, 4, value); }
221 inline uint8_t sdaIn() const
222 { return bitRead(PORTD, 4); }
223 inline void sclHi() const
224 { hold(); bitWrite(PORTC, 0, 1); }
225 inline void sclLo() const
226 { hold(); bitWrite(PORTC, 0, 0); }
8dcb901 @jcw fix erroneous commit
authored
227 public:
228 enum { KHZMAX, KHZ400, KHZ100, KHZ_SLOW };
681769e @jcw change defaults for convenient use without jumpers
authored
229 #else
fa906da @jcw files imported
authored
230 inline void hold() const
231 { delayMicroseconds(uswait); }
232 inline void sdaOut(uint8_t value) const
233 { mode(!value); digiWrite(value); }
234 inline uint8_t sdaIn() const
235 { return digiRead(); }
236 inline void sclHi() const
237 { hold(); digiWrite2(1); }
238 inline void sclLo() const
239 { hold(); digiWrite2(0); }
240 public:
8dcb901 @jcw fix erroneous commit
authored
241 enum { KHZMAX = 1, KHZ400 = 2, KHZ100 = 9 };
242 #endif
fa906da @jcw files imported
authored
243
64f7c7c @jcw more docs from old wiki copied in
authored
244 /// Creates an instance of class PortI2C
245 /// @param num port number corresponding to physical JeeNode port number.
246 /// @param rate in microseconds - time delay between bits? (not quite!)
fa906da @jcw files imported
authored
247 PortI2C (uint8_t num, uint8_t rate =KHZMAX);
248
64f7c7c @jcw more docs from old wiki copied in
authored
249 /// Initalize I2C communication on a JeeNode port.
250 /// @param addr I2C address of device with which to communicate
251 /// @returns 1 if communication succeeded, 0 otherwise
fa906da @jcw files imported
authored
252 uint8_t start(uint8_t addr) const;
64f7c7c @jcw more docs from old wiki copied in
authored
253 /// Terminate transmission on an I2C connection.
fa906da @jcw files imported
authored
254 void stop() const;
64f7c7c @jcw more docs from old wiki copied in
authored
255 /// Send one byte of data to the currently address I2C device.
256 /// @param data the data byte to send out
257 /// @returns 1 if device acknowledged write, 0 if device did not respond
fa906da @jcw files imported
authored
258 uint8_t write(uint8_t data) const;
64f7c7c @jcw more docs from old wiki copied in
authored
259 /// Read a byte using I2C protocol on a JeeNode port.
260 /// @param last pass 1 to signal the last byte read in this bus transaction
261 /// @returns data (byte) read from the I2C device
fa906da @jcw files imported
authored
262 uint8_t read(uint8_t last) const;
263 };
264
f57da01 @jcw lots of little docstrings added
authored
265 /// Each device on the I2C bus needs to be defined using a DeviceI2C instance.
fa906da @jcw files imported
authored
266 class DeviceI2C {
267 const PortI2C& port;
268 uint8_t addr;
269
270 public:
271 DeviceI2C(const PortI2C& p, uint8_t me) : port (p), addr (me << 1) {}
272
64f7c7c @jcw more docs from old wiki copied in
authored
273 /// see if a device answers at an I2C address
fa906da @jcw files imported
authored
274 bool isPresent() const;
275
64f7c7c @jcw more docs from old wiki copied in
authored
276 /// Create a start condition on the I2C bus, and set things up for sending
277 /// data to this device.
278 /// @returns true if acknowledged by the slave device.
fa906da @jcw files imported
authored
279 uint8_t send() const
280 { return port.start(addr); }
64f7c7c @jcw more docs from old wiki copied in
authored
281 /// Create a start condition on the I2C bus, and set things up for receiving
282 /// data from this device.
283 /// @returns true if acknowledged.
fa906da @jcw files imported
authored
284 uint8_t receive() const
285 { return port.start(addr | 1); }
64f7c7c @jcw more docs from old wiki copied in
authored
286 /// Create a stop condition on the I2C bus, ending the current transfer.
fa906da @jcw files imported
authored
287 void stop() const
288 { port.stop(); }
64f7c7c @jcw more docs from old wiki copied in
authored
289 /// Write a byte to the currently addressed device. Must be preceded by a
290 /// proper PortI2C start() call.
291 /// @param data Data byte to be sent.
292 /// @returns true if the device acknowledged the byte (accepts more data).
fa906da @jcw files imported
authored
293 uint8_t write(uint8_t data) const
294 { return port.write(data); }
64f7c7c @jcw more docs from old wiki copied in
authored
295 /// Read a byte from the currently addressed device. Must be preceded by a
296 /// proper PortI2C start() call.
297 /// @param last Indicates whether this is the last byte to read. Used to
298 /// respond to the write with a positive or negative ack.
299 /// Pass 1 if reading the last byte, otherwise pass 0.
fa906da @jcw files imported
authored
300 uint8_t read(uint8_t last) const
301 { return port.read(last); }
302
3b6884f @jcw get rid of some long-standing warnings
authored
303 void setAddress(uint8_t me)
fa906da @jcw files imported
authored
304 { addr = me << 1; }
305 };
306
2d1102a @Vliegendehuiskat Mainly formatting already existing comments to Doxygen format.
Vliegendehuiskat authored
307 /// The millisecond timer can be used for timeouts up to 60000 milliseconds.
308 /// Setting the timeout to zero disables the timer.
309 ///
64f7c7c @jcw more docs from old wiki copied in
authored
310 /// * for periodic use, poll the timer object with "if (timer.poll(123)) ..."
311 /// * for one-shot use, call "timer.set(123)" and poll as "if (timer.poll())"
fa906da @jcw files imported
authored
312
313 class MilliTimer {
314 word next;
315 byte armed;
316 public:
317 MilliTimer () : armed (0) {}
318
64f7c7c @jcw more docs from old wiki copied in
authored
319 /// poll until the timer fires
320 /// @param ms Periodic repeat rate of the time, omit for a one-shot timer.
fa906da @jcw files imported
authored
321 byte poll(word ms =0);
64f7c7c @jcw more docs from old wiki copied in
authored
322 /// Return the number of milliseconds before the timer will fire
fa906da @jcw files imported
authored
323 word remaining() const;
64f7c7c @jcw more docs from old wiki copied in
authored
324 /// Returns true if the timer is not armed
fa906da @jcw files imported
authored
325 byte idle() const { return !armed; }
64f7c7c @jcw more docs from old wiki copied in
authored
326 /// set the one-shot timeout value
327 /// @param ms Timeout value. Timer stops once the timer has fired.
fa906da @jcw files imported
authored
328 void set(word ms);
329 };
330
f57da01 @jcw lots of little docstrings added
authored
331 /// Low-power utility code using the Watchdog Timer (WDT). Requires a WDT
332 /// interrupt handler, e.g. EMPTY_INTERRUPT(WDT_vect);
fa906da @jcw files imported
authored
333 class Sleepy {
334 public:
2d1102a @Vliegendehuiskat Mainly formatting already existing comments to Doxygen format.
Vliegendehuiskat authored
335 /// start the watchdog timer (or disable it if mode < 0)
64f7c7c @jcw more docs from old wiki copied in
authored
336 /// @param mode Enable watchdog trigger after "16 << mode" milliseconds
337 /// (mode 0..9), or disable it (mode < 0).
338 /// @note If you use this function, you MUST included a definition of a WDT
339 /// interrupt handler in your code. The simplest is to include this line:
340 ///
341 /// ISR(WDT_vect) { Sleepy::watchdogEvent(); }
342 ///
343 /// This will get called when the watchdog fires.
fa906da @jcw files imported
authored
344 static void watchdogInterrupts (char mode);
345
2d1102a @Vliegendehuiskat Mainly formatting already existing comments to Doxygen format.
Vliegendehuiskat authored
346 /// enter low-power mode, wake up with watchdog, INT0/1, or pin-change
67adc3a @jcw arg to powerDown() is useless, dropped
authored
347 static void powerDown ();
fa906da @jcw files imported
authored
348
64f7c7c @jcw more docs from old wiki copied in
authored
349 /// Spend some time in low-power mode, the timing is only approximate.
350 /// @param msecs Number of milliseconds to sleep, in range 0..65535.
351 /// @returns 1 if all went normally, or 0 if some other interrupt occurred
352 /// @note If you use this function, you MUST included a definition of a WDT
353 /// interrupt handler in your code. The simplest is to include this line:
354 ///
355 /// ISR(WDT_vect) { Sleepy::watchdogEvent(); }
356 ///
357 /// This will get called when the watchdog fires.
fa906da @jcw files imported
authored
358 static byte loseSomeTime (word msecs);
359
64f7c7c @jcw more docs from old wiki copied in
authored
360 /// This must be called from your watchdog interrupt code.
fa906da @jcw files imported
authored
361 static void watchdogEvent();
362 };
363
2d1102a @Vliegendehuiskat Mainly formatting already existing comments to Doxygen format.
Vliegendehuiskat authored
364 /// simple task scheduler for times up to 6000 seconds
fa906da @jcw files imported
authored
365 class Scheduler {
b70fe01 @stumo Some bugs in scheduler
stumo authored
366 word* tasks;
367 word remaining;
fa906da @jcw files imported
authored
368 byte maxTasks;
369 MilliTimer ms100;
370 public:
2d1102a @Vliegendehuiskat Mainly formatting already existing comments to Doxygen format.
Vliegendehuiskat authored
371 /// initialize for a specified maximum number of tasks
fa906da @jcw files imported
authored
372 Scheduler (byte max);
373 Scheduler (word* buf, byte max);
374
f57da01 @jcw lots of little docstrings added
authored
375 /// Return next task to run, -1 if there are none ready to run, but there
376 /// are tasks waiting, or -2 if there are no tasks waiting (i.e. all idle)
fa906da @jcw files imported
authored
377 char poll();
2d1102a @Vliegendehuiskat Mainly formatting already existing comments to Doxygen format.
Vliegendehuiskat authored
378 /// same as poll, but wait for event in power-down mode.
f57da01 @jcw lots of little docstrings added
authored
379 /// Uses Sleepy::loseSomeTime() - see comments there re requiring the
380 /// watchdog timer.
fa906da @jcw files imported
authored
381 char pollWaiting();
382
2d1102a @Vliegendehuiskat Mainly formatting already existing comments to Doxygen format.
Vliegendehuiskat authored
383 /// set a task timer, in tenths of seconds
fa906da @jcw files imported
authored
384 void timer(byte task, word tenths);
2d1102a @Vliegendehuiskat Mainly formatting already existing comments to Doxygen format.
Vliegendehuiskat authored
385 /// cancel a task timer
fa906da @jcw files imported
authored
386 void cancel(byte task);
387
2d1102a @Vliegendehuiskat Mainly formatting already existing comments to Doxygen format.
Vliegendehuiskat authored
388 /// return true if a task timer is not running
3b6884f @jcw get rid of some long-standing warnings
authored
389 byte idle(byte task) { return tasks[task] == ~0U; }
fa906da @jcw files imported
authored
390 };
391
f57da01 @jcw lots of little docstrings added
authored
392 /// Interface for the Blink Plug - see http://jeelabs.org/bp
fa906da @jcw files imported
authored
393 class BlinkPlug : public Port {
394 MilliTimer debounce;
395 byte leds, lastState, checkFlags;
396 public:
f57da01 @jcw lots of little docstrings added
authored
397 /// Enum containing shorthands for BlinkPlug button states.
fa906da @jcw files imported
authored
398 enum { ALL_OFF, ON1, OFF1, ON2, OFF2, SOME_ON, ALL_ON }; // for buttonCheck
7d90bc2 @Vliegendehuiskat Added some more doxygen documentation.
Vliegendehuiskat authored
399
f57da01 @jcw lots of little docstrings added
authored
400 /// Constructor for the BlinkPlug class.
401 /// @param port Portnumber the blinkplug is connected to.
fa906da @jcw files imported
authored
402 BlinkPlug (byte port)
403 : Port (port), leds (0), lastState (0), checkFlags (0) {}
404
405 void ledOn(byte mask);
406 void ledOff(byte mask);
7d90bc2 @Vliegendehuiskat Added some more doxygen documentation.
Vliegendehuiskat authored
407 /// @return One byte containing the state of both leds.
c1c40cf @jcw add blink_timers demo
authored
408 byte ledState() const { return leds; }
409
fa906da @jcw files imported
authored
410 byte state();
411 byte pushed(); // deprecated, don't use in combination with buttonCheck
412 byte buttonCheck();
413 };
414
f57da01 @jcw lots of little docstrings added
authored
415 /// Interface for the Memory Plug - see http://jeelabs.org/mp
fa906da @jcw files imported
authored
416 class MemoryPlug : public DeviceI2C {
417 uint32_t nextSave;
418 public:
419 MemoryPlug (PortI2C& port)
420 : DeviceI2C (port, 0x50), nextSave (0) {}
421
9806885 @jcw change MemoryPlug API: arg order & defaults
authored
422 void load(word page, byte offset, void* buf, int count);
423 void save(word page, byte offset, const void* buf, int count);
fa906da @jcw files imported
authored
424 };
425
f57da01 @jcw lots of little docstrings added
authored
426 /// A memory stream can save and reload a stream of bytes on a MemoryPlug.
fa906da @jcw files imported
authored
427 class MemoryStream {
428 MemoryPlug& dev;
429 word start, curr;
430 char step;
431 byte buffer[256], pos;
432 public:
433 MemoryStream (MemoryPlug& plug, word page =0, char dir =1)
434 : dev (plug), start (page), curr (page), step (dir), pos (0) {}
435
436 long position(byte writing) const;
437 byte get();
438 void put(byte data);
439 word flush();
440 void reset();
441 };
442
f57da01 @jcw lots of little docstrings added
authored
443 /// Interface for the UART Plug - see http://jeelabs.org/up
fa906da @jcw files imported
authored
444 class UartPlug : public Print {
445 DeviceI2C dev;
446 // avoid per-byte access, fill entire buffer instead to reduce I2C overhead
447 byte rxbuf[20], in, out;
448
449 void regSet (byte reg, byte value);
450 void regRead (byte reg);
451
452 public:
453 UartPlug (PortI2C& port, byte addr)
454 : dev (port, addr), in (0), out (0) {}
455
456 void begin(long);
457 byte available();
458 int read();
459 void flush();
3e08ca2 @jcw add the "tiny50hz" sample sketch for ATtiny85
authored
460 virtual WRITE_RESULT write(byte);
fa906da @jcw files imported
authored
461 };
462
f57da01 @jcw lots of little docstrings added
authored
463 /// Interface for the Dimmer Plug - see http://jeelabs.org/dp
fa906da @jcw files imported
authored
464 class DimmerPlug : public DeviceI2C {
465 public:
466 enum {
467 MODE1, MODE2,
468 PWM0, PWM1, PWM2, PWM3, PWM4, PWM5, PWM6, PWM7,
469 PWM8, PWM9, PWM10, PWM11, PWM12, PWM13, PWM14, PWM15,
470 GRPPWM, GRPFREQ,
471 LEDOUT0, LEDOUT1, LEDOUT2, LEDOUT3,
472 SUBADR1, SUBADR2, SUBADR3, ALLCALLADR,
473 };
474
475 DimmerPlug (PortI2C& port, byte addr)
476 : DeviceI2C (port, addr) {}
477
478 void begin ();
479 byte getReg(byte reg) const;
480 void setReg(byte reg, byte value) const;
481 void setMulti(byte reg, ...) const;
482 };
483
f57da01 @jcw lots of little docstrings added
authored
484 /// Interface for the Lux Plug - see http://jeelabs.org/xp
fa906da @jcw files imported
authored
485 class LuxPlug : public DeviceI2C {
486 union { byte b[4]; word w[2]; } data;
487 public:
488 enum {
489 CONTROL, TIMING,
490 THRESHLOWLOW, THRESHLOWHIGH, THRESHHIGHLOW, THRESHHIGHHIGH, INTERRUPT,
491 LUXID = 0xA,
492 DATA0LOW = 0xC, DATA0HIGH, DATA1LOW, DATA1HIGH,
493 };
494
495 LuxPlug (PortI2C& port, byte addr) : DeviceI2C (port, addr) {}
6f94f49 @Vliegendehuiskat Documented LuxPlug member functions.
Vliegendehuiskat authored
496
f57da01 @jcw lots of little docstrings added
authored
497 /// Initialize the LuxPlug. Wait at least 1000 ms after calling this!
fa906da @jcw files imported
authored
498 void begin() {
499 send();
500 write(0xC0 | CONTROL);
501 write(3); // power up
502 stop();
503 }
6f94f49 @Vliegendehuiskat Documented LuxPlug member functions.
Vliegendehuiskat authored
504
1235cd8 @Vliegendehuiskat Made Ports.h Documentation more compact and added some documentation to ...
Vliegendehuiskat authored
505 ///Power down the lux plug for low power usage.
03a0ff6 @Vliegendehuiskat Added function to completely power off the Lux Plug and an example sketc...
Vliegendehuiskat authored
506 void poweroff() {
507 send();
508 write(0xC0 | CONTROL);
509 write(0); // power down
510 stop();
511 }
512
fa906da @jcw files imported
authored
513 void setGain(byte high);
514
515 const word* getData();
516
517 word calcLux(byte iGain =0, byte tInt =2) const;
518 };
519
c38783a Add HYT131 support to Ports and roomNode
Geert Folkertsma authored
520 // Interface for the HYT131 thermometer/hygrometer - see http://jeelabs.org/2012/06/30/new-hyt131-sensor/
521 class HYT131 : public DeviceI2C {
522 public:
523 // Constructor for the HYT131 sensor.
524 HYT131 (PortI2C& port) : DeviceI2C (port, 0x28) {}
525
526 // Execute a reading; results are in tenths of degrees and percent, respectively
527 // @param temp in which to store the temperature (int, tenths of degrees C)
528 // @param humi in which to store the humidity (int, tenths of percent)
529 // @param delayFun (optional) supply delayFun that takes ms delay as argument, for low-power waiting during reading (e.g. Sleepy::loseSomeTime()). By default, delay() is used
530 void reading (int& temp, int& humi, byte (*delayFun)(word ms) =0);
531 };
532
f57da01 @jcw lots of little docstrings added
authored
533 /// Interface for the Gravity Plug - see http://jeelabs.org/gp
fa906da @jcw files imported
authored
534 class GravityPlug : public DeviceI2C {
f57da01 @jcw lots of little docstrings added
authored
535 /// Data storage for getAxes() and sensitivity()
fa906da @jcw files imported
authored
536 union { byte b[6]; int w[3]; } data;
537 public:
f57da01 @jcw lots of little docstrings added
authored
538 /// Constructor for Gravity Plug.
fa906da @jcw files imported
authored
539 GravityPlug (PortI2C& port) : DeviceI2C (port, 0x38) {}
032d9c4 @Vliegendehuiskat Added GravityPlug documentation.
Vliegendehuiskat authored
540
f57da01 @jcw lots of little docstrings added
authored
541 /// Setup GravityPlug. Call during setup()
fa906da @jcw files imported
authored
542 void begin() {}
f57da01 @jcw lots of little docstrings added
authored
543 /// Set GravityPlug sensitivity.
544 /// @param range 2,4,8
545 /// @param bw (optional) bandwidth.
032d9c4 @Vliegendehuiskat Added GravityPlug documentation.
Vliegendehuiskat authored
546 void sensitivity(byte range, word bw =0);
547
f57da01 @jcw lots of little docstrings added
authored
548 /// Get accelleration data from GravityPlug.
549 /// @return An array with 3 integers. (x,y,z) respectively.
fa906da @jcw files imported
authored
550 const int* getAxes();
17a45b2 @jcw add support for BMA150
authored
551 /// Read out the temperature (only for BMA150, not the older BMA020)
552 /// @return temp, in half deg C steps, from -30C to +50C (i.e. times 2)
553 char temperature();
fa906da @jcw files imported
authored
554 };
555
f57da01 @jcw lots of little docstrings added
authored
556 /// Interface for the Input Plug - see http://jeelabs.org/ip
fa906da @jcw files imported
authored
557 class InputPlug : public Port {
558 uint8_t slow;
559 public:
560 InputPlug (uint8_t num, uint8_t fix =0) : Port (num), slow (fix) {}
561
562 void select(uint8_t channel);
563 };
564
f57da01 @jcw lots of little docstrings added
authored
565 /// Interface for the Infrared Plug - see http://jeelabs.org/ir
fa906da @jcw files imported
authored
566 class InfraredPlug : public Port {
567 uint8_t slot, gap, buf [40];
568 char fill;
569 uint32_t prev;
570 public:
f57da01 @jcw lots of little docstrings added
authored
571 /// Initialize with default values for NEC protocol
fa906da @jcw files imported
authored
572 InfraredPlug (uint8_t num);
573
f57da01 @jcw lots of little docstrings added
authored
574 /// Set slot size (us*4) and end-of-data gap (us*256)
fa906da @jcw files imported
authored
575 void configure(uint8_t slot4, uint8_t gap256 =80);
576
f57da01 @jcw lots of little docstrings added
authored
577 /// Call this continuously or at least right after a pin change
fa906da @jcw files imported
authored
578 void poll();
579
f57da01 @jcw lots of little docstrings added
authored
580 /// Returns number of nibbles read, or 0 if not yet ready
fa906da @jcw files imported
authored
581 uint8_t done();
7d90bc2 @Vliegendehuiskat Added some more doxygen documentation.
Vliegendehuiskat authored
582
fa906da @jcw files imported
authored
583 enum { UNKNOWN, NEC, NEC_REP };
f57da01 @jcw lots of little docstrings added
authored
584 /// Try to decode a received packet, return type of packet
585 /// if recognized, the receive buffer will be overwritten with the results
fa906da @jcw files imported
authored
586 uint8_t decoder(uint8_t nibbles);
587
f57da01 @jcw lots of little docstrings added
authored
588 /// Access to the receive buffer
fa906da @jcw files imported
authored
589 const uint8_t* buffer() { return buf; }
590
f57da01 @jcw lots of little docstrings added
authored
591 /// Send out a bit pattern, cycle time is the "slot4" config value
fa906da @jcw files imported
authored
592 void send(const uint8_t* data, uint16_t bits);
593 };
594
f57da01 @jcw lots of little docstrings added
authored
595 /// Interface for the Heading Board - see http://jeelabs.org/hb
fa906da @jcw files imported
authored
596 class HeadingBoard : public PortI2C {
597 DeviceI2C eeprom, adc, compass;
598 Port aux;
599 // keep following fields in order:
600 word C1, C2, C3, C4, C5, C6, C7;
601 byte A, B, C, D, setReset;
602
603 byte eepromByte(byte reg) const;
604 void getConstants();
605 word adcValue(byte press) const;
606
607 public:
608 HeadingBoard (int num)
609 : PortI2C (num), eeprom (*this, 0x50), adc (*this, 0x77),
610 compass (*this, 0x30), aux (5-num), setReset (0x02) {}
611
612 void begin();
613 void pressure(int& temp, int& pres) const;
614 void heading(int& xaxis, int& yaxis);
615 };
616
f57da01 @jcw lots of little docstrings added
authored
617 /// Interface for the Modern Device 3-axis Compass board.
618 /// See http://shop.moderndevice.com/products/3-axis-compass
b4249aa @jcw add CompassBoard class and demo
authored
619 class CompassBoard : public DeviceI2C {
620 int read2 (byte last);
621 public:
622 CompassBoard (PortI2C& port) : DeviceI2C (port, 0x1E) {}
623
624 float heading();
625 };
626
f57da01 @jcw lots of little docstrings added
authored
627 /// Interface for the Proximity Plug - see http://jeelabs.org/yp
fa906da @jcw files imported
authored
628 class ProximityPlug : public DeviceI2C {
629 public:
630 enum {
631 FIFO, FAULT, TPSTATUS, TPCONFIG,
632 STR1, STR2, STR3, STR4, STR5, STR6, STR7, STR8,
633 ECEMR, MNTPR, MTPR, TASPR, SCR, LPCR, SKTR,
634 CONFIG, SINFO,
635 };
636
637 ProximityPlug (PortI2C& port, byte num =0)
638 : DeviceI2C (port, 0x5C + num) {}
639
640 void begin();
641
642 void setReg(byte reg, byte value) const;
643 byte getReg(byte reg) const;
644 };
645
f57da01 @jcw lots of little docstrings added
authored
646 /// Interface for the Analog Plug - see http://jeelabs.org/ap
0a694f7 @jcw new AnalogPlug class added
authored
647 class AnalogPlug : public DeviceI2C {
648 byte config;
649 public:
650 AnalogPlug (const PortI2C& port, byte addr =0x69)
651 : DeviceI2C (port, addr), config (0x1C) {}
652
2d1102a @Vliegendehuiskat Mainly formatting already existing comments to Doxygen format.
Vliegendehuiskat authored
653 /// Default mode is channel 1, continuous, 18-bit, gain x1
0a694f7 @jcw new AnalogPlug class added
authored
654 void begin (byte mode =0x1C);
f57da01 @jcw lots of little docstrings added
authored
655 /// Select channel (1..4), must wait to read it out (up to 270 ms for 18-bit)
0a694f7 @jcw new AnalogPlug class added
authored
656 void select (byte channel);
2d1102a @Vliegendehuiskat Mainly formatting already existing comments to Doxygen format.
Vliegendehuiskat authored
657 /// Read out 4 bytes, caller will need to shift out the irrelevant lower bits
0a694f7 @jcw new AnalogPlug class added
authored
658 long reading ();
659 };
660
2d1102a @Vliegendehuiskat Mainly formatting already existing comments to Doxygen format.
Vliegendehuiskat authored
661 /// Interface for the DHT11 and DHT22 sensors, does not use floating point
604bec3 @jcw add DHTxx class for interfacing to DHT11/DHT22 sensors
authored
662 class DHTxx {
663 byte pin;
664 public:
665 DHTxx (byte pinNum);
bacf47c @jcw fix the DHTxx class for DHT21/DHT22 use, fixes #47
authored
666 /// Results are returned in tenths of a degree and percent, respectively.
667 /// Set "precise" to true for the more accurate DHT21 and DHT22 sensors.
668 bool reading (int& temp, int &humi, bool precise =false);
604bec3 @jcw add DHTxx class for interfacing to DHT11/DHT22 sensors
authored
669 };
670
bc4ee1b @jcw add ColorPlug class and demo code
authored
671 /// Interface for the Color Plug - see http://jeelabs.org/cp
672 class ColorPlug : public DeviceI2C {
673 union { byte b[8]; word w[4]; } data;
8fa0aee @jcw remove foating point from the public ColorPlug API
authored
674 word chromacct[3];
bc4ee1b @jcw add ColorPlug class and demo code
authored
675 public:
676 enum {
677 CONTROL, TIMING, INTERRUPT, INTERRUPTSOURCE, CPID, GAIN = 0x7,
678 THRESHLOWLOW, THRESHLOWHIGH, THRESHHIGHLOW, THRESHHIGHHIGH,
679 DATA0LOW = 0x10, DATA0HIGH, DATA1LOW, DATA1HIGH,
680 DATA2LOW, DATA2HIGH, DATA3LOW, DATA3HIGH,
681 BLOCKREAD = 0x4F
682 };
683
684 ColorPlug (PortI2C& port, byte addr) : DeviceI2C (port, addr) {}
685
686 void begin() {
687 send();
688 write(0x80 | CONTROL);
689 write(3); // power up
690 stop();
691 }
692
693 void setGain(byte gain, byte prescaler);
694
4ba349a @jcw better settings to avoid overflow
authored
695 // returns four 16-bit values: red, green, blue, and clear intensities
bc4ee1b @jcw add ColorPlug class and demo code
authored
696 const word* getData();
697
8fa0aee @jcw remove foating point from the public ColorPlug API
authored
698 const word* chromaCCT();
bc4ee1b @jcw add ColorPlug class and demo code
authored
699 };
700
fa906da @jcw files imported
authored
701 #ifdef Stream_h // only available in recent Arduino IDE versions
702
2d1102a @Vliegendehuiskat Mainly formatting already existing comments to Doxygen format.
Vliegendehuiskat authored
703 /// Simple parser for input data and one-letter commands
fa906da @jcw files imported
authored
704 class InputParser {
705 public:
706 typedef struct {
707 char code; // one-letter command code
708 byte bytes; // number of bytes required as input
709 void (*fun)(); // code to call for this command
710 } Commands;
711
2d1102a @Vliegendehuiskat Mainly formatting already existing comments to Doxygen format.
Vliegendehuiskat authored
712 /// Set up with a buffer of specified size
3b6884f @jcw get rid of some long-standing warnings
authored
713 InputParser (byte size, Commands*, Stream& =Serial);
714 InputParser (byte* buf, byte size, Commands*, Stream& =Serial);
fa906da @jcw files imported
authored
715
2d1102a @Vliegendehuiskat Mainly formatting already existing comments to Doxygen format.
Vliegendehuiskat authored
716 /// Number of data bytes
fa906da @jcw files imported
authored
717 byte count() { return fill; }
718
2d1102a @Vliegendehuiskat Mainly formatting already existing comments to Doxygen format.
Vliegendehuiskat authored
719 /// Call this frequently to check for incoming data
fa906da @jcw files imported
authored
720 void poll();
721
722 InputParser& operator >> (char& v) { return get(&v, 1); }
723 InputParser& operator >> (byte& v) { return get(&v, 1); }
724 InputParser& operator >> (int& v) { return get(&v, 2); }
725 InputParser& operator >> (word& v) { return get(&v, 2); }
726 InputParser& operator >> (long& v) { return get(&v, 4); }
727 InputParser& operator >> (uint32_t& v) { return get(&v, 4); }
728 InputParser& operator >> (const char*& v);
729
730 private:
731 InputParser& get(void*, byte);
732 void reset();
733
734 byte *buffer, limit, fill, top, next;
735 byte instring, hexmode, hasvalue;
736 uint32_t value;
737 Commands* cmds;
738 Stream& io;
739 };
740
741 #endif // Stream_h
742
743 #endif
Something went wrong with that request. Please try again.