From 8fb20ab065dfe50cf0e92da85fd2c1fda30ddcdb Mon Sep 17 00:00:00 2001 From: Youen Toupin Date: Thu, 23 Apr 2015 18:56:18 +0200 Subject: [PATCH] =?UTF-8?q?improved=20debugging=20messages=20on=20serial?= =?UTF-8?q?=20port=20(approximate=20measured=20time=20to=20append=20a=20me?= =?UTF-8?q?ssage=20:=2010=C2=B5s=20;=20messages=20are=20actually=20sent=20?= =?UTF-8?q?later,=20from=20the=20main=20loop)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- OneWireIO.ino | 31 ++++++++++--- SerialChannel.cpp | 58 +++++++++++++++++++++++-- SerialChannel.h | 32 ++++++++++++-- SerialMonitor/SerialMonitor/App.xaml.cs | 2 +- 4 files changed, 107 insertions(+), 16 deletions(-) diff --git a/OneWireIO.ino b/OneWireIO.ino index 72920bd..604a01e 100644 --- a/OneWireIO.ino +++ b/OneWireIO.ino @@ -6,9 +6,12 @@ #define OWPin 2 #define InterruptNumber 0 // Must correspond to the OWPin to correctly detect state changes. On Arduino Uno, interrupt 0 is for digital pin 2 -#define ResetMinDuration 360 +#define ResetMinDuration 480 #define ResetMaxDuration 900 +#define PresenceWaitDuration 30 +#define PresenceDuration 150 + SerialChannel debug("debug"); Pin owPin(OWPin); @@ -16,6 +19,16 @@ Pin ledPin(LEDPin); unsigned long resetStart = (unsigned long)-1; +enum Status +{ + S_WaitReset, + S_WaitPresence, + S_Presence, +}; + +Status status = S_WaitReset; +unsigned long statusChangeTime = (unsigned long)-1; + void setup() { ledPin.outputMode(); @@ -31,9 +44,10 @@ void setup() void loop() { cli();//disable interrupts - + SerialChannel::swap(); sei();//enable interrupts + SerialChannel::flush(); } void onewireInterrupt(void) @@ -48,12 +62,15 @@ void onewireInterrupt(void) if (state) { - unsigned long resetDuration = now - resetStart; - if (resetStart != (unsigned long)-1 && resetDuration >= ResetMinDuration && resetDuration <= ResetMaxDuration) + unsigned long resetDuration = resetStart == (unsigned long)-1 ? (unsigned long)-1 : now - resetStart; + resetStart = (unsigned long)-1; + + if (resetDuration >= ResetMinDuration && resetDuration <= ResetMaxDuration) { - debug.write("reset"); - ledPin.writeHigh(); + debug.SC_APPEND_STR_TIME("reset", now); + status = S_WaitPresence; + statusChangeTime = now; + return; } - resetStart = (unsigned long)-1; } } diff --git a/SerialChannel.cpp b/SerialChannel.cpp index 4c8c51d..73720ae 100644 --- a/SerialChannel.cpp +++ b/SerialChannel.cpp @@ -4,6 +4,12 @@ byte SerialChannel::nextId = 1; SerialChannel* SerialChannel::first = 0; +SerialChannel::Message SerialChannel::buffer1[SerialChannel::MaxPendingMessages]; +SerialChannel::Message SerialChannel::buffer2[SerialChannel::MaxPendingMessages]; +SerialChannel::Message* SerialChannel::backBuffer; +byte SerialChannel::backBufferPos; +byte SerialChannel::frontBufferSize; + SerialChannel::SerialChannel(const char* name_) : next(0) , id((byte)-1) @@ -21,6 +27,14 @@ SerialChannel::SerialChannel(const char* name_) id = nextId++; } +void SerialChannel::beginWriteInChannel(byte id, short byteCount, unsigned long time) +{ + Serial.write("START"); + Serial.write(id); + writeULong(time); + writeShort(byteCount); +} + void SerialChannel::write(byte* data, short byteCount, unsigned long time) { beginWrite(byteCount, time); @@ -34,10 +48,7 @@ void SerialChannel::beginWrite(short byteCount, unsigned long time) handleConnection(); - Serial.write("START"); - Serial.write(id); - writeULong(time); - writeShort(byteCount); + beginWriteInChannel(id, byteCount, time); } void SerialChannel::continueWrite(byte* data, short byteCount) @@ -50,6 +61,44 @@ void SerialChannel::write(const char* text, unsigned long time) write((byte*)text, strlen(text), time); } +void SerialChannel::append(byte* data, short byteCount, unsigned long time) +{ + if (time == (unsigned long)-1) + time = micros(); + + Message& msg = backBuffer[backBufferPos++]; + if (backBufferPos >= MaxPendingMessages) + backBufferPos = MaxPendingMessages - 1; + msg.id = id; + msg.data = data; + msg.byteCount = byteCount; + msg.time = time; +} + +void SerialChannel::append(const char* text, unsigned long time) +{ + append((byte*)text, strlen(text), time); +} + +void SerialChannel::swap() +{ + backBuffer = backBuffer == buffer1 ? buffer2 : buffer1; + frontBufferSize = backBufferPos; + backBufferPos = 0; +} + +void SerialChannel::flush() +{ + handleConnection(); + + Message* frontBuffer = backBuffer == buffer1 ? buffer2 : buffer1; + for (Message* msg = frontBuffer; msg < frontBuffer + frontBufferSize; ++msg) + { + beginWriteInChannel(msg->id, msg->byteCount, msg->time); + Serial.write(msg->data, msg->byteCount); + } +} + void SerialChannel::writeShort(short num) { Serial.write((byte*)&num, 2); @@ -77,6 +126,7 @@ void SerialChannel::handleConnection() Serial.write(c->name); c = c->next; } + Serial.flush(); } } diff --git a/SerialChannel.h b/SerialChannel.h index c58aa4c..116a53c 100644 --- a/SerialChannel.h +++ b/SerialChannel.h @@ -1,9 +1,22 @@ #ifndef _SerialChannel_h_ #define _SerialChannel_h_ +#define SC_APPEND_STR(str) append((byte*)str, sizeof(str)-1) +#define SC_APPEND_STR_TIME(str, time) append((byte*)str, sizeof(str)-1, time) + class SerialChannel { private: + static const byte MaxPendingMessages = 16; + + struct Message + { + byte* data; + unsigned long time; + short byteCount; + byte id; + }; + static SerialChannel* first; SerialChannel* next; @@ -11,20 +24,31 @@ private: byte id; const char* name; + static Message buffer1[MaxPendingMessages]; + static Message buffer2[MaxPendingMessages]; + static Message* backBuffer; + static byte backBufferPos; + static byte frontBufferSize; + public: SerialChannel(const char* name_); + static void beginWriteInChannel(byte id, short byteCount, unsigned long time); void write(byte* data, short byteCount, unsigned long time = (unsigned long)-1); - void write(const char* text, unsigned long time = (unsigned long)-1); void beginWrite(short byteCount, unsigned long time = (unsigned long)-1); void continueWrite(byte* data, short byteCount); + + void append(byte* data, short byteCount, unsigned long time = (unsigned long)-1); + void append(const char* text, unsigned long time = (unsigned long)-1); + static void swap(); + static void flush(); private: - void handleConnection(); - void writeShort(short num); - void writeULong(unsigned long num); + static void handleConnection(); + static void writeShort(short num); + static void writeULong(unsigned long num); }; #endif diff --git a/SerialMonitor/SerialMonitor/App.xaml.cs b/SerialMonitor/SerialMonitor/App.xaml.cs index bd2b9f2..897717c 100644 --- a/SerialMonitor/SerialMonitor/App.xaml.cs +++ b/SerialMonitor/SerialMonitor/App.xaml.cs @@ -80,7 +80,7 @@ namespace SerialMonitor switch (message.ChannelName) { case "debug": - string text = ((float)message.SendTime / 1000000).ToString("0.000") + "s " + message.StringData; + string text = ((float)message.SendTime / 1000000).ToString("0.000000") + "s " + message.StringData; //Debug.WriteLine(text); MainWindowContext.Get.WriteLine(text); break;