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.
148 lines
6.6 KiB
148 lines
6.6 KiB
10 years ago
|
#ifndef _OneWireSlave_h_
|
||
|
#define _OneWireSlave_h_
|
||
|
|
||
|
#include "Arduino.h"
|
||
10 years ago
|
#include "LowLevel.h"
|
||
10 years ago
|
|
||
|
class OneWireSlave
|
||
|
{
|
||
|
public:
|
||
10 years ago
|
enum ReceiveEvent
|
||
|
{
|
||
|
RE_Reset, //!< The master has sent a general reset
|
||
|
RE_Byte, //!< The master just sent a byte of data
|
||
|
RE_Error //!< A communication error happened (such as a timeout) ; the library will stop all 1-wire activities until the next reset
|
||
|
};
|
||
|
|
||
10 years ago
|
//! 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.
|
||
9 years ago
|
void begin(const byte* rom, byte pinNumber);
|
||
10 years ago
|
|
||
10 years ago
|
//! Stops all 1-wire activities, which frees hardware resources for other purposes.
|
||
10 years ago
|
void end();
|
||
10 years ago
|
|
||
10 years ago
|
//! 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; }
|
||
10 years ago
|
|
||
9 years ago
|
//! 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; }
|
||
|
|
||
9 years ago
|
//! Sets (or replaces) a function to be called when the library has a message to log, if the functionality is enabled in OneWireSlave.cpp. This is for debugging purposes.
|
||
|
void setLogCallback(void(*callback)(const char* message)) { logCallback_ = callback; }
|
||
|
|
||
9 years ago
|
//! Writes the specified bytes synchronously. This function blocks until the write operation has finished. Do not call from an interrupt handler! Returns true in case of success, false if an error occurs.
|
||
9 years ago
|
bool write(const byte* bytes, short numBytes);
|
||
|
|
||
|
//! Starts sending the specified bytes. They will be sent in the background, and the buffer must remain valid and unchanged until the write operation has finished or is cancelled. 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 beginWrite(const byte* bytes, short numBytes, void(*complete)(bool error));
|
||
10 years ago
|
|
||
9 years ago
|
//! Writes a single bit synchronously. This function blocks until the bit is sent. Do not call from an interrupt handler! Returns true in case of success, false if an error occurs.
|
||
|
bool writeBit(bool value);
|
||
|
|
||
9 years ago
|
//! Sets a bit that will be sent next time the master asks for one. Optionnaly, the repeat parameter can be set to true to continue sending the same bit each time. In both cases, the send operation can be canceled by calling stopWrite.
|
||
9 years ago
|
void beginWriteBit(bool value, bool repeat = false, void(*bitSent)(bool error) = 0);
|
||
9 years ago
|
|
||
|
//! Cancels any pending write operation, started by writeBit or write. If this function is called before the master asked for a bit, then nothing is sent to the master.
|
||
|
void stopWrite();
|
||
|
|
||
9 years ago
|
void alarmed(bool value);
|
||
|
|
||
9 years ago
|
static byte crc8(const byte* data, short numBytes);
|
||
10 years ago
|
|
||
9 years ago
|
private:
|
||
10 years ago
|
static void setTimerEvent_(short delayMicroSeconds, void(*handler)());
|
||
|
static void disableTimer_();
|
||
10 years ago
|
|
||
10 years ago
|
static void onEnterInterrupt_();
|
||
|
static void onLeaveInterrupt_();
|
||
10 years ago
|
|
||
10 years ago
|
static void error_(const char* message);
|
||
10 years ago
|
|
||
10 years ago
|
static void pullLow_();
|
||
|
static void releaseBus_();
|
||
10 years ago
|
|
||
10 years ago
|
static void beginReceiveBit_(void(*completeCallback)(bool bit, bool error));
|
||
|
static void beginSendBit_(bool bit, void(*completeCallback)(bool error));
|
||
10 years ago
|
|
||
10 years ago
|
static void beginResetDetection_();
|
||
9 years ago
|
static void beginResetDetectionSendZero_();
|
||
10 years ago
|
static void cancelResetDetection_();
|
||
|
|
||
10 years ago
|
static void beginWaitReset_();
|
||
|
static void beginWaitCommand_();
|
||
|
static void beginReceive_();
|
||
|
static void onBitReceived_(bool bit, bool error);
|
||
10 years ago
|
|
||
10 years ago
|
static void beginSearchRom_();
|
||
|
static void beginSearchRomSendBit_();
|
||
|
static void continueSearchRom_(bool error);
|
||
|
static void searchRomOnBitReceived_(bool bit, bool error);
|
||
10 years ago
|
|
||
9 years ago
|
static void beginWriteBytes_(const byte* data, short numBytes, void(*complete)(bool error));
|
||
10 years ago
|
static void beginReceiveBytes_(byte* buffer, short numBytes, void(*complete)(bool error));
|
||
|
|
||
9 years ago
|
static void endWrite_(bool error, bool resetInterrupts = true);
|
||
9 years ago
|
|
||
|
static void onSynchronousWriteComplete_(bool error);
|
||
10 years ago
|
static void noOpCallback_(bool error);
|
||
|
static void matchRomBytesReceived_(bool error);
|
||
|
static void notifyClientByteReceived_(bool error);
|
||
10 years ago
|
static void bitSent_(bool error);
|
||
10 years ago
|
|
||
10 years ago
|
// interrupt handlers
|
||
10 years ago
|
static void waitReset_();
|
||
|
static void beginPresence_();
|
||
|
static void endPresence_();
|
||
|
static void receive_();
|
||
|
static void readBit_();
|
||
|
static void sendBitOne_();
|
||
|
static void sendBitZero_();
|
||
|
static void endSendBitZero_();
|
||
10 years ago
|
static void resetCheck_();
|
||
10 years ago
|
|
||
|
private:
|
||
10 years ago
|
static byte rom_[8];
|
||
10 years ago
|
static byte scratchpad_[8];
|
||
10 years ago
|
static Pin pin_;
|
||
10 years ago
|
|
||
10 years ago
|
static unsigned long resetStart_;
|
||
|
static unsigned long lastReset_;
|
||
10 years ago
|
|
||
10 years ago
|
static void(*receiveBitCallback_)(bool bit, bool error);
|
||
|
static void(*bitSentCallback_)(bool error);
|
||
10 years ago
|
|
||
10 years ago
|
static byte receivingByte_;
|
||
10 years ago
|
|
||
10 years ago
|
static byte searchRomBytePos_;
|
||
|
static byte searchRomBitPos_;
|
||
|
static bool searchRomInverse_;
|
||
9 years ago
|
static bool resumeCommandFlag_;
|
||
9 years ago
|
static bool alarmedFlag_;
|
||
10 years ago
|
|
||
9 years ago
|
static const byte* sendBuffer_;
|
||
|
static byte* recvBuffer_;
|
||
10 years ago
|
static short bufferLength_;
|
||
|
static short bufferPos_;
|
||
|
static byte bufferBitPos_;
|
||
10 years ago
|
static void(*receiveBytesCallback_)(bool error);
|
||
10 years ago
|
static void(*sendBytesCallback_)(bool error);
|
||
|
|
||
9 years ago
|
static volatile bool waitingSynchronousWriteToComplete_;
|
||
|
static volatile bool synchronousWriteError_;
|
||
9 years ago
|
|
||
|
static bool sendingClientBytes_;
|
||
|
|
||
9 years ago
|
static bool singleBit_;
|
||
|
static bool singleBitRepeat_;
|
||
|
static void (*singleBitSentCallback_)(bool error);
|
||
|
static void onSingleBitSent_(bool error);
|
||
|
|
||
10 years ago
|
static void(*clientReceiveCallback_)(ReceiveEvent evt, byte data);
|
||
9 years ago
|
static void(*clientReceiveBitCallback_)(bool bit);
|
||
9 years ago
|
|
||
|
static void(*logCallback_)(const char* message);
|
||
10 years ago
|
};
|
||
|
|
||
9 years ago
|
extern OneWireSlave OWSlave;
|
||
10 years ago
|
|
||
10 years ago
|
#endif
|