Browse Source

improved debugging messages on serial port (approximate measured time to append a message : 10µs ; messages are actually sent later, from the main loop)

timer1
Youen Toupin 9 years ago
parent
commit
8fb20ab065
  1. 31
      OneWireIO.ino
  2. 58
      SerialChannel.cpp
  3. 32
      SerialChannel.h
  4. 2
      SerialMonitor/SerialMonitor/App.xaml.cs

31
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;
}
}

58
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();
}
}

32
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

2
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;

Loading…
Cancel
Save