+//
+// Chezak Node - Jeelabs official Room Board (http://jeelabs.com/products/room-board)
+//
+// Original sketch: https://github.com/jcw/jeelib/blob/master/examples/RF12/roomNode/roomNode.ino
+//
+// Sensors:
+//  - SHT11 for temperature and humidity
+//  - LDR for light
+//  - PIR for motion
+//
+
 /// @dir roomNode
 /// New version of the Room Node (derived from rooms.pde).
 // 2010-10-19 <jc@wippler.nl> http://opensource.org/licenses/mit-license.php

}

void setup () {
    #if SERIAL || DEBUG
        Serial.begin(57600);
        Serial.print("\n[roomNode.3]");
        myNodeID = rf12_config();
        serialFlush();
    #else
        myNodeID = rf12_config(0); // don't report info on the serial port
    #endif

    rf12_sleep(RF12_SLEEP); // power down

    #if PIR_PORT
        pir.digiWrite(PIR_PULLUP);
#ifdef PCMSK2
        bitSet(PCMSK2, PIR_PORT + 3);
        bitSet(PCICR, PCIE2);
#else
        //XXX TINY!
#endif
    #endif

    reportCount = REPORT_EVERY;     // report right away for easy debugging
    scheduler.timer(MEASURE, 0);    // start the measurement loop going
}

void loop () {
    #if DEBUG
        Serial.print('.');
        serialFlush();
    #endif

    #if PIR_PORT
        if (pir.triggered()) {
            payload.moved = pir.state();
            doTrigger();
        }
    #endif

    switch (scheduler.pollWaiting()) {

        case MEASURE:
            // reschedule these measurements periodically
            scheduler.timer(MEASURE, MEASURE_PERIOD);

#endif

#if PIR_PORT
    #define PIR_PULLUP   1 // set to one to pull-up the PIR input pin
    #define PIR_INVERTED 0 // 0 or 1, to match PIR reporting high or low

    /// Interface to a Passive Infrared motion sensor.
    class PIR : public Port {
        volatile byte value, changed;

    public:
        PIR (byte portnum)
            : Port (portnum), value (0), changed (0) {}

        // this code is called from the pin-change interrupt handler
        void poll() {
            // see http://talk.jeelabs.net/topic/811#post-4734 for PIR_INVERTED
            byte pin = digiRead() ^ PIR_INVERTED;

            // if the pin changed then set the changed flag to report it
            if (pin != state())
                changed = 1;

            value = pin;
        }

        // state is true if curr value is still on or if it was on recently
        byte state() const {
            byte f = value;
            return f;
        }

