improved debugging messages on serial port (approximate measured time to append a message : 10µs ; messages are actually sent later, from the main loop)
This commit is contained in:
parent
03c2f0afd3
commit
8fb20ab065
@ -6,9 +6,12 @@
|
|||||||
#define OWPin 2
|
#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 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 ResetMaxDuration 900
|
||||||
|
|
||||||
|
#define PresenceWaitDuration 30
|
||||||
|
#define PresenceDuration 150
|
||||||
|
|
||||||
SerialChannel debug("debug");
|
SerialChannel debug("debug");
|
||||||
|
|
||||||
Pin owPin(OWPin);
|
Pin owPin(OWPin);
|
||||||
@ -16,6 +19,16 @@ Pin ledPin(LEDPin);
|
|||||||
|
|
||||||
unsigned long resetStart = (unsigned long)-1;
|
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()
|
void setup()
|
||||||
{
|
{
|
||||||
ledPin.outputMode();
|
ledPin.outputMode();
|
||||||
@ -31,9 +44,10 @@ void setup()
|
|||||||
void loop()
|
void loop()
|
||||||
{
|
{
|
||||||
cli();//disable interrupts
|
cli();//disable interrupts
|
||||||
|
SerialChannel::swap();
|
||||||
sei();//enable interrupts
|
sei();//enable interrupts
|
||||||
|
|
||||||
|
SerialChannel::flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
void onewireInterrupt(void)
|
void onewireInterrupt(void)
|
||||||
@ -48,12 +62,15 @@ void onewireInterrupt(void)
|
|||||||
|
|
||||||
if (state)
|
if (state)
|
||||||
{
|
{
|
||||||
unsigned long resetDuration = now - resetStart;
|
unsigned long resetDuration = resetStart == (unsigned long)-1 ? (unsigned long)-1 : now - resetStart;
|
||||||
if (resetStart != (unsigned long)-1 && resetDuration >= ResetMinDuration && resetDuration <= ResetMaxDuration)
|
|
||||||
{
|
|
||||||
debug.write("reset");
|
|
||||||
ledPin.writeHigh();
|
|
||||||
}
|
|
||||||
resetStart = (unsigned long)-1;
|
resetStart = (unsigned long)-1;
|
||||||
|
|
||||||
|
if (resetDuration >= ResetMinDuration && resetDuration <= ResetMaxDuration)
|
||||||
|
{
|
||||||
|
debug.SC_APPEND_STR_TIME("reset", now);
|
||||||
|
status = S_WaitPresence;
|
||||||
|
statusChangeTime = now;
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,12 @@
|
|||||||
byte SerialChannel::nextId = 1;
|
byte SerialChannel::nextId = 1;
|
||||||
SerialChannel* SerialChannel::first = 0;
|
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_)
|
SerialChannel::SerialChannel(const char* name_)
|
||||||
: next(0)
|
: next(0)
|
||||||
, id((byte)-1)
|
, id((byte)-1)
|
||||||
@ -21,6 +27,14 @@ SerialChannel::SerialChannel(const char* name_)
|
|||||||
id = nextId++;
|
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)
|
void SerialChannel::write(byte* data, short byteCount, unsigned long time)
|
||||||
{
|
{
|
||||||
beginWrite(byteCount, time);
|
beginWrite(byteCount, time);
|
||||||
@ -34,10 +48,7 @@ void SerialChannel::beginWrite(short byteCount, unsigned long time)
|
|||||||
|
|
||||||
handleConnection();
|
handleConnection();
|
||||||
|
|
||||||
Serial.write("START");
|
beginWriteInChannel(id, byteCount, time);
|
||||||
Serial.write(id);
|
|
||||||
writeULong(time);
|
|
||||||
writeShort(byteCount);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void SerialChannel::continueWrite(byte* data, short byteCount)
|
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);
|
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)
|
void SerialChannel::writeShort(short num)
|
||||||
{
|
{
|
||||||
Serial.write((byte*)&num, 2);
|
Serial.write((byte*)&num, 2);
|
||||||
@ -77,6 +126,7 @@ void SerialChannel::handleConnection()
|
|||||||
Serial.write(c->name);
|
Serial.write(c->name);
|
||||||
c = c->next;
|
c = c->next;
|
||||||
}
|
}
|
||||||
|
Serial.flush();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,9 +1,22 @@
|
|||||||
#ifndef _SerialChannel_h_
|
#ifndef _SerialChannel_h_
|
||||||
#define _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
|
class SerialChannel
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
|
static const byte MaxPendingMessages = 16;
|
||||||
|
|
||||||
|
struct Message
|
||||||
|
{
|
||||||
|
byte* data;
|
||||||
|
unsigned long time;
|
||||||
|
short byteCount;
|
||||||
|
byte id;
|
||||||
|
};
|
||||||
|
|
||||||
static SerialChannel* first;
|
static SerialChannel* first;
|
||||||
SerialChannel* next;
|
SerialChannel* next;
|
||||||
|
|
||||||
@ -11,20 +24,31 @@ private:
|
|||||||
byte id;
|
byte id;
|
||||||
const char* name;
|
const char* name;
|
||||||
|
|
||||||
|
static Message buffer1[MaxPendingMessages];
|
||||||
|
static Message buffer2[MaxPendingMessages];
|
||||||
|
static Message* backBuffer;
|
||||||
|
static byte backBufferPos;
|
||||||
|
static byte frontBufferSize;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
SerialChannel(const char* name_);
|
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(byte* data, short byteCount, unsigned long time = (unsigned long)-1);
|
||||||
|
|
||||||
void write(const char* text, 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 beginWrite(short byteCount, unsigned long time = (unsigned long)-1);
|
||||||
void continueWrite(byte* data, short byteCount);
|
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:
|
private:
|
||||||
void handleConnection();
|
static void handleConnection();
|
||||||
void writeShort(short num);
|
static void writeShort(short num);
|
||||||
void writeULong(unsigned long num);
|
static void writeULong(unsigned long num);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -80,7 +80,7 @@ namespace SerialMonitor
|
|||||||
switch (message.ChannelName)
|
switch (message.ChannelName)
|
||||||
{
|
{
|
||||||
case "debug":
|
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);
|
//Debug.WriteLine(text);
|
||||||
MainWindowContext.Get.WriteLine(text);
|
MainWindowContext.Get.WriteLine(text);
|
||||||
break;
|
break;
|
||||||
|
Loading…
Reference in New Issue
Block a user