Skip to content

Commit

Permalink
Merge branch 'develop' into feature/proper-mdns-support
Browse files Browse the repository at this point in the history
  • Loading branch information
hhvrc authored Oct 8, 2024
2 parents 9269344 + 3d1acb7 commit dae41bb
Show file tree
Hide file tree
Showing 42 changed files with 928 additions and 416 deletions.
6 changes: 3 additions & 3 deletions .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ AlignTrailingComments:
Kind: Always
OverEmptyLines: 1
AllowAllArgumentsOnNextLine: true
AllowAllParametersOfDeclarationOnNextLine: false
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: Always
AllowShortCaseLabelsOnASingleLine: false
AllowShortEnumsOnASingleLine: false
Expand All @@ -87,7 +87,7 @@ BraceWrapping:
AfterClass: false
AfterControlStatement: MultiLine
AfterEnum: false
AfterFunction: false
AfterFunction: true
AfterNamespace: false
AfterStruct: false
AfterUnion: false
Expand All @@ -100,7 +100,7 @@ BraceWrapping:
SplitEmptyFunction: true
SplitEmptyRecord: true
SplitEmptyNamespace: true
BreakAfterAttributes: Never
BreakAfterAttributes: Leave
BreakBeforeBinaryOperators: All
BreakBeforeConceptDeclarations: Allowed
BreakBeforeInlineASMColon: Always
Expand Down
10 changes: 10 additions & 0 deletions frontend/src/lib/MessageHandlers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ PayloadHandlers[HubToLocalMessagePayload.ReadyMessage] = (cli, msg) => {
store.accountLinked = payload.accountLinked();
store.config = mapConfig(payload.config());

const gpioValidInputs = payload.gpioValidInputsArray();
if (gpioValidInputs) {
store.gpioValidInputs = gpioValidInputs;
}

const gpioValidOutputs = payload.gpioValidOutputsArray();
if (gpioValidOutputs) {
store.gpioValidOutputs = gpioValidOutputs;
}

console.log('[WS] Updated device state store: ', store);

return store;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ enabled():boolean {
*/
gpioPin():number {
const offset = this.bb!.__offset(this.bb_pos, 6);
return offset ? this.bb!.readUint8(this.bb_pos + offset) : 0;
return offset ? this.bb!.readInt8(this.bb_pos + offset) : 0;
}

static startEStopConfig(builder:flatbuffers.Builder) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ static getSizePrefixedRootAsRFConfig(bb:flatbuffers.ByteBuffer, obj?:RFConfig):R
*/
txPin():number {
const offset = this.bb!.__offset(this.bb_pos, 4);
return offset ? this.bb!.readUint8(this.bb_pos + offset) : 0;
return offset ? this.bb!.readInt8(this.bb_pos + offset) : 0;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,38 @@ config(obj?:HubConfig):HubConfig|null {
return offset ? (obj || new HubConfig()).__init(this.bb!.__indirect(this.bb_pos + offset), this.bb!) : null;
}

gpioValidInputs(index: number):number|null {
const offset = this.bb!.__offset(this.bb_pos, 12);
return offset ? this.bb!.readInt8(this.bb!.__vector(this.bb_pos + offset) + index) : 0;
}

gpioValidInputsLength():number {
const offset = this.bb!.__offset(this.bb_pos, 12);
return offset ? this.bb!.__vector_len(this.bb_pos + offset) : 0;
}

gpioValidInputsArray():Int8Array|null {
const offset = this.bb!.__offset(this.bb_pos, 12);
return offset ? new Int8Array(this.bb!.bytes().buffer, this.bb!.bytes().byteOffset + this.bb!.__vector(this.bb_pos + offset), this.bb!.__vector_len(this.bb_pos + offset)) : null;
}

gpioValidOutputs(index: number):number|null {
const offset = this.bb!.__offset(this.bb_pos, 14);
return offset ? this.bb!.readInt8(this.bb!.__vector(this.bb_pos + offset) + index) : 0;
}

gpioValidOutputsLength():number {
const offset = this.bb!.__offset(this.bb_pos, 14);
return offset ? this.bb!.__vector_len(this.bb_pos + offset) : 0;
}

gpioValidOutputsArray():Int8Array|null {
const offset = this.bb!.__offset(this.bb_pos, 14);
return offset ? new Int8Array(this.bb!.bytes().buffer, this.bb!.bytes().byteOffset + this.bb!.__vector(this.bb_pos + offset), this.bb!.__vector_len(this.bb_pos + offset)) : null;
}

static startReadyMessage(builder:flatbuffers.Builder) {
builder.startObject(4);
builder.startObject(6);
}

static addPoggies(builder:flatbuffers.Builder, poggies:boolean) {
Expand All @@ -66,6 +96,48 @@ static addConfig(builder:flatbuffers.Builder, configOffset:flatbuffers.Offset) {
builder.addFieldOffset(3, configOffset, 0);
}

static addGpioValidInputs(builder:flatbuffers.Builder, gpioValidInputsOffset:flatbuffers.Offset) {
builder.addFieldOffset(4, gpioValidInputsOffset, 0);
}

static createGpioValidInputsVector(builder:flatbuffers.Builder, data:number[]|Int8Array):flatbuffers.Offset;
/**
* @deprecated This Uint8Array overload will be removed in the future.
*/
static createGpioValidInputsVector(builder:flatbuffers.Builder, data:number[]|Uint8Array):flatbuffers.Offset;
static createGpioValidInputsVector(builder:flatbuffers.Builder, data:number[]|Int8Array|Uint8Array):flatbuffers.Offset {
builder.startVector(1, data.length, 1);
for (let i = data.length - 1; i >= 0; i--) {
builder.addInt8(data[i]!);
}
return builder.endVector();
}

static startGpioValidInputsVector(builder:flatbuffers.Builder, numElems:number) {
builder.startVector(1, numElems, 1);
}

static addGpioValidOutputs(builder:flatbuffers.Builder, gpioValidOutputsOffset:flatbuffers.Offset) {
builder.addFieldOffset(5, gpioValidOutputsOffset, 0);
}

static createGpioValidOutputsVector(builder:flatbuffers.Builder, data:number[]|Int8Array):flatbuffers.Offset;
/**
* @deprecated This Uint8Array overload will be removed in the future.
*/
static createGpioValidOutputsVector(builder:flatbuffers.Builder, data:number[]|Uint8Array):flatbuffers.Offset;
static createGpioValidOutputsVector(builder:flatbuffers.Builder, data:number[]|Int8Array|Uint8Array):flatbuffers.Offset {
builder.startVector(1, data.length, 1);
for (let i = data.length - 1; i >= 0; i--) {
builder.addInt8(data[i]!);
}
return builder.endVector();
}

static startGpioValidOutputsVector(builder:flatbuffers.Builder, numElems:number) {
builder.startVector(1, numElems, 1);
}

static endReadyMessage(builder:flatbuffers.Builder):flatbuffers.Offset {
const offset = builder.endObject();
return offset;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ static getSizePrefixedRootAsSetEstopPinCommandResult(bb:flatbuffers.ByteBuffer,

gpioPin():number {
const offset = this.bb!.__offset(this.bb_pos, 4);
return offset ? this.bb!.readUint8(this.bb_pos + offset) : 0;
return offset ? this.bb!.readInt8(this.bb_pos + offset) : 0;
}

result():SetGPIOResultCode {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ static getSizePrefixedRootAsSetEstopPinCommand(bb:flatbuffers.ByteBuffer, obj?:S

pin():number {
const offset = this.bb!.__offset(this.bb_pos, 4);
return offset ? this.bb!.readUint8(this.bb_pos + offset) : 0;
return offset ? this.bb!.readInt8(this.bb_pos + offset) : 0;
}

static startSetEstopPinCommand(builder:flatbuffers.Builder) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ static getSizePrefixedRootAsSetRfTxPinCommandResult(bb:flatbuffers.ByteBuffer, o

pin():number {
const offset = this.bb!.__offset(this.bb_pos, 4);
return offset ? this.bb!.readUint8(this.bb_pos + offset) : 0;
return offset ? this.bb!.readInt8(this.bb_pos + offset) : 0;
}

result():SetGPIOResultCode {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ static getSizePrefixedRootAsSetRfTxPinCommand(bb:flatbuffers.ByteBuffer, obj?:Se

pin():number {
const offset = this.bb!.__offset(this.bb_pos, 4);
return offset ? this.bb!.readUint8(this.bb_pos + offset) : 0;
return offset ? this.bb!.readInt8(this.bb_pos + offset) : 0;
}

static startSetRfTxPinCommand(builder:flatbuffers.Builder) {
Expand Down
46 changes: 46 additions & 0 deletions frontend/src/lib/components/GpioPinSelector.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<script lang="ts">
import { DeviceStateStore } from '$lib/stores';
import { UsedPinsStore } from '$lib/stores/UsedPinsStore';
import { WebSocketClient } from '$lib/WebSocketClient';
export let name: string;
export let currentPin: number | null;
export let serializer: (gpio: number) => Uint8Array;
let pendingPin: number | null = null;
let statusText: string = 'Loading...';
$: canSet = pendingPin !== null && pendingPin !== currentPin && pendingPin >= 0 && pendingPin <= 255 && $DeviceStateStore.gpioValidOutputs.includes(pendingPin) && !$UsedPinsStore.has(pendingPin);
$: if (currentPin !== null) {
UsedPinsStore.markPinUsed(currentPin, name);
statusText = currentPin >= 0 ? 'Currently ' + currentPin : 'Invalid pin';
} else {
statusText = 'Loading...';
}
$: if (pendingPin !== null) {
if (pendingPin < 0) {
pendingPin = null;
} else if (pendingPin > 255) {
pendingPin = 255;
}
}
function setGpioPin() {
if (!canSet) return;
const data = serializer(pendingPin!);
WebSocketClient.Instance.Send(data);
}
</script>

<div class="flex flex-col space-y-2">
<div class="flex flex-row space-x-2 items-center">
<h3 class="h3">{name}</h3>
<span class="text-sm text-gray-500">{statusText}</span>
</div>
<div class="flex space-x-2">
<input class="input variant-form-material" type="number" placeholder="GPIO Pin" bind:value={pendingPin} />
<button class="btn variant-filled" on:click={setGpioPin} disabled={!canSet}>Set</button>
</div>
</div>
2 changes: 2 additions & 0 deletions frontend/src/lib/stores/DeviceStateStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ const { subscribe, update } = writable<DeviceState>({
wifiNetworkGroups: new Map<string, WiFiNetworkGroup>(),
accountLinked: false,
config: null,
gpioValidInputs: new Int8Array(),
gpioValidOutputs: new Int8Array(),
});

function insertSorted<T>(array: T[], value: T, compare: (a: T, b: T) => number) {
Expand Down
21 changes: 21 additions & 0 deletions frontend/src/lib/stores/UsedPinsStore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { writable } from "svelte/store";

const { subscribe, update } = writable<Map<number, string>>(new Map<number, string>());

export const UsedPinsStore = {
subscribe,
markPinUsed(pin: number, name: string) {
update((store) => {
// Remove any existing entries with the same pin or name
for (const [key, value] of store) {
if (key === pin || value === name) {
store.delete(key);
}
}

store.set(pin, name);

return store;
});
}
};
2 changes: 2 additions & 0 deletions frontend/src/lib/types/DeviceState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ export type DeviceState = {
wifiNetworkGroups: Map<string, WiFiNetworkGroup>;
accountLinked: boolean;
config: Config | null;
gpioValidInputs: Int8Array;
gpioValidOutputs: Int8Array;
};
42 changes: 3 additions & 39 deletions frontend/src/routes/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<script lang="ts">
import { browser } from '$app/environment';
import { SerializeAccountLinkCommand } from '$lib/Serializers/AccountLinkCommand';
import { SerializeSetRfTxPinCommand } from '$lib/Serializers/SetRfTxPinCommand';
import { SerializeSetEstopPinCommand } from '$lib/Serializers/SetEstopPinCommand';
import { WebSocketClient } from '$lib/WebSocketClient';
import GpioPinSelector from '$lib/components/GpioPinSelector.svelte';
import WiFiList from '$lib/components/WiFiList.svelte';
import { DeviceStateStore } from '$lib/stores';
Expand All @@ -20,29 +20,11 @@
let linkCode: string = '';
$: linkCodeValid = isValidLinkCode(linkCode);
let rfTxPin: number | null = $DeviceStateStore.config?.rf.txPin ?? null;
$: rfTxPinValid = rfTxPin !== null && rfTxPin >= 0 && rfTxPin < 255;
let estopPin: number | null = $DeviceStateStore.config?.estop.gpioPin ?? null;
$: estopPinValid = estopPin !== null && estopPin >= 0 && estopPin < 255;
function linkAccount() {
if (!linkCodeValid) return;
const data = SerializeAccountLinkCommand(linkCode!);
WebSocketClient.Instance.Send(data);
}
function setRfTxPin() {
if (!rfTxPinValid) return;
const data = SerializeSetRfTxPinCommand(rfTxPin!);
WebSocketClient.Instance.Send(data);
}
function setEstopPin() {
if (!estopPinValid) return;
const data = SerializeSetEstopPinCommand(estopPin!);
WebSocketClient.Instance.Send(data);
}
</script>

<div class="flex flex-col items-center justify-center h-full">
Expand All @@ -57,28 +39,10 @@
</div>
</div>

<div class="flex flex-col space-y-2">
<div class="flex flex-row space-x-2 items-center">
<h3 class="h3">RF TX Pin</h3>
<span class="text-sm text-gray-500">(Currently {$DeviceStateStore.config?.rf == null ? ' unavailable' : $DeviceStateStore.config.rf.txPin}) </span>
</div>
<div class="flex space-x-2">
<input class="input variant-form-material" type="number" placeholder="TX Pin" bind:value={rfTxPin} />
<button class="btn variant-filled" on:click={setRfTxPin} disabled={!rfTxPinValid}>Set</button>
</div>
</div>
<GpioPinSelector name="RF TX Pin" currentPin={$DeviceStateStore.config?.rf?.txPin ?? null} serializer={SerializeSetRfTxPinCommand} />

<!-- TODO: Add EStop Enable/Disable toggle -->

<div class="flex flex-col space-y-2">
<div class="flex flex-row space-x-2 items-center">
<h3 class="h3">EStop GPIO Pin</h3>
<span class="text-sm text-gray-500">(Currently {$DeviceStateStore.config?.estop == null ? ' unavailable' : $DeviceStateStore.config.estop.gpioPin}) </span>
</div>
<div class="flex space-x-2">
<input class="input variant-form-material" type="number" placeholder="EStop Pin" bind:value={estopPin} />
<button class="btn variant-filled" on:click={setEstopPin} disabled={!estopPinValid}>Set</button>
</div>
</div>
<GpioPinSelector name="EStop Pin" currentPin={$DeviceStateStore.config?.estop?.gpioPin ?? null} serializer={SerializeSetEstopPinCommand} />
</div>
</div>
Loading

1 comment on commit dae41bb

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cpp-Linter Report ⚠️

Some files did not pass the configured checks!

clang-format reports: 3 file(s) not formatted
  • include/serialization/_fbs/HubConfig_generated.h
  • include/serialization/_fbs/HubToLocalMessage_generated.h
  • include/serialization/_fbs/LocalToHubMessage_generated.h

Have any feedback or feature suggestions? Share it here.

Please sign in to comment.