From c5a141b57e2d8248664d7ebb2c9c0e0fce24d09f Mon Sep 17 00:00:00 2001 From: Youen Toupin Date: Mon, 14 Dec 2015 22:32:26 +0100 Subject: [PATCH] added code to write individual bits (instead of bytes), and to cancel the current write operation before it completes --- .gitignore | 193 +---------------------------------------------- OneWireSlave.cpp | 43 ++++++++++- OneWireSlave.h | 13 +++- 3 files changed, 57 insertions(+), 192 deletions(-) diff --git a/.gitignore b/.gitignore index 7964536..5d05d5f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,189 +1,4 @@ -## Ignore Visual Studio temporary files, build results, and -## files generated by popular Visual Studio add-ons. - -# User-specific files -*.suo -*.user -*.sln.docstates - -# Build results -[Dd]ebug/ -[Dd]ebugPublic/ -[Rr]elease/ -x64/ -build/ -bld/ -[Bb]in/ -[Oo]bj/ - -# Roslyn cache directories -*.ide/ - -# MSTest test Results -[Tt]est[Rr]esult*/ -[Bb]uild[Ll]og.* - -#NUNIT -*.VisualState.xml -TestResult.xml - -# Build Results of an ATL Project -[Dd]ebugPS/ -[Rr]eleasePS/ -dlldata.c - -*_i.c -*_p.c -*_i.h -*.ilk -*.meta -*.obj -*.pch -*.pdb -*.pgc -*.pgd -*.rsp -*.sbr -*.tlb -*.tli -*.tlh -*.tmp -*.tmp_proj -*.log -*.vspscc -*.vssscc -.builds -*.pidb -*.svclog -*.scc - -# Chutzpah Test files -_Chutzpah* - -# Visual C++ cache files -ipch/ -*.aps -*.ncb -*.opensdf -*.sdf -*.cachefile - -# Visual Studio profiler -*.psess -*.vsp -*.vspx - -# TFS 2012 Local Workspace -$tf/ - -# Guidance Automation Toolkit -*.gpState - -# ReSharper is a .NET coding add-in -_ReSharper*/ -*.[Rr]e[Ss]harper -*.DotSettings.user - -# JustCode is a .NET coding addin-in -.JustCode - -# TeamCity is a build add-in -_TeamCity* - -# DotCover is a Code Coverage Tool -*.dotCover - -# NCrunch -_NCrunch_* -.*crunch*.local.xml - -# MightyMoose -*.mm.* -AutoTest.Net/ - -# Web workbench (sass) -.sass-cache/ - -# Installshield output folder -[Ee]xpress/ - -# DocProject is a documentation generator add-in -DocProject/buildhelp/ -DocProject/Help/*.HxT -DocProject/Help/*.HxC -DocProject/Help/*.hhc -DocProject/Help/*.hhk -DocProject/Help/*.hhp -DocProject/Help/Html2 -DocProject/Help/html - -# Click-Once directory -publish/ - -# Publish Web Output -*.[Pp]ublish.xml -*.azurePubxml -## TODO: Comment the next line if you want to checkin your -## web deploy settings but do note that will include unencrypted -## passwords -#*.pubxml - -# NuGet Packages Directory -packages/* -## TODO: If the tool you use requires repositories.config -## uncomment the next line -#!packages/repositories.config - -# Enable "build/" folder in the NuGet Packages folder since -# NuGet packages use it for MSBuild targets. -# This line needs to be after the ignore of the build folder -# (and the packages folder if the line above has been uncommented) -!packages/build/ - -# Windows Azure Build Output -csx/ -*.build.csdef - -# Windows Store app package directory -AppPackages/ - -# Others -sql/ -*.Cache -ClientBin/ -[Ss]tyle[Cc]op.* -~$* -*~ -*.dbmdl -*.dbproj.schemaview -*.pfx -*.publishsettings -node_modules/ - -# RIA/Silverlight projects -Generated_Code/ - -# Backup & report files from converting an old project file -# to a newer Visual Studio version. Backup files are not needed, -# because we have git ;-) -_UpgradeReport_Files/ -Backup*/ -UpgradeLog*.XML -UpgradeLog*.htm - -# SQL Server files -*.mdf -*.ldf - -# Business Intelligence projects -*.rdl.data -*.bim.layout -*.bim_*.settings - -# Microsoft Fakes -FakesAssemblies/ - -# LightSwitch generated files -GeneratedArtifacts/ -_Pvt_Extensions/ -ModelManifest.xml \ No newline at end of file +/.vs +/Debug +/*.opensdf +/*.sdf diff --git a/OneWireSlave.cpp b/OneWireSlave.cpp index b5f4e2d..3d5466a 100644 --- a/OneWireSlave.cpp +++ b/OneWireSlave.cpp @@ -107,6 +107,38 @@ void OneWireSlave::write(const byte* bytes, short numBytes, void(*complete)(bool sei(); } +void OneWireSlave::writeBit(bool value, bool repeat, void(*bitSent)(bool)) +{ + cli(); + singleBit_ = value; + singleBitRepeat_ = repeat; + singleBitSentCallback_ = bitSent; + beginSendBit_(value, &OneWireSlave::onSingleBitSent_); + sei(); +} + +void OneWireSlave::onSingleBitSent_(bool error) +{ + if (!error && singleBitRepeat_) + { + beginSendBit_(singleBit_, &OneWireSlave::onSingleBitSent_); + } + else + { + beginResetDetection_(); + } + + if (singleBitSentCallback_ != 0) + { + singleBitSentCallback_(error); + } +} + +void OneWireSlave::stopWrite() +{ + write(0, 0, 0); +} + byte OneWireSlave::crc8(const byte* data, short numBytes) { byte crc = 0; @@ -476,8 +508,15 @@ void OneWireSlave::beginWriteBytes_(const byte* data, short numBytes, void(*comp bufferBitPos_ = 0; sendBytesCallback_ = complete; - bool bit = bitRead(sendBuffer_[0], 0); - beginSendBit_(bit, &OneWireSlave::bitSent_); + if (sendBuffer_ != 0 && bufferLength_ > 0) + { + bool bit = bitRead(sendBuffer_[0], 0); + beginSendBit_(bit, &OneWireSlave::bitSent_); + } + else + { + beginResetDetection_(); + } } void OneWireSlave::bitSent_(bool error) diff --git a/OneWireSlave.h b/OneWireSlave.h index ac8838d..f8d6f3d 100644 --- a/OneWireSlave.h +++ b/OneWireSlave.h @@ -23,9 +23,15 @@ 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; } - //! 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. + //! 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)); + //! 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. + void writeBit(bool value, bool repeat = false, void(*bitSent)(bool error) = 0); + + //! 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(); + static byte crc8(const byte* data, short numBytes); private: @@ -101,6 +107,11 @@ private: static void(*receiveBytesCallback_)(bool error); static void(*sendBytesCallback_)(bool error); + static bool singleBit_; + static bool singleBitRepeat_; + static void (*singleBitSentCallback_)(bool error); + static void onSingleBitSent_(bool error); + static void(*clientReceiveCallback_)(ReceiveEvent evt, byte data); };