 //
 // With thanks for the inspiration by 2006 David A. Mellis and his AFSoftSerial code
 //  All right reserved.
-// Connect Tiny84 PA2 to USB-BUB TXD for serial input to sketch.    // Jeenode DIO2
+// Connect Tiny84 PA2 to USB-BUB TXD for serial input to sketch.        // Jeenode DIO2
 // 9600 or 38400 at present.
 
 #if SERIAL_BAUD == 9600
-#define BITDELAY 54      // 9k6 @ 8MHz, 19k2 @16MHz
+#define BITDELAY 54          // 9k6 @ 8MHz, 19k2 @16MHz
 #endif
 #if SERIAL_BAUD == 38400
-#define BITDELAY 11     // 38k4 @ 8MHz, 76k8 @16MHz
+#define BITDELAY 11         // 38k4 @ 8MHz, 76k8 @16MHz
 #endif
 
 #define MAX_NODES 14

static byte stack[RF12_MAXDATA+4], top, sendLen, dest;
static byte testbuf[RF12_MAXDATA], testCounter;

static byte nodes[MAX_NODES + 1];  // [0] is unused
static byte postingsIn, postingsOut;

static void showNibble (byte nibble) {
  char c = '0' + (nibble & 0x0F);
  if (c > '9')
    c += 7;
  Serial.print(c);
}

static void showByte (byte value) {
  if (config.hex_output) {
    showNibble(value >> 4);
    showNibble(value);
  } else
    Serial.print((int) value);
}

static void loadConfig () {
  eeprom_read_block(&config, RF12_EEPROM_ADDR, sizeof config);
}

static void saveConfig () {
  config.format = MAJOR_VERSION;
  config.crc = ~0;
  for (byte i = 0; i < sizeof config - 2; ++i)
    config.crc = _crc16_update(config.crc, ((byte*) &config)[i]);

  eeprom_write_block(&config, RF12_EEPROM_ADDR, sizeof config);
  Serial.println(sizeof config);

  if (!rf12_config())
     showString(INITFAIL);
}

static byte bandToFreq (byte band) {
   return band == 4 ? RF12_433MHZ : band == 8 ? RF12_868MHZ : band == 9 ? RF12_915MHZ : 0;
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

// for 25 us SPI overhead to end up with the proper on-the-air pulse widths.
// With thanks to JGJ Veken for his help in getting these values right.
static void ookPulse(int on, int off) {
    rf12_onOff(1);
    delayMicroseconds(on + 150);
    rf12_onOff(0);
    delayMicroseconds(off - 200);
}

static void fs20sendBits(word data, byte bits) {
    if (bits == 8) {
        ++bits;
        data = (data << 1) | parity_even_bit(data);
    }
    for (word mask = bit(bits-1); mask != 0; mask >>= 1) {
        int width = data & mask ? 600 : 400;
        ookPulse(width, width);
    }
}

static void fs20cmd(word house, byte addr, byte cmd) {
    byte sum = 6 + (house >> 8) + house + addr + cmd;
    for (byte i = 0; i < 3; ++i) {
        fs20sendBits(1, 13);
        fs20sendBits(house >> 8, 8);
        fs20sendBits(house, 8);
        fs20sendBits(addr, 8);
        fs20sendBits(cmd, 8);
        fs20sendBits(sum, 8);
        fs20sendBits(0, 1);
        delay(10);
    }
}

static void kakuSend(char addr, byte device, byte on) {
    int cmd = 0x600 | ((device - 1) << 4) | ((addr - 1) & 0xF);
    if (on)
        cmd |= 0x800;
    for (byte i = 0; i < 4; ++i) {
        for (byte bit = 0; bit < 12; ++bit) {
            ookPulse(375, 1125);
            int on = bitRead(cmd, bit) ? 1125 : 375;
            ookPulse(on, 1500 - on);
        }
        ookPulse(375, 375);
        delay(11); // approximate
    }
}

#endif
