Skip to content

Commit

Permalink
fix: handle button tracking at device level
Browse files Browse the repository at this point in the history
  • Loading branch information
pelikhan committed Jan 20, 2022
1 parent 7f268fa commit 420c141
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 28 deletions.
2 changes: 1 addition & 1 deletion Jacdac.NET.Playground/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"profiles": {
"Jacdac.NET.Playground": {
"commandName": "Project",
"commandLineArgs": "iothub settings sounds devtools",
"commandLineArgs": "iothub settings sounds devtools buttontracker",
"hotReloadEnabled": false
}
}
Expand Down
28 changes: 26 additions & 2 deletions Jacdac/Device.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ public static string ShortDeviceId(string devid)
private JDBus bus;
public readonly string DeviceId;
public readonly string ShortId;
public TimeSpan LastSeen;
public uint EventCounter;
public TimeSpan LastSeen { get; internal set; }
public uint EventCounter { get; private set; }

byte[] _servicesData;
JDService[] _services = JDService.EmptyServices;
Expand Down Expand Up @@ -119,13 +119,37 @@ public void ProcessPacket(Packet pkt)
this.ReceiveAck(pkt);
else
{
if (pkt.IsEvent)
this.MarkRepeatedEvent(pkt);

var srvs = this._services;
var i = pkt.ServiceIndex;
if (srvs != null && i < srvs.Length)
srvs[i].ProcessPacket(pkt);
}
}

private void MarkRepeatedEvent(Packet pkt)
{
var ec = this.EventCounter + 1;
// how many packets ahead and behind current are we?
var ahead = (pkt.EventCounter - ec) & Jacdac.Constants.CMD_EVENT_COUNTER_MASK;
var behind = (ec - pkt.EventCounter) & Jacdac.Constants.CMD_EVENT_COUNTER_MASK;
// ahead == behind == 0 is the usual case, otherwise
// behind < 60 means this is an old event (or retransmission of something we already processed)
var old = behind < 60;
var missed5 = ahead < 5;
var isahead = ahead > 0;

// ahead < 5 means we missed at most 5 events,
// so we ignore this one and rely on retransmission
// of the missed events, and then eventually the current event
if (isahead && (old || missed5))
pkt.IsRepeated = true;
else
this.EventCounter = pkt.EventCounter;
}

public void ProcessAnnouncement(Packet pkt)
{
var data = pkt.Data;
Expand Down
27 changes: 3 additions & 24 deletions Jacdac/Event.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public sealed partial class JDEvent : JDServiceNode
{
private byte[] data;
private TimeSpan lastGetTimestamp = TimeSpan.MinValue;
public uint Count = 0;
public uint Count { get; private set; } = 0;

internal JDEvent(JDService service, ushort code)
: base(service, code)
Expand All @@ -33,32 +33,11 @@ public override string ToString()

internal bool ProcessPacket(Packet pkt)
{
var device = this.Service.Device;
if (device?.Bus == null) return false;
if (pkt.IsRepeated) return false;

var ec = device.EventCounter + 1;
// how many packets ahead and behind current are we?
var ahead = (pkt.EventCounter - ec) & Jacdac.Constants.CMD_EVENT_COUNTER_MASK;
var behind = (ec - pkt.EventCounter) & Jacdac.Constants.CMD_EVENT_COUNTER_MASK;
// ahead == behind == 0 is the usual case, otherwise
// behind < 60 means this is an old event (or retransmission of something we already processed)
var old = behind < 60;
var missed5 = ahead < 5;
var isahead = ahead > 0;

// ahead < 5 means we missed at most 5 events,
// so we ignore this one and rely on retransmission
// of the missed events, and then eventually the current event
if (isahead && (old || missed5)) return false;

this.data = pkt.Data;
this.lastGetTimestamp = pkt.Timestamp;
this.Count++;

// update device counter
device.EventCounter = pkt.EventCounter;
this.RaiseChanged();

// update device counter
if (this.Code == (ushort)SystemEvent.StatusCodeChanged)
{
var reg = this.Service.GetRegister((ushort)SystemReg.StatusCode);
Expand Down
2 changes: 1 addition & 1 deletion Jacdac/LedPixelProgramEncoding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public LedPixelProgramBuilder Range(uint startIndex, uint length)
}

/// <summary>
/// range from pixel P, Npixels long (currently unsupported: every Wpixels skip Spixels)
/// Set the LED update mode, use temp if the mode should be used once only
/// </summary>
public LedPixelProgramBuilder SetUpdateMode(LedPixelProgramUpdateMode mode, bool temp = false)
{
Expand Down
1 change: 1 addition & 0 deletions Jacdac/Packet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public sealed class Packet
private readonly byte[] header;
private readonly byte[] data;
public TimeSpan Timestamp { get; set; }
public bool IsRepeated { get; internal set; }

public static readonly Packet[] EmptyFrame = new Packet[0];
public static readonly byte[] EmptyData = new byte[0];
Expand Down

0 comments on commit 420c141

Please sign in to comment.