From 9e2a921d559855c62c23547c7763472d8f635301 Mon Sep 17 00:00:00 2001 From: destroyedlolo Date: Fri, 23 Mar 2018 19:42:02 +0100 Subject: [PATCH 01/18] Move library sources to src directory --- LowLevel.h => src/LowLevel.h | 0 OneWireSlave.cpp => src/OneWireSlave.cpp | 0 OneWireSlave.h => src/OneWireSlave.h | 0 3 files changed, 0 insertions(+), 0 deletions(-) rename LowLevel.h => src/LowLevel.h (100%) rename OneWireSlave.cpp => src/OneWireSlave.cpp (100%) rename OneWireSlave.h => src/OneWireSlave.h (100%) diff --git a/LowLevel.h b/src/LowLevel.h similarity index 100% rename from LowLevel.h rename to src/LowLevel.h diff --git a/OneWireSlave.cpp b/src/OneWireSlave.cpp similarity index 100% rename from OneWireSlave.cpp rename to src/OneWireSlave.cpp diff --git a/OneWireSlave.h b/src/OneWireSlave.h similarity index 100% rename from OneWireSlave.h rename to src/OneWireSlave.h From 8dc5d5dd3ebf576af84ba8a9260fd807a56c0881 Mon Sep 17 00:00:00 2001 From: destroyedlolo Date: Fri, 23 Mar 2018 19:44:03 +0100 Subject: [PATCH 02/18] Remove uneeded m$eries --- OneWireIO.sln | 32 ------------ OneWireIO.vcxproj | 107 -------------------------------------- OneWireIO.vcxproj.filters | 13 ----- 3 files changed, 152 deletions(-) delete mode 100644 OneWireIO.sln delete mode 100644 OneWireIO.vcxproj delete mode 100644 OneWireIO.vcxproj.filters diff --git a/OneWireIO.sln b/OneWireIO.sln deleted file mode 100644 index f10e9f5..0000000 --- a/OneWireIO.sln +++ /dev/null @@ -1,32 +0,0 @@ - -Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio 2013 -VisualStudioVersion = 12.0.31101.0 -MinimumVisualStudioVersion = 10.0.40219.1 -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "OneWireIO", "OneWireIO.vcxproj", "{3B500971-1570-460F-81C3-22AC3B7764B9}" -EndProject -Global - GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|Any CPU = Debug|Any CPU - Debug|Mixed Platforms = Debug|Mixed Platforms - Debug|Win32 = Debug|Win32 - Release|Any CPU = Release|Any CPU - Release|Mixed Platforms = Release|Mixed Platforms - Release|Win32 = Release|Win32 - EndGlobalSection - GlobalSection(ProjectConfigurationPlatforms) = postSolution - {3B500971-1570-460F-81C3-22AC3B7764B9}.Debug|Any CPU.ActiveCfg = Debug|Win32 - {3B500971-1570-460F-81C3-22AC3B7764B9}.Debug|Mixed Platforms.ActiveCfg = Debug|Win32 - {3B500971-1570-460F-81C3-22AC3B7764B9}.Debug|Mixed Platforms.Build.0 = Debug|Win32 - {3B500971-1570-460F-81C3-22AC3B7764B9}.Debug|Win32.ActiveCfg = Debug|Win32 - {3B500971-1570-460F-81C3-22AC3B7764B9}.Debug|Win32.Build.0 = Debug|Win32 - {3B500971-1570-460F-81C3-22AC3B7764B9}.Release|Any CPU.ActiveCfg = Release|Win32 - {3B500971-1570-460F-81C3-22AC3B7764B9}.Release|Mixed Platforms.ActiveCfg = Release|Win32 - {3B500971-1570-460F-81C3-22AC3B7764B9}.Release|Mixed Platforms.Build.0 = Release|Win32 - {3B500971-1570-460F-81C3-22AC3B7764B9}.Release|Win32.ActiveCfg = Release|Win32 - {3B500971-1570-460F-81C3-22AC3B7764B9}.Release|Win32.Build.0 = Release|Win32 - EndGlobalSection - GlobalSection(SolutionProperties) = preSolution - HideSolutionNode = FALSE - EndGlobalSection -EndGlobal diff --git a/OneWireIO.vcxproj b/OneWireIO.vcxproj deleted file mode 100644 index 94ae257..0000000 --- a/OneWireIO.vcxproj +++ /dev/null @@ -1,107 +0,0 @@ - - - - - Debug - Win32 - - - Release - Win32 - - - - {3B500971-1570-460F-81C3-22AC3B7764B9} - OneWireIO - OneWireIO - - - - Application - true - v140 - MultiByte - - - Application - false - v140 - true - MultiByte - - - - - - - - - - - - - - - Level3 - Disabled - true - D:\Outils\Arduino\hardware\arduino\avr\cores\arduino;D:\Outils\Arduino\hardware\tools\avr\avr\include;C:\Program Files %28x86%29\Arduino\hardware\arduino\avr\cores\arduino;C:\Program Files %28x86%29\Arduino\hardware\tools\avr\avr\include - _MBCS;%(PreprocessorDefinitions);ARDUINO=160;__AVR__;UBRRH;__AVR_ATmega328P__;VS_INTELLISENSE - - - true - - - if exist $(ProjectDir)$(IntDir)$(ProjectName).ino.elf ( -echo Elf generated -) else ( -echo no output -exit /B 1 -) - - - - - - - - - - Level3 - MaxSpeed - true - true - true - - - true - true - true - - - - - Document - false - if exist $(ProjectDir)$(IntDir)$(ProjectName).ino.elf del $(ProjectDir)$(IntDir)$(ProjectName).ino.elf -"C:\Program Files (x86)\Arduino\arduino.exe" --upload --pref build.path=$(ProjectDir)$(Configuration) %(FullPath) | more - - always_build_$(ProjectConfiguration).dummy - - - Building and uploading sketch... - - - - - true - - - - - - - - - - \ No newline at end of file diff --git a/OneWireIO.vcxproj.filters b/OneWireIO.vcxproj.filters deleted file mode 100644 index 6f73395..0000000 --- a/OneWireIO.vcxproj.filters +++ /dev/null @@ -1,13 +0,0 @@ - - - - - - - - - - - - - \ No newline at end of file From 7d6e4b520abb1c5f5f2eb33a7415db00cf6f4662 Mon Sep 17 00:00:00 2001 From: destroyedlolo Date: Fri, 23 Mar 2018 19:54:54 +0100 Subject: [PATCH 03/18] Separate DS18B20 example --- OneWireIO.ino => examples/orgDS18B20/orgDS18B20.ino | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) rename OneWireIO.ino => examples/orgDS18B20/orgDS18B20.ino (97%) diff --git a/OneWireIO.ino b/examples/orgDS18B20/orgDS18B20.ino similarity index 97% rename from OneWireIO.ino rename to examples/orgDS18B20/orgDS18B20.ino index 62b2ca3..d7d7399 100644 --- a/OneWireIO.ino +++ b/examples/orgDS18B20/orgDS18B20.ino @@ -1,6 +1,6 @@ -#include "Arduino.h" -#include "LowLevel.h" -#include "OneWireSlave.h" +#include +#include +#include // 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 From 47034b0fa5215e47e2992dd7b4a2cf1e0e33c52d Mon Sep 17 00:00:00 2001 From: destroyedlolo Date: Fri, 23 Mar 2018 19:56:11 +0100 Subject: [PATCH 04/18] Make it as a workable library --- library.properties | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 library.properties diff --git a/library.properties b/library.properties new file mode 100644 index 0000000..4e68401 --- /dev/null +++ b/library.properties @@ -0,0 +1,9 @@ +name=OneWireArduinoSlave +version=0.2 +author=neuoy +maintainer=https://github.com/destroyedlolo/OneWireArduinoSlave +sentence=Library to create slave 1-wire devices +paragraph= +category=Device Control +url=https://github.com/destroyedlolo/OneWireArduinoSlave +architectures=* From add8145f95a227c1d5bd7be863bf53c9ab742b60 Mon Sep 17 00:00:00 2001 From: destroyedlolo Date: Fri, 23 Mar 2018 20:03:03 +0100 Subject: [PATCH 05/18] Remove a compilation warning --- src/LowLevel.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/LowLevel.h b/src/LowLevel.h index d18d387..184e382 100644 --- a/src/LowLevel.h +++ b/src/LowLevel.h @@ -112,8 +112,8 @@ __attribute__((always_inline)) static inline void UserTimer_Run(short skipTicks) class Pin { private: - volatile IO_REG_TYPE *reg_; IO_REG_TYPE mask_; + volatile IO_REG_TYPE *reg_; byte interruptNumber_; byte pinNumber_; From a74a79f29ee3841248fa17b3120ec43b30eaf854 Mon Sep 17 00:00:00 2001 From: destroyedlolo Date: Fri, 23 Mar 2018 20:05:08 +0100 Subject: [PATCH 06/18] Add a document about interesting URL found on the web --- Docs/Interesting URL | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Docs/Interesting URL diff --git a/Docs/Interesting URL b/Docs/Interesting URL new file mode 100644 index 0000000..773b38d --- /dev/null +++ b/Docs/Interesting URL @@ -0,0 +1,2 @@ +http://palsbo.com/pi/owfsslave.html + An interesting project covering both Arduino and OWFS aspect From df897199175859a54ab8b2cd47194139fdbee7f2 Mon Sep 17 00:00:00 2001 From: destroyedlolo Date: Fri, 23 Mar 2018 22:07:49 +0100 Subject: [PATCH 07/18] Add a description --- examples/orgDS18B20/README.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 examples/orgDS18B20/README.md diff --git a/examples/orgDS18B20/README.md b/examples/orgDS18B20/README.md new file mode 100644 index 0000000..392ca18 --- /dev/null +++ b/examples/orgDS18B20/README.md @@ -0,0 +1,12 @@ +This is the **DS18B20** fake emulator provided with the original version of OneWireArduinoSlave. + +To make it working, connect : + +- a GND pin of your Uno to your 1-wire network ground. +- the pin "2" of your Unto to the data line of your 1-wire network. You may use "3" as well but need to change the code. + +You will see 28.000000000002 probe that always return 42 as temperature : + + $ cat 28.000000000002/temperature + 42 +Notez-bien : conversion timing is emulated as well. From bf53ea5c774d5445b246af9be4cdf7d277853ab5 Mon Sep 17 00:00:00 2001 From: destroyedlolo Date: Fri, 23 Mar 2018 23:01:51 +0100 Subject: [PATCH 08/18] Suite IDE Arduino specs --- {Docs => extras}/Interesting URL | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename {Docs => extras}/Interesting URL (100%) diff --git a/Docs/Interesting URL b/extras/Interesting URL similarity index 100% rename from Docs/Interesting URL rename to extras/Interesting URL From 6723ee6e52df87e6347778ffcc1694ffcbf21a0d Mon Sep 17 00:00:00 2001 From: destroyedlolo Date: Sat, 24 Mar 2018 02:19:33 +0100 Subject: [PATCH 09/18] Use a DHT22 as a DS18B20 --- examples/DS18B20_DHT22/DS18B20_DHT22.ino | 112 +++++++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 examples/DS18B20_DHT22/DS18B20_DHT22.ino diff --git a/examples/DS18B20_DHT22/DS18B20_DHT22.ino b/examples/DS18B20_DHT22/DS18B20_DHT22.ino new file mode 100644 index 0000000..9497d39 --- /dev/null +++ b/examples/DS18B20_DHT22/DS18B20_DHT22.ino @@ -0,0 +1,112 @@ +/* Emulates a DS18B20 using a DHT22 + * + * Provided by Destroyedlolo. (http://destroyedlolo.info) + * + * 23/03/2018 - First version + */ + +#include +#include +#include + +Pin oneWireData(2); // Where the 1-wire bus is connected to + +const byte owROM[7] = { 0x28, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02 }; + +enum OW_Cmd { + START_CONVERSION = 0x44, + READ_SCRATCHPAD = 0xBE +}; + +volatile byte scratchpad[9]; + + /* + * DHT22 related + */ +#include // https://github.com/winlinvip/SimpleDHT + +SimpleDHT22 DHT; +#define pinDHT 3 + + /* + * Let's go + */ +#define DEBUG // Comment out to be silence + +enum DeviceState { + WAIT4RESET, + WAIT4COMMAND, + CONVERTING, + TEMPERATUREREADY +}; + +volatile DeviceState state = DeviceState::WAIT4RESET; + +void owRcv( OneWireSlave::ReceiveEvent evt, byte cmd ){ + switch( evt ){ + case OneWireSlave::RE_Reset: + state = DeviceState::WAIT4COMMAND; + break; + case OneWireSlave::RE_Error: + state = DeviceState::WAIT4RESET; + break; + case OneWireSlave::RE_Byte: + switch( cmd ){ + case OW_Cmd::START_CONVERSION: + state = DeviceState::CONVERTING; + OWSlave.beginWriteBit(0, true); // send zeros as long as the conversion is not finished + break; + case OW_Cmd::READ_SCRATCHPAD: + state = DeviceState::WAIT4RESET; + OWSlave.beginWrite((const byte*)scratchpad, 9, 0); + break; +#ifdef DEBUG + default: + Serial.print(F("Unknown command :")); + Serial.println( cmd, HEX ); +#endif + } + } +} + +void setTemperature( float temp ){ // Write given temperature to the scratchpad + int16_t raw = (int16_t)(temp * 16.0f + 0.5f); + + scratchpad[0] = (byte)raw; + scratchpad[1] = (byte)(raw >> 8); + scratchpad[8] = OWSlave.crc8((const byte*)scratchpad, 8); +} + +void setup(){ + Serial.begin(115200); // debugging + + for(int i = 2; i<8; i++) + scratchpad[i] = 0; + setTemperature( 85 ); + + OWSlave.setReceiveCallback(&owRcv); + OWSlave.begin(owROM, oneWireData.getPinNumber()); +} + + +void loop(){ + delay(10); + + if(state == DeviceState::CONVERTING){ // start conversion + float temperature = 0; + float humidite = 0; + int err; + + if((err = DHT.read2(pinDHT, &temperature, &humidite, NULL)) != SimpleDHTErrSuccess){ +#ifdef DEBUG + Serial.print("\nError while reading, err="); + Serial.println(err); +#endif + temperature = 85; + } else { + setTemperature( temperature ); + state = DeviceState::TEMPERATUREREADY; + OWSlave.beginWriteBit(1, true); + } + } +} From ac43646ce5a1f54ecf2c89ac8ba5afff5fcf9630 Mon Sep 17 00:00:00 2001 From: destroyedlolo Date: Sat, 24 Mar 2018 13:29:10 +0100 Subject: [PATCH 10/18] More library friendly (from ntruchsess fork) --- keywords.txt | 34 ++++++++++++++++++++++++++++++++++ library.properties | 2 +- 2 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 keywords.txt diff --git a/keywords.txt b/keywords.txt new file mode 100644 index 0000000..7a929d7 --- /dev/null +++ b/keywords.txt @@ -0,0 +1,34 @@ +####################################### +# Syntax Coloring Map For OneWireSlave +####################################### + +####################################### +# Datatypes (KEYWORD1) +####################################### + +ReceiveEvent KEYWORD1 +OneWireSlave KEYWORD1 +Pin KEYWORD1 +OWSlave KEYWORD1 + +####################################### +# Methods and Functions (KEYWORD2) +####################################### + +begin KEYWORD2 +end KEYWORD2 +setReceiveCallback KEYWORD2 +setReceiveBitCallback KEYWORD2 +write KEYWORD2 +writeBit KEYWORD2 +stopWrite KEYWORD2 +alarmed KEYWORD2 +crc8 KEYWORD2 + +####################################### +# Constants (LITERAL1) +####################################### + +RE_Reset LITERAL1 +RE_Byte LITERAL1 +RE_Error LITERAL1 diff --git a/library.properties b/library.properties index 4e68401..71c0be7 100644 --- a/library.properties +++ b/library.properties @@ -3,7 +3,7 @@ version=0.2 author=neuoy maintainer=https://github.com/destroyedlolo/OneWireArduinoSlave sentence=Library to create slave 1-wire devices -paragraph= +paragraph= +paragraph=This library allows you to emulate existing 1-wire devices with an Arduino, or to create your own protocol. All low-level details are handled by the library, such as reset detection, ROM matching, byte sending and receiving. category=Device Control url=https://github.com/destroyedlolo/OneWireArduinoSlave architectures=* From 28f9205701d2da5f75a54f49f7b2235bf7beff73 Mon Sep 17 00:00:00 2001 From: destroyedlolo Date: Sat, 24 Mar 2018 13:46:19 +0100 Subject: [PATCH 11/18] Correct OW code in case of DHT reading error --- examples/DS18B20_DHT22/DS18B20_DHT22.ino | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/examples/DS18B20_DHT22/DS18B20_DHT22.ino b/examples/DS18B20_DHT22/DS18B20_DHT22.ino index 9497d39..cb4f752 100644 --- a/examples/DS18B20_DHT22/DS18B20_DHT22.ino +++ b/examples/DS18B20_DHT22/DS18B20_DHT22.ino @@ -92,6 +92,8 @@ void setup(){ void loop(){ delay(10); + // No need to have a local copy of "state" as we are looking + // for only one value if(state == DeviceState::CONVERTING){ // start conversion float temperature = 0; float humidite = 0; @@ -103,10 +105,9 @@ void loop(){ Serial.println(err); #endif temperature = 85; - } else { + } else setTemperature( temperature ); - state = DeviceState::TEMPERATUREREADY; - OWSlave.beginWriteBit(1, true); - } + state = DeviceState::TEMPERATUREREADY; + OWSlave.beginWriteBit(1, true); } } From 08991060dacd0060126e621b3ac8ea44ca011a17 Mon Sep 17 00:00:00 2001 From: destroyedlolo Date: Sat, 24 Mar 2018 13:58:27 +0100 Subject: [PATCH 12/18] Add some comments about race condition --- examples/DS18B20_DHT22/DS18B20_DHT22.ino | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/examples/DS18B20_DHT22/DS18B20_DHT22.ino b/examples/DS18B20_DHT22/DS18B20_DHT22.ino index cb4f752..70b5ce5 100644 --- a/examples/DS18B20_DHT22/DS18B20_DHT22.ino +++ b/examples/DS18B20_DHT22/DS18B20_DHT22.ino @@ -55,6 +55,7 @@ void owRcv( OneWireSlave::ReceiveEvent evt, byte cmd ){ case OW_Cmd::START_CONVERSION: state = DeviceState::CONVERTING; OWSlave.beginWriteBit(0, true); // send zeros as long as the conversion is not finished + // consequently, we don't have to take care of race condition as no other command can arrive break; case OW_Cmd::READ_SCRATCHPAD: state = DeviceState::WAIT4RESET; @@ -72,6 +73,9 @@ void owRcv( OneWireSlave::ReceiveEvent evt, byte cmd ){ void setTemperature( float temp ){ // Write given temperature to the scratchpad int16_t raw = (int16_t)(temp * 16.0f + 0.5f); + // We don't care about race condition as well as only one command + // can be processed at a time otherwise we are failing in error/collision + // condition. scratchpad[0] = (byte)raw; scratchpad[1] = (byte)(raw >> 8); scratchpad[8] = OWSlave.crc8((const byte*)scratchpad, 8); From acb3069883506daba9997b3df09b052b00381d22 Mon Sep 17 00:00:00 2001 From: destroyedlolo Date: Mon, 26 Mar 2018 22:36:11 +0200 Subject: [PATCH 13/18] Add online CRC calculator --- extras/Interesting URL | 3 +++ 1 file changed, 3 insertions(+) diff --git a/extras/Interesting URL b/extras/Interesting URL index 773b38d..bccbddd 100644 --- a/extras/Interesting URL +++ b/extras/Interesting URL @@ -1,2 +1,5 @@ http://palsbo.com/pi/owfsslave.html An interesting project covering both Arduino and OWFS aspect + +http://www.datastat.com/sysadminjournal/maximcrc.cgi + Address CRC calculator From 08d8346954852ba7d4a41892fe76dfa0810a5171 Mon Sep 17 00:00:00 2001 From: destroyedlolo Date: Tue, 27 Mar 2018 01:44:28 +0200 Subject: [PATCH 14/18] Ensure enough delay b/w samples --- examples/DS18B20_DHT22/DS18B20_DHT22.ino | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/examples/DS18B20_DHT22/DS18B20_DHT22.ino b/examples/DS18B20_DHT22/DS18B20_DHT22.ino index 70b5ce5..fd504e7 100644 --- a/examples/DS18B20_DHT22/DS18B20_DHT22.ino +++ b/examples/DS18B20_DHT22/DS18B20_DHT22.ino @@ -41,6 +41,7 @@ enum DeviceState { }; volatile DeviceState state = DeviceState::WAIT4RESET; +volatile unsigned long conversionStartTime = 0; void owRcv( OneWireSlave::ReceiveEvent evt, byte cmd ){ switch( evt ){ @@ -53,9 +54,11 @@ void owRcv( OneWireSlave::ReceiveEvent evt, byte cmd ){ case OneWireSlave::RE_Byte: switch( cmd ){ case OW_Cmd::START_CONVERSION: - state = DeviceState::CONVERTING; - OWSlave.beginWriteBit(0, true); // send zeros as long as the conversion is not finished - // consequently, we don't have to take care of race condition as no other command can arrive + // Do a new request only if enough time passed + if( millis() < conversionStartTime || millis() > conversionStartTime + 2000 ){ + state = DeviceState::CONVERTING; + OWSlave.beginWriteBit(0, true); // send zeros as long as the conversion is not finished + } break; case OW_Cmd::READ_SCRATCHPAD: state = DeviceState::WAIT4RESET; @@ -96,21 +99,25 @@ void setup(){ void loop(){ delay(10); - // No need to have a local copy of "state" as we are looking - // for only one value if(state == DeviceState::CONVERTING){ // start conversion float temperature = 0; float humidite = 0; int err; + conversionStartTime = millis(); if((err = DHT.read2(pinDHT, &temperature, &humidite, NULL)) != SimpleDHTErrSuccess){ #ifdef DEBUG Serial.print("\nError while reading, err="); Serial.println(err); #endif temperature = 85; - } else + } else { +#ifdef DEBUG + Serial.print("Temperature :"); + Serial.println(temperature); +#endif setTemperature( temperature ); + } state = DeviceState::TEMPERATUREREADY; OWSlave.beginWriteBit(1, true); } From f0ff69d8d1588631b7eb1472b155bdfe225dd4c9 Mon Sep 17 00:00:00 2001 From: destroyedlolo Date: Tue, 27 Mar 2018 21:59:32 +0200 Subject: [PATCH 15/18] Tell when a conversion is ignored --- examples/DS18B20_DHT22/DS18B20_DHT22.ino | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/examples/DS18B20_DHT22/DS18B20_DHT22.ino b/examples/DS18B20_DHT22/DS18B20_DHT22.ino index fd504e7..014f234 100644 --- a/examples/DS18B20_DHT22/DS18B20_DHT22.ino +++ b/examples/DS18B20_DHT22/DS18B20_DHT22.ino @@ -59,6 +59,10 @@ void owRcv( OneWireSlave::ReceiveEvent evt, byte cmd ){ state = DeviceState::CONVERTING; OWSlave.beginWriteBit(0, true); // send zeros as long as the conversion is not finished } +#ifdef DEBUG + else + Serial.println(F("Ignored StartConv")); +#endif break; case OW_Cmd::READ_SCRATCHPAD: state = DeviceState::WAIT4RESET; @@ -68,7 +72,7 @@ void owRcv( OneWireSlave::ReceiveEvent evt, byte cmd ){ default: Serial.print(F("Unknown command :")); Serial.println( cmd, HEX ); -#endif +#endif } } } From 273fb764e847cb206c4915ff4e9146a196d2b9b7 Mon Sep 17 00:00:00 2001 From: destroyedlolo Date: Wed, 28 Mar 2018 00:40:51 +0200 Subject: [PATCH 16/18] Correct early requests --- examples/DS18B20_DHT22/DS18B20_DHT22.ino | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/examples/DS18B20_DHT22/DS18B20_DHT22.ino b/examples/DS18B20_DHT22/DS18B20_DHT22.ino index 014f234..2d88af7 100644 --- a/examples/DS18B20_DHT22/DS18B20_DHT22.ino +++ b/examples/DS18B20_DHT22/DS18B20_DHT22.ino @@ -47,15 +47,21 @@ void owRcv( OneWireSlave::ReceiveEvent evt, byte cmd ){ switch( evt ){ case OneWireSlave::RE_Reset: state = DeviceState::WAIT4COMMAND; +#ifdef DEBUG + Serial.println(F("Reset")); +#endif break; case OneWireSlave::RE_Error: state = DeviceState::WAIT4RESET; +#ifdef DEBUG + Serial.println(F("Error")); +#endif break; case OneWireSlave::RE_Byte: switch( cmd ){ case OW_Cmd::START_CONVERSION: // Do a new request only if enough time passed - if( millis() < conversionStartTime || millis() > conversionStartTime + 2000 ){ + if( !millis() || millis() < conversionStartTime || millis() > conversionStartTime + 2000 ){ state = DeviceState::CONVERTING; OWSlave.beginWriteBit(0, true); // send zeros as long as the conversion is not finished } From 60d5d95175d0b39bdd0640f39c9ae0e9f711c0cb Mon Sep 17 00:00:00 2001 From: destroyedlolo Date: Wed, 28 Mar 2018 00:45:17 +0200 Subject: [PATCH 17/18] Really handle early requests and secure command waiting --- examples/DS18B20_DHT22/DS18B20_DHT22.ino | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/examples/DS18B20_DHT22/DS18B20_DHT22.ino b/examples/DS18B20_DHT22/DS18B20_DHT22.ino index 2d88af7..d9e894d 100644 --- a/examples/DS18B20_DHT22/DS18B20_DHT22.ino +++ b/examples/DS18B20_DHT22/DS18B20_DHT22.ino @@ -42,11 +42,13 @@ enum DeviceState { volatile DeviceState state = DeviceState::WAIT4RESET; volatile unsigned long conversionStartTime = 0; +volatile int ignByte = 0; // Bytes to be ignored void owRcv( OneWireSlave::ReceiveEvent evt, byte cmd ){ switch( evt ){ case OneWireSlave::RE_Reset: state = DeviceState::WAIT4COMMAND; + ignByte = 0; #ifdef DEBUG Serial.println(F("Reset")); #endif @@ -58,10 +60,10 @@ void owRcv( OneWireSlave::ReceiveEvent evt, byte cmd ){ #endif break; case OneWireSlave::RE_Byte: - switch( cmd ){ + if( state == DeviceState::WAIT4COMMAND ) switch( cmd ){ case OW_Cmd::START_CONVERSION: // Do a new request only if enough time passed - if( !millis() || millis() < conversionStartTime || millis() > conversionStartTime + 2000 ){ + if( !conversionStartTime || millis() < conversionStartTime || millis() > conversionStartTime + 2000 ){ state = DeviceState::CONVERTING; OWSlave.beginWriteBit(0, true); // send zeros as long as the conversion is not finished } From 82e83725126d0ca61579ff55100513a752acb81c Mon Sep 17 00:00:00 2001 From: destroyedlolo Date: Wed, 28 Mar 2018 01:20:03 +0200 Subject: [PATCH 18/18] Better handle unknown commands --- examples/DS18B20_DHT22/DS18B20_DHT22.ino | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/examples/DS18B20_DHT22/DS18B20_DHT22.ino b/examples/DS18B20_DHT22/DS18B20_DHT22.ino index d9e894d..089d4c3 100644 --- a/examples/DS18B20_DHT22/DS18B20_DHT22.ino +++ b/examples/DS18B20_DHT22/DS18B20_DHT22.ino @@ -15,6 +15,8 @@ const byte owROM[7] = { 0x28, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02 }; enum OW_Cmd { START_CONVERSION = 0x44, + WRITE_SCRATCHPAD = 0x4e, + READ_POWERSUPPLY = 0xb4, READ_SCRATCHPAD = 0xBE }; @@ -42,13 +44,11 @@ enum DeviceState { volatile DeviceState state = DeviceState::WAIT4RESET; volatile unsigned long conversionStartTime = 0; -volatile int ignByte = 0; // Bytes to be ignored void owRcv( OneWireSlave::ReceiveEvent evt, byte cmd ){ switch( evt ){ case OneWireSlave::RE_Reset: state = DeviceState::WAIT4COMMAND; - ignByte = 0; #ifdef DEBUG Serial.println(F("Reset")); #endif @@ -76,8 +76,13 @@ void owRcv( OneWireSlave::ReceiveEvent evt, byte cmd ){ state = DeviceState::WAIT4RESET; OWSlave.beginWrite((const byte*)scratchpad, 9, 0); break; -#ifdef DEBUG + case OW_Cmd::WRITE_SCRATCHPAD: + case OW_Cmd::READ_POWERSUPPLY: + state = DeviceState::WAIT4RESET; // Ignore parameters + break; default: + state = DeviceState::WAIT4RESET; +#ifdef DEBUG Serial.print(F("Unknown command :")); Serial.println( cmd, HEX ); #endif @@ -86,8 +91,7 @@ void owRcv( OneWireSlave::ReceiveEvent evt, byte cmd ){ } void setTemperature( float temp ){ // Write given temperature to the scratchpad - int16_t raw = (int16_t)(temp * 16.0f + 0.5f); - + int16_t raw = (int16_t)(temp * 16.0f + 0.5); // We don't care about race condition as well as only one command // can be processed at a time otherwise we are failing in error/collision // condition. @@ -128,8 +132,8 @@ void loop(){ Serial.print("Temperature :"); Serial.println(temperature); #endif - setTemperature( temperature ); } + setTemperature( temperature ); state = DeviceState::TEMPERATUREREADY; OWSlave.beginWriteBit(1, true); }