15 #include <avr/pgmspace.h>
22 #define WRITE_RESULT size_t
24 #define WRITE_RESULT void
39 #if defined(__AVR_ATtiny85__) || defined(__AVR_ATtiny45__)
50 inline uint8_t
anaPin()
const
52 #elif defined(__AVR_ATtiny84__)
63 inline uint8_t
anaPin()
const
89 inline void mode(uint8_t value)
const
94 {
return digitalRead(
digiPin()); }
98 {
return digitalWrite(
digiPin(), value); }
101 { analogWrite(
digiPin(), val); }
103 inline uint32_t
pulse(uint8_t state, uint32_t timeout =1000000L)
const
104 {
return pulseIn(
digiPin(), state, timeout); }
111 inline void mode2(uint8_t value)
const
116 {
return analogRead(
anaPin()); }
124 {
return digitalWrite(
digiPin2(), value); }
127 inline uint32_t
pulse2(uint8_t state, uint32_t timeout =1000000L)
const
128 {
return pulseIn(
digiPin2(), state, timeout); }
145 {
return digitalWrite(
digiPin3(), value); }
153 inline void shift(uint8_t bitOrder, uint8_t value)
const
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;
166 uint8_t flags, modes, digiIO, anaOut[2];
170 RemoteNode (
char id, uint8_t band, uint8_t group =0);
172 void poll(uint16_t msecs);
185 static void setup(uint8_t
id, uint8_t band, uint8_t group =0);
186 static uint8_t poll(
RemoteNode& node, uint8_t send);
193 inline uint8_t pinBit()
const
195 inline uint8_t pinBit2()
const
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;
205 void mode2(uint8_t value)
const;
206 uint16_t anaRead()
const;
207 uint8_t digiRead2()
const;
208 void digiWrite2(uint8_t value)
const;
217 inline void hold()
const
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); }
228 enum { KHZMAX, KHZ400, KHZ100, KHZ_SLOW };
230 inline void hold()
const
231 { delayMicroseconds(uswait); }
232 inline void sdaOut(uint8_t value)
const
234 inline uint8_t sdaIn()
const
236 inline void sclHi()
const
238 inline void sclLo()
const
241 enum { KHZMAX = 1, KHZ400 = 2, KHZ100 = 9 };
247 PortI2C (uint8_t num, uint8_t rate =KHZMAX);
252 uint8_t
start(uint8_t addr)
const;
258 uint8_t
write(uint8_t data)
const;
262 uint8_t
read(uint8_t last)
const;
280 {
return port.
start(addr); }
285 {
return port.
start(addr | 1); }
294 {
return port.
write(data); }
300 uint8_t
read(uint8_t last)
const
301 {
return port.
read(last); }
303 void setAddress(uint8_t me)
321 byte
poll(word ms =0);
325 byte
idle()
const {
return !armed; }
384 void timer(byte task, word tenths);
389 byte
idle(byte task) {
return tasks[task] == ~0U; }
395 byte leds, lastState, checkFlags;
398 enum { ALL_OFF, ON1, OFF1, ON2, OFF2, SOME_ON, ALL_ON };
403 :
Port (port), leds (0), lastState (0), checkFlags (0) {}
405 void ledOn(byte mask);
420 :
DeviceI2C (port, 0x50), nextSave (0) {}
422 void load(word page, byte offset,
void* buf,
int count);
423 void save(word page, byte offset,
const void* buf,
int count);
431 byte buffer[256], pos;
434 : dev (plug), start (page), curr (page), step (dir), pos (0) {}
436 long position(byte writing)
const;
447 byte rxbuf[20], in, out;
449 void regSet (byte reg, byte value);
450 void regRead (byte reg);
454 : dev (port, addr), in (0), out (0) {}
460 virtual WRITE_RESULT
write(byte);
468 PWM0, PWM1, PWM2, PWM3, PWM4, PWM5, PWM6, PWM7,
469 PWM8, PWM9, PWM10, PWM11, PWM12, PWM13, PWM14, PWM15,
471 LEDOUT0, LEDOUT1, LEDOUT2, LEDOUT3,
472 SUBADR1, SUBADR2, SUBADR3, ALLCALLADR,
479 byte getReg(byte reg)
const;
480 void setReg(byte reg, byte value)
const;
481 void setMulti(byte reg, ...)
const;
486 union { byte b[4]; word w[2]; } data;
490 THRESHLOWLOW, THRESHLOWHIGH, THRESHHIGHLOW, THRESHHIGHHIGH, INTERRUPT,
492 DATA0LOW = 0xC, DATA0HIGH, DATA1LOW, DATA1HIGH,
500 write(0xC0 | CONTROL);
508 write(0xC0 | CONTROL);
517 word
calcLux(byte iGain =0, byte tInt =2)
const;
530 void reading (
int& temp,
int& humi, byte (*delayFun)(word ms) =0);
536 union { byte b[6];
int w[3]; } data;
560 InputPlug (uint8_t num, uint8_t fix =0) :
Port (num), slow (fix) {}
562 void select(uint8_t channel);
567 uint8_t slot, gap, buf [40];
575 void configure(uint8_t slot4, uint8_t gap256 =80);
583 enum { UNKNOWN, NEC, NEC_REP };
586 uint8_t
decoder(uint8_t nibbles);
592 void send(
const uint8_t* data, uint16_t bits);
600 word C1, C2, C3, C4, C5, C6, C7;
601 byte A, B, C, D, setReset;
603 byte eepromByte(byte reg)
const;
605 word adcValue(byte press)
const;
609 :
PortI2C (num), eeprom (*
this, 0x50), adc (*
this, 0x77),
610 compass (*
this, 0x30), aux (5-num), setReset (0x02) {}
613 void pressure(
int& temp,
int& pres)
const;
614 void heading(
int& xaxis,
int& yaxis);
620 int read2 (byte last);
631 FIFO, FAULT, TPSTATUS, TPCONFIG,
632 STR1, STR2, STR3, STR4, STR5, STR6, STR7, STR8,
633 ECEMR, MNTPR, MTPR, TASPR, SCR, LPCR, SKTR,
642 void setReg(byte reg, byte value)
const;
643 byte getReg(byte reg)
const;
651 :
DeviceI2C (port, addr), config (0x1C) {}
654 void begin (byte mode =0x1C);
656 void select (byte channel);
668 bool reading (
int& temp,
int &humi,
bool precise =
false);
673 union { byte b[8]; word w[4]; } data;
677 CONTROL, TIMING, INTERRUPT, INTERRUPTSOURCE, CPID, GAIN = 0x7,
678 THRESHLOWLOW, THRESHLOWHIGH, THRESHHIGHLOW, THRESHHIGHHIGH,
679 DATA0LOW = 0x10, DATA0HIGH, DATA1LOW, DATA1HIGH,
680 DATA2LOW, DATA2HIGH, DATA3LOW, DATA3HIGH,
688 write(0x80 | CONTROL);
693 void setGain(byte gain, byte prescaler);
696 const word* getData();
698 const word* chromaCCT();
701 #ifdef Stream_h // only available in recent Arduino IDE versions
713 InputParser (byte size, Commands*, Stream& =Serial);
714 InputParser (byte* buf, byte size, Commands*, Stream& =Serial);
717 byte count() {
return fill; }
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);
731 InputParser&
get(
void*, byte);
734 byte *buffer, limit, fill, top, next;
735 byte instring, hexmode, hasvalue;
byte buttonCheck()
Check the state of the buttons.
Definition: Ports.cpp:331
Data structure exchanged to implement RemoteNode functionality.
Definition: Ports.h:165
Interface for the Color Plug - see https://jeelabs.org/cp.
Definition: Ports.h:672
Interface for JeeNode Ports - see the wiki docs for JeeNodes and pinouts.
Definition: Ports.h:33
uint8_t decoder(uint8_t nibbles)
Try to decode a received packet, return type of packet if recognized, the receive buffer will be over...
Definition: Ports.cpp:873
const word * getData()
Read the raw data from the photodiodes.
Definition: Ports.cpp:560
The millisecond timer can be used for timeouts up to 60000 milliseconds.
Definition: Ports.h:313
PortI2C(uint8_t num, uint8_t rate=KHZMAX)
Creates an instance of class PortI2C.
Definition: Ports.cpp:182
uint16_t shiftRead(uint8_t bitOrder, uint8_t count=8) const
Shift a number of bites in to read them.
Definition: Ports.cpp:87
uint8_t write(uint8_t data) const
Send one byte of data to the currently address I2C device.
Definition: Ports.cpp:203
Interface for the Dimmer Plug - see https://jeelabs.org/dp.
Definition: Ports.h:464
simple task scheduler for times up to 6000 seconds
Definition: Ports.h:365
void cancel(byte task)
cancel a task timer
Definition: Ports.cpp:1248
static void digiWrite3(uint8_t value)
Writes the value of the I pin on all Ports.
Definition: Ports.h:144
void begin(byte mode=0x1C)
Default mode is channel 1, continuous, 18-bit, gain x1.
Definition: Ports.cpp:934
void mode2(uint8_t value) const
Set the pin mode of a Port's A pin.
Definition: Ports.h:111
uint8_t start(uint8_t addr) const
Initalize I2C communication on a JeeNode port.
Definition: Ports.cpp:190
Low-power utility code using the Watchdog Timer (WDT).
Definition: Ports.h:333
void set(word ms)
set the one-shot timeout value
Definition: Ports.cpp:263
static void mode3(uint8_t value)
Set the pin mode of the I pin on all Ports.
Definition: Ports.h:136
A remote handler is able to deal with information from remote nodes.
Definition: Ports.h:183
bool reading(int &temp, int &humi, bool precise=false)
Results are returned in tenths of a degree and percent, respectively.
Definition: Ports.cpp:985
word calcLux(byte iGain=0, byte tInt=2) const
Calculate Lux value from the raw data retreived.
Definition: Ports.cpp:581
void send(const uint8_t *data, uint16_t bits)
Send out a bit pattern, cycle time is the "slot4" config value.
Definition: Ports.cpp:899
void setGain(byte high)
Set the gain mode of the 16x multiplier in the LuxPlug.
Definition: Ports.cpp:550
char pollWaiting()
same as poll, but wait for event in power-down mode.
Definition: Ports.cpp:1219
Interface for the Gravity Plug - see https://jeelabs.org/gp.
Definition: Ports.h:534
uint8_t digiRead2() const
Reads the value of a Port's A pin.
Definition: Ports.h:119
static void watchdogEvent()
This must be called from your watchdog interrupt code.
Definition: Ports.cpp:1172
void begin()
Initialize the LuxPlug. Wait at least 1000 ms after calling this!
Definition: Ports.h:498
Interface for the Modern Device 3-axis Compass board.
Definition: Ports.h:619
void poweroff()
Power down the lux plug for low power usage.
Definition: Ports.h:506
void anaWrite(uint8_t val) const
Writes a PWM value to a Port's D pin.
Definition: Ports.h:100
uint8_t receive() const
Create a start condition on the I2C bus, and set things up for receiving data from this device...
Definition: Ports.h:284
Interface for the Lux Plug - see https://jeelabs.org/xp.
Definition: Ports.h:485
bool isPresent() const
see if a device answers at an I2C address
Definition: Ports.cpp:234
static void powerDown()
enter low-power mode, wake up with watchdog, INT0/1, or pin-change
Definition: Ports.cpp:1120
Interface for the Proximity Plug - see https://jeelabs.org/yp.
Definition: Ports.h:628
void poll()
Call this continuously or at least right after a pin change.
Definition: Ports.cpp:831
void digiWrite(uint8_t value) const
Write High or Low to a Port's D pin.
Definition: Ports.h:97
Scheduler(byte max)
initialize for a specified maximum number of tasks
Definition: Ports.cpp:1176
void shiftWrite(uint8_t bitOrder, uint16_t value, uint8_t count=8) const
The shiftWrite() call is similar but more general than the shift() call in that it allows an adjustab...
Definition: Ports.cpp:115
void ledOff(byte mask)
Turn off the corresponding leds.
Definition: Ports.cpp:289
Can be used to drive a software (bit-banged) I2C bus via a Port interface.
Definition: Ports.h:213
GravityPlug(PortI2C &port)
Constructor for Gravity Plug.
Definition: Ports.h:539
void shift(uint8_t bitOrder, uint8_t value) const
Does Arduino shiftOut() with data on D and clock on A pin of the Port.
Definition: Ports.h:153
void stop() const
Terminate transmission on an I2C connection.
Definition: Ports.cpp:197
byte idle(byte task)
return true if a task timer is not running
Definition: Ports.h:389
These objects represent remote nodes connected via wireless.
Definition: Ports.h:161
static byte loseSomeTime(word msecs)
Spend some time in low-power mode, the timing is only approximate.
Definition: Ports.cpp:1138
long reading()
Read out 4 bytes, caller will need to shift out the irrelevant lower bits.
Definition: Ports.cpp:946
static void anaWrite3(uint8_t val)
Writes a PWM value to the I pin of all Ports.
Definition: Ports.h:147
uint8_t write(uint8_t data) const
Write a byte to the currently addressed device.
Definition: Ports.h:293
A remote port is like a local port, bot connected to a remote node.
Definition: Ports.h:190
static void watchdogInterrupts(char mode)
start the watchdog timer (or disable it if mode < 0)
Definition: Ports.cpp:1102
uint8_t done()
Returns number of nibbles read, or 0 if not yet ready.
Definition: Ports.cpp:859
byte ledState() const
Definition: Ports.h:408
Interface for the Infrared Plug - see https://jeelabs.org/ir.
Definition: Ports.h:566
byte poll(word ms=0)
poll until the timer fires
Definition: Ports.cpp:240
uint8_t anaPin() const
Definition: Ports.h:76
void sensitivity(byte range, word bw=0)
Set GravityPlug sensitivity.
Definition: Ports.cpp:614
const uint8_t * buffer()
Access to the receive buffer.
Definition: Ports.h:589
void configure(uint8_t slot4, uint8_t gap256=80)
Set slot size (us*4) and end-of-data gap (us*256)
Definition: Ports.cpp:825
uint16_t anaRead() const
Reads an analog value from a Port's A pin.
Definition: Ports.h:115
const int * getAxes()
Get accelleration data from GravityPlug.
Definition: Ports.cpp:628
Each device on the I2C bus needs to be defined using a DeviceI2C instance.
Definition: Ports.h:266
static uint8_t digiRead3()
Reads the value of the I pin on all Ports.
Definition: Ports.h:140
A memory stream can save and reload a stream of bytes on a MemoryPlug.
Definition: Ports.h:427
uint8_t digiPin2() const
Definition: Ports.h:70
void timer(byte task, word tenths)
set a task timer, in tenths of seconds
Definition: Ports.cpp:1236
Interface for the Memory Plug - see https://jeelabs.org/mp.
Definition: Ports.h:416
BlinkPlug(byte port)
Constructor for the BlinkPlug class.
Definition: Ports.h:402
void begin()
Setup GravityPlug. Call during setup()
Definition: Ports.h:542
char poll()
Return next task to run, -1 if there are none ready to run, but there are tasks waiting, or -2 if there are no tasks waiting (i.e.
Definition: Ports.cpp:1187
Port(uint8_t num)
Contructor for a Port.
Definition: Ports.h:82
char temperature()
Read out the temperature (only for BMA150, not the older BMA020)
Definition: Ports.cpp:644
Interface for the UART Plug - see https://jeelabs.org/up.
Definition: Ports.h:444
uint8_t portNum
The port number is a small integer mathing the hardware port used.
Definition: Ports.h:37
Interface for the Blink Plug - see https://jeelabs.org/bp.
Definition: Ports.h:393
static uint8_t digiPin3()
Definition: Ports.h:73
void stop() const
Create a stop condition on the I2C bus, ending the current transfer.
Definition: Ports.h:287
void mode(uint8_t value) const
Set the pin mode of a Port's D pin.
Definition: Ports.h:89
InfraredPlug(uint8_t num)
Initialize with default values for NEC protocol.
Definition: Ports.cpp:817
byte available()
Test if UartPlug has incoming data.
Definition: Ports.cpp:470
uint8_t send() const
Create a start condition on the I2C bus, and set things up for sending data to this device...
Definition: Ports.h:279
uint8_t digiPin() const
Definition: Ports.h:67
byte idle() const
Returns true if the timer is not armed.
Definition: Ports.h:325
virtual WRITE_RESULT write(byte)
Write data on the serial port of the UartPlug.
Definition: Ports.cpp:503
uint32_t pulse2(uint8_t state, uint32_t timeout=1000000L) const
Applies the Arduino pulseIn() function on a Port's A pin.
Definition: Ports.h:127
uint8_t read(uint8_t last) const
Read a byte using I2C protocol on a JeeNode port.
Definition: Ports.cpp:217
uint8_t read(uint8_t last) const
Read a byte from the currently addressed device.
Definition: Ports.h:300
void begin(long)
Initialize a UartPlug.
Definition: Ports.cpp:457
byte state()
Read entire BlinkPlug state.
Definition: Ports.cpp:306
uint8_t digiRead() const
Reads the value of a Port's D pin.
Definition: Ports.h:93
uint32_t pulse(uint8_t state, uint32_t timeout=1000000L) const
Applies the Arduino pulseIn() function on a Port's D pin.
Definition: Ports.h:103
byte pushed()
Definition: Ports.cpp:315
void flush()
Clear the RX and TX queues.
Definition: Ports.cpp:494
int read()
Read two bytes from the UartPlug's serial input.
Definition: Ports.cpp:489
Interface for the Heading Board - see https://jeelabs.org/hb.
Definition: Ports.h:596
void select(byte channel)
Select channel (1..4), must wait to read it out (up to 270 ms for 18-bit)
Definition: Ports.cpp:940
void ledOn(byte mask)
Turn on the corresponding leds.
Definition: Ports.cpp:273
Interface for the Analog Plug - see https://jeelabs.org/ap.
Definition: Ports.h:647
void digiWrite2(uint8_t value) const
Write High or Low to a Port's A pin.
Definition: Ports.h:123
word remaining() const
Return the number of milliseconds before the timer will fire.
Definition: Ports.cpp:258
Interface for the DHT11 and DHT22 sensors, does not use floating point.
Definition: Ports.h:662