Browse Source

- implemented simple protocol to turn an output on/off

- disabled debug code
timer1
Youen Toupin 10 years ago
parent
commit
4d94761fd5
  1. 45
      OneWireIO.ino
  2. 2
      OneWireSlave.cpp
  3. 3
      SerialChannel.cpp
  4. 4
      SerialChannel.h

45
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:
;

2
OneWireSlave.cpp

@ -1,6 +1,6 @@
#include "OneWireSlave.h"
#define DEBUG_LOG
//#define DEBUG_LOG
#define ERROR_MESSAGES
#ifdef DEBUG_LOG

3
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

4
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

Loading…
Cancel
Save