From 4d94761fd5939d06dd6c8d44d9a99389a97ba42e Mon Sep 17 00:00:00 2001 From: Youen Toupin Date: Mon, 27 Apr 2015 23:17:45 +0200 Subject: [PATCH] - implemented simple protocol to turn an output on/off - disabled debug code --- OneWireIO.ino | 45 ++++++++++++--------------------------------- OneWireSlave.cpp | 2 +- SerialChannel.cpp | 3 +++ SerialChannel.h | 4 ++++ 4 files changed, 20 insertions(+), 34 deletions(-) diff --git a/OneWireIO.ino b/OneWireIO.ino index b3a9b16..e3abba5 100644 --- a/OneWireIO.ino +++ b/OneWireIO.ino @@ -6,18 +6,15 @@ #define LEDPin 13 #define OWPin 2 +#ifdef ENABLE_SERIAL_CHANNEL SerialChannel debug("debug"); +#endif Pin led(LEDPin); byte owROM[7] = { 0xE2, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02 }; -byte buffer1[8]; -byte buffer2[8]; -volatile byte* backBuffer = buffer1; -volatile byte bufferPos = 0; - -byte sendBuffer[8]; +byte acknowledge = 0x42; void owReceive(OneWireSlave::ReceiveEvent evt, byte data); @@ -32,41 +29,19 @@ void setup() Serial.begin(9600); } -int count = 0; void loop() { delay(1); - if (count++ == 1000) - { - led.write(!led.read()); - count = 0; - } cli();//disable interrupts + #ifdef ENABLE_SERIAL_CHANNEL SerialChannel::swap(); - byte* frontBuffer = (byte*)backBuffer; - byte frontBufferSize = bufferPos; - backBuffer = backBuffer == buffer1 ? buffer2 : buffer1; - bufferPos = 0; + #endif sei();//enable interrupts + #ifdef ENABLE_SERIAL_CHANNEL SerialChannel::flush(); - - for (int i = 0; i < frontBufferSize; ++i) - { - char msg[16]; - sprintf(msg, "Received byte: %d", (int)frontBuffer[i]); - debug.write(msg); - - if (frontBuffer[i] == 0x42) - { - sendBuffer[0] = 0xBA; - sendBuffer[1] = 0xAD; - sendBuffer[2] = 0xF0; - sendBuffer[3] = 0x0D; - OneWire.write(sendBuffer, 4, 0); - } - } + #endif } void owReceive(OneWireSlave::ReceiveEvent evt, byte data) @@ -74,7 +49,11 @@ void owReceive(OneWireSlave::ReceiveEvent evt, byte data) switch (evt) { case OneWireSlave::RE_Byte: - backBuffer[bufferPos++] = data; + if (data == 0x01) + led.writeHigh(); + else if (data == 0x02) + led.writeLow(); + OneWire.write(&acknowledge, 1, 0); break; default: ; diff --git a/OneWireSlave.cpp b/OneWireSlave.cpp index ec1c55d..d4d05b5 100644 --- a/OneWireSlave.cpp +++ b/OneWireSlave.cpp @@ -1,6 +1,6 @@ #include "OneWireSlave.h" -#define DEBUG_LOG +//#define DEBUG_LOG #define ERROR_MESSAGES #ifdef DEBUG_LOG diff --git a/SerialChannel.cpp b/SerialChannel.cpp index 63537b6..951f390 100644 --- a/SerialChannel.cpp +++ b/SerialChannel.cpp @@ -1,6 +1,8 @@ #include "Arduino.h" #include "SerialChannel.h" +#ifdef ENABLE_SERIAL_CHANNEL + byte SerialChannel::nextId = 1; SerialChannel* SerialChannel::first = 0; @@ -165,3 +167,4 @@ void SerialChannel::handleConnection() } } +#endif //ENABLE_SERIAL_CHANNEL diff --git a/SerialChannel.h b/SerialChannel.h index 53e1bd3..2da7bf1 100644 --- a/SerialChannel.h +++ b/SerialChannel.h @@ -1,6 +1,9 @@ #ifndef _SerialChannel_h_ #define _SerialChannel_h_ +//#define ENABLE_SERIAL_CHANNEL + +#ifdef ENABLE_SERIAL_CHANNEL #define SC_APPEND_STR(str) append((byte*)str, sizeof(str)-1) #define SC_APPEND_STR_INT(str, arg0) appendInt(str, sizeof(str)-1, arg0) @@ -55,6 +58,7 @@ private: static void writeShort(short num); static void writeULong(unsigned long num); }; +#endif // ENABLE_SERIAL_CHANNEL #endif