Browse Source

- added possibility to be notified of individual bits received

- fixed bug when sending individual bits or canceling a write operation
- DS18B20 sample now sends zeros during conversion, and ones when conversion is finished
timer1
Youen Toupin 9 years ago
parent
commit
887741a806
  1. 3
      OneWireIO.ino
  2. 14
      OneWireSlave.cpp
  3. 4
      OneWireSlave.h

3
OneWireIO.ino

@ -18,7 +18,6 @@ const byte DS18B20_WRITE_SCRATCHPAD = 0x4E;
// TODO:
// - handle configuration (resolution, alarms)
// - send 0 bits while conversion is in progress, 1 bits when it's done (until reset)
enum DeviceState
{
@ -73,6 +72,7 @@ void loop()
cli();
memcpy((void*)scratchpad, data, 9);
state = DS_TemperatureConverted;
OWSlave.writeBit(1, true); // now that conversion is finished, start sending ones until reset
sei();
}
}
@ -90,6 +90,7 @@ void owReceive(OneWireSlave::ReceiveEvent evt, byte data)
case DS18B20_START_CONVERSION:
state = DS_ConvertingTemperature;
conversionStartTime = millis();
OWSlave.writeBit(0, true); // send zeros as long as the conversion is not finished
break;
case DS18B20_READ_SCRATCHPAD:

14
OneWireSlave.cpp

@ -39,6 +39,7 @@ unsigned long OneWireSlave::lastReset_;
void(*OneWireSlave::receiveBitCallback_)(bool bit, bool error);
void(*OneWireSlave::bitSentCallback_)(bool error);
void(*OneWireSlave::clientReceiveCallback_)(ReceiveEvent evt, byte data);
void(*OneWireSlave::clientReceiveBitCallback_)(bool bit);
byte OneWireSlave::receivingByte_;
@ -54,6 +55,10 @@ short OneWireSlave::bufferPos_;
void(*OneWireSlave::receiveBytesCallback_)(bool error);
void(*OneWireSlave::sendBytesCallback_)(bool error);
bool OneWireSlave::singleBit_;
bool OneWireSlave::singleBitRepeat_;
void(*OneWireSlave::singleBitSentCallback_)(bool error);
ISR(TIMER1_COMPA_vect) // timer1 interrupt
{
@ -72,6 +77,8 @@ void OneWireSlave::begin(const byte* rom, byte pinNumber)
memcpy(rom_, rom, 7);
rom_[7] = crc8(rom_, 7);
clientReceiveBitCallback_ = 0;
// log("Enabling 1-wire library")
cli(); // disable interrupts
@ -125,7 +132,7 @@ void OneWireSlave::onSingleBitSent_(bool error)
}
else
{
beginResetDetection_();
beginReceiveBytes_(scratchpad_, 1, &OneWireSlave::notifyClientByteReceived_);
}
if (singleBitSentCallback_ != 0)
@ -380,6 +387,9 @@ void OneWireSlave::onBitReceived_(bool bit, bool error)
receivingByte_ |= ((bit ? 1 : 0) << bufferBitPos_);
++bufferBitPos_;
if (clientReceiveBitCallback_ != 0 && bufferPos_ != ReceiveCommand)
clientReceiveBitCallback_(bit);
if (bufferBitPos_ == 8)
{
// log("received byte", (long)receivingByte_);
@ -515,7 +525,7 @@ void OneWireSlave::beginWriteBytes_(const byte* data, short numBytes, void(*comp
}
else
{
beginResetDetection_();
beginReceiveBytes_(scratchpad_, 1, &OneWireSlave::notifyClientByteReceived_);
}
}

4
OneWireSlave.h

@ -23,6 +23,9 @@ public:
//! Sets (or replaces) a function to be called when something is received. The callback is executed from interrupts and should be as short as possible. Failure to return quickly can prevent the library from correctly reading the next byte.
void setReceiveCallback(void(*callback)(ReceiveEvent evt, byte data)) { clientReceiveCallback_ = callback; }
//! Sets (or replaces) a function to be called when a bit is received. The byte reception callback is called after that if the received bit was the last of a byte. The callback is executed from interrupts and should be as short as possible. Failure to return quickly can prevent the library from correctly reading the next bit.
void setReceiveBitCallback(void(*callback)(bool bit)) { clientReceiveBitCallback_ = 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. If bytes is null or numBytes is 0, nothing is sent, which is equivalent to calling stopWrite. In any case, calling the write function will cancel the previous write operation if it didn't complete yet.
void write(const byte* bytes, short numBytes, void(*complete)(bool error));
@ -113,6 +116,7 @@ private:
static void onSingleBitSent_(bool error);
static void(*clientReceiveCallback_)(ReceiveEvent evt, byte data);
static void(*clientReceiveBitCallback_)(bool bit);
};
extern OneWireSlave OWSlave;

Loading…
Cancel
Save