forked from youen/OneWireArduinoSlave
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
99 lines
3.6 KiB
99 lines
3.6 KiB
#ifndef _OneWireSlave_h_ |
|
#define _OneWireSlave_h_ |
|
|
|
#include "Arduino.h" |
|
#include "LowLevel.h" |
|
|
|
class OneWireSlave |
|
{ |
|
public: |
|
///! Constructs a 1-wire slave that will be identified by the specified ROM (7 bytes, starting from the family code, CRC will be computed internally). Call enable to actually start listening for the 1-wire master. |
|
OneWireSlave(byte* rom, byte pinNumber); |
|
|
|
///! Starts listening for the 1-wire master. Reset, Presence and SearchRom are handled automatically. The library will use interrupts on the pin specified in the constructor, 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 enable(); |
|
|
|
///! Stops all 1-wire activities, which frees hardware resources for other purposes. |
|
void disable(); |
|
|
|
///! Pops one byte from the receive buffer, or returns false if no byte has been received. |
|
bool read(byte& b); |
|
|
|
///! Sets (or replaces) a function to be called when at least one byte has been received. Callbacks are executed from interrupts and should be as short as possible. |
|
void setReceiveCallback(void(*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)); |
|
|
|
private: |
|
byte crc8_(byte* data, short numBytes); |
|
|
|
void setTimerEvent_(short delayMicroSeconds, void(*handler)()); |
|
void disableTimer_(); |
|
|
|
void onEnterInterrupt_(); |
|
void onLeaveInterrupt_(); |
|
|
|
void error_(const char* message); |
|
|
|
void pullLow_(); |
|
void releaseBus_(); |
|
|
|
void beginWaitReset_(); |
|
void beginWaitCommand_(); |
|
void beginReceive_(); |
|
|
|
void beginReceiveBit_(void(*completeCallback)(bool bit, bool error)); |
|
void beginSendBit_(bool bit, void(*completeCallback)(bool error)); |
|
|
|
void beginSearchRom_(); |
|
void beginSearchRomSendBit_(); |
|
inline static void continueSearchRomHandler_(bool error) { inst_->continueSearchRom_(error); } |
|
void continueSearchRom_(bool error); |
|
inline static void searchRomOnBitReceivedHandler_(bool bit, bool error) { inst_->searchRomOnBitReceived_(bit, error); } |
|
void searchRomOnBitReceived_(bool bit, bool error); |
|
|
|
// interrupt handlers |
|
inline static void waitResetHandler_() { inst_->waitReset_(); } |
|
void waitReset_(); |
|
inline static void beginPresenceHandler_() { inst_->beginPresence_(); } |
|
void beginPresence_(); |
|
inline static void endPresenceHandler_() { inst_->endPresence_(); } |
|
void endPresence_(); |
|
inline static void receiveHandler_() { inst_->receive_(); } |
|
void receive_(); |
|
inline static void readBitHandler_() { inst_->readBit_(); } |
|
void readBit_(); |
|
inline static void onBitReceivedHandler_(bool bit, bool error) { inst_->onBitReceived_(bit, error); } |
|
void onBitReceived_(bool bit, bool error); |
|
inline static void sendBitOneHandler_() { inst_->sendBitOne_(); } |
|
void sendBitOne_(); |
|
inline static void sendBitZeroHandler_() { inst_->sendBitZero_(); } |
|
void sendBitZero_(); |
|
inline static void endSendBitZeroHandler_() { inst_->endSendBitZero_(); } |
|
void endSendBitZero_(); |
|
|
|
private: |
|
static OneWireSlave* inst_; |
|
byte rom_[8]; |
|
Pin pin_; |
|
byte tccr1bEnable_; |
|
|
|
unsigned long resetStart_; |
|
unsigned long lastReset_; |
|
|
|
void(*receiveBitCallback_)(bool bit, bool error); |
|
void(*bitSentCallback_)(bool error); |
|
|
|
byte receivingByte_; |
|
byte receivingBitPos_; |
|
byte receiveTarget_; |
|
|
|
bool ignoreNextEdge_; |
|
|
|
byte searchRomBytePos_; |
|
byte searchRomBitPos_; |
|
bool searchRomInverse_; |
|
}; |
|
|
|
#endif
|
|
|