Browse Source

- methods that don't write memory now take const parameters

- simplified pin number management
timer1
Youen Toupin 10 years ago
parent
commit
216fcf8cb5
  1. 5
      LowLevel.h
  2. 12
      OneWireIO_Demo.ino
  3. 21
      OneWireSlave.cpp
  4. 11
      OneWireSlave.h

5
LowLevel.h

@ -74,16 +74,19 @@ private:
volatile IO_REG_TYPE *reg_;
IO_REG_TYPE mask_;
byte interruptNumber_;
byte pinNumber_;
public:
Pin()
: mask_(0)
, reg_(0)
, interruptNumber_((byte)-1)
, pinNumber_(255)
{ }
Pin(uint8_t pin)
{
pinNumber_ = pin;
mask_ = PIN_TO_BITMASK(pin);
reg_ = PIN_TO_BASEREG(pin);
@ -95,6 +98,8 @@ public:
}
}
inline byte getPinNumber() { return pinNumber_; }
inline void inputMode() { DIRECT_MODE_INPUT(reg_, mask_); }
inline void outputMode() { DIRECT_MODE_OUTPUT(reg_, mask_); }

12
OneWireIO_Demo.ino

@ -2,18 +2,16 @@
#include "LowLevel.h"
#include "OneWireSlave.h"
#define LEDPin 13
// This is the pin that will be used for one-wire data (depending on your arduino model, you are limited to a few choices, because some pins don't have complete interrupt support)
// On Arduino Uno, you can use pin 2 or pin 3
#define OWPin 2
Pin oneWireData(2);
Pin led(LEDPin);
Pin led(13);
// This is the ROM the arduino will respond to, make sure it doesn't conflict with another device
byte owROM[7] = { 0xE2, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02 };
const byte owROM[7] = { 0xE2, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02 };
byte acknowledge = 0x42;
const byte acknowledge = 0x42;
// This sample implements a simple protocol : sending match ROM, then the ROM, then 0x01 will turn the arduino light on. Sending 0x02 will turn it off. In each case, the byte 0x42 is sent as acknowledgement.
const byte CMD_TurnOn = 0x01;
@ -29,7 +27,7 @@ void setup()
// Setup the OneWire library
OneWire.setReceiveCallback(&owReceive);
OneWire.begin(owROM, OWPin);
OneWire.begin(owROM, oneWireData.getPinNumber());
}
void loop()

21
OneWireSlave.cpp

@ -46,7 +46,8 @@ byte OneWireSlave::searchRomBytePos_;
byte OneWireSlave::searchRomBitPos_;
bool OneWireSlave::searchRomInverse_;
byte* OneWireSlave::buffer_;
const byte* OneWireSlave::sendBuffer_;
byte* OneWireSlave::recvBuffer_;
short OneWireSlave::bufferLength_;
byte OneWireSlave::bufferBitPos_;
short OneWireSlave::bufferPos_;
@ -62,7 +63,7 @@ ISR(TIMER1_COMPA_vect) // timer1 interrupt
event();
}
void OneWireSlave::begin(byte* rom, byte pinNumber)
void OneWireSlave::begin(const byte* rom, byte pinNumber)
{
pin_ = Pin(pinNumber);
resetStart_ = (unsigned long)-1;
@ -99,14 +100,14 @@ void OneWireSlave::end()
sei();
}
void OneWireSlave::write(byte* bytes, short numBytes, void(*complete)(bool error))
void OneWireSlave::write(const byte* bytes, short numBytes, void(*complete)(bool error))
{
cli();
beginWriteBytes_(bytes, numBytes, complete == 0 ? noOpCallback_ : complete);
sei();
}
byte OneWireSlave::crc8(byte* data, short numBytes)
byte OneWireSlave::crc8(const byte* data, short numBytes)
{
byte crc = 0;
@ -377,7 +378,7 @@ void OneWireSlave::onBitReceived_(bool bit, bool error)
}
else
{
buffer_[bufferPos_++] = receivingByte_;
recvBuffer_[bufferPos_++] = receivingByte_;
receivingByte_ = 0;
bufferBitPos_ = 0;
if (bufferPos_ == bufferLength_)
@ -467,15 +468,15 @@ void OneWireSlave::searchRomOnBitReceived_(bool bit, bool error)
}
}
void OneWireSlave::beginWriteBytes_(byte* data, short numBytes, void(*complete)(bool error))
void OneWireSlave::beginWriteBytes_(const byte* data, short numBytes, void(*complete)(bool error))
{
buffer_ = data;
sendBuffer_ = data;
bufferLength_ = numBytes;
bufferPos_ = 0;
bufferBitPos_ = 0;
sendBytesCallback_ = complete;
bool bit = bitRead(buffer_[0], 0);
bool bit = bitRead(sendBuffer_[0], 0);
beginSendBit_(bit, &OneWireSlave::bitSent_);
}
@ -502,13 +503,13 @@ void OneWireSlave::bitSent_(bool error)
return;
}
bool bit = bitRead(buffer_[bufferPos_], bufferBitPos_);
bool bit = bitRead(sendBuffer_[bufferPos_], bufferBitPos_);
beginSendBit_(bit, &OneWireSlave::bitSent_);
}
void OneWireSlave::beginReceiveBytes_(byte* buffer, short numBytes, void(*complete)(bool error))
{
buffer_ = buffer;
recvBuffer_ = buffer;
bufferLength_ = numBytes;
bufferPos_ = 0;
receiveBytesCallback_ = complete;

11
OneWireSlave.h

@ -15,7 +15,7 @@ public:
};
//! Starts listening for the 1-wire master, on the specified pin, as a virtual slave device identified by the specified ROM (7 bytes, starting from the family code, CRC will be computed internally). Reset, Presence, SearchRom and MatchRom are handled automatically. The library will use the external interrupt on the specified pin (note that this is usually not possible with all pins, depending on the board), as well as one hardware timer. Blocking interrupts (either by disabling them explicitely with sei/cli, or by spending time in another interrupt) can lead to malfunction of the library, due to tight timing for some 1-wire operations.
void begin(byte* rom, byte pinNumber);
void begin(const byte* rom, byte pinNumber);
//! Stops all 1-wire activities, which frees hardware resources for other purposes.
void end();
@ -24,9 +24,9 @@ public:
void setReceiveCallback(void(*callback)(ReceiveEvent evt, byte data)) { clientReceiveCallback_ = callback; }
//! Enqueues the specified bytes in the send buffer. They will be sent in the background. The optional callback is used to notify when the bytes are sent, or if an error occured. Callbacks are executed from interrupts and should be as short as possible.
void write(byte* bytes, short numBytes, void(*complete)(bool error));
void write(const byte* bytes, short numBytes, void(*complete)(bool error));
static byte crc8(byte* data, short numBytes);
static byte crc8(const byte* data, short numBytes);
private:
static void setTimerEvent_(short delayMicroSeconds, void(*handler)());
@ -56,7 +56,7 @@ private:
static void continueSearchRom_(bool error);
static void searchRomOnBitReceived_(bool bit, bool error);
static void beginWriteBytes_(byte* data, short numBytes, void(*complete)(bool error));
static void beginWriteBytes_(const byte* data, short numBytes, void(*complete)(bool error));
static void beginReceiveBytes_(byte* buffer, short numBytes, void(*complete)(bool error));
static void noOpCallback_(bool error);
@ -93,7 +93,8 @@ private:
static byte searchRomBitPos_;
static bool searchRomInverse_;
static byte* buffer_;
static const byte* sendBuffer_;
static byte* recvBuffer_;
static short bufferLength_;
static short bufferPos_;
static byte bufferBitPos_;

Loading…
Cancel
Save