Skip to content

Commit

Permalink
add examples/node/streamingData
Browse files Browse the repository at this point in the history
improve python streaming data example
  • Loading branch information
2bndy5 committed Oct 25, 2024
1 parent 1dcf2bb commit 2c76611
Show file tree
Hide file tree
Showing 6 changed files with 292 additions and 83 deletions.
50 changes: 25 additions & 25 deletions examples/node/ts/acknowledgementPayloads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,24 +72,24 @@ export async function setup(): Promise<AppState> {
* The transmitting node's behavior.
* @param count The number of payloads to send
*/
export async function master(example: AppState, count: number | null) {
example.radio.stopListening();
export async function master(app: AppState, count: number | null) {
app.radio.stopListening();
// we'll use a DataView object to store our string and number into a bytearray buffer
const outgoing = Buffer.from("Hello \0.");
for (let i = 0; i < (count || 5); i++) {
outgoing.writeUint8(example.counter, 7);
outgoing.writeUint8(app.counter, 7);
const start = process.hrtime.bigint();
const result = example.radio.send(outgoing);
const result = app.radio.send(outgoing);
const end = process.hrtime.bigint();
if (result) {
const elapsed = (end - start) / BigInt(1000);
process.stdout.write(
`Transmission successful! Time to Transmit: ${elapsed} us. Sent: ` +
`${outgoing.subarray(0, 6).toString()}${example.counter} `,
`${outgoing.subarray(0, 6).toString()}${app.counter} `,
);
example.counter += 1;
if (example.radio.available()) {
const incoming = example.radio.read();
app.counter += 1;
if (app.radio.available()) {
const incoming = app.radio.read();
const counter = incoming.readUint8(7);
console.log(
` Received: ${incoming.subarray(0, 6).toString()}${counter}`,
Expand All @@ -108,36 +108,36 @@ export async function master(example: AppState, count: number | null) {
* The receiving node's behavior.
* @param duration The timeout duration (in seconds) to listen after receiving a payload.
*/
export function slave(example: AppState, duration: number | null) {
example.radio.startListening();
export function slave(app: AppState, duration: number | null) {
app.radio.startListening();
// we'll use a DataView object to store our string and number into a bytearray buffer
const outgoing = Buffer.from("World \0.");
outgoing.writeUint8(example.counter, 7);
example.radio.writeAckPayload(1, outgoing);
outgoing.writeUint8(app.counter, 7);
app.radio.writeAckPayload(1, outgoing);
let timeout = Date.now() + (duration || 6) * 1000;
while (Date.now() < timeout) {
const hasRx = example.radio.availablePipe();
const hasRx = app.radio.availablePipe();
if (hasRx.available) {
const incoming = example.radio.read();
const incoming = app.radio.read();
const counter = incoming.readUint8(7);
console.log(
`Received ${incoming.length} bytes on pipe ${hasRx.pipe}: ` +
`${incoming.subarray(0, 6).toString()}${counter} Sent: ` +
`${outgoing.subarray(0, 6).toString()}${example.counter}`,
`${outgoing.subarray(0, 6).toString()}${app.counter}`,
);
example.counter = counter;
app.counter = counter;
outgoing.writeUint8(counter + 1, 7);
example.radio.writeAckPayload(1, outgoing);
app.radio.writeAckPayload(1, outgoing);
timeout = Date.now() + (duration || 6) * 1000;
}
}
example.radio.stopListening(); // flushes TX FIFO when ACK payloads are enabled
app.radio.stopListening(); // flushes TX FIFO when ACK payloads are enabled
}

/**
* This function prompts the user and performs the specified role for the radio.
*/
export async function setRole(example: AppState): Promise<boolean> {
export async function setRole(app: AppState): Promise<boolean> {
const prompt =
"*** Enter 'T' to transmit\n" +
"*** Enter 'R' to receive\n" +
Expand All @@ -149,25 +149,25 @@ export async function setRole(example: AppState): Promise<boolean> {
}
switch (input[0].charAt(0).toLowerCase()) {
case "t":
await master(example, param);
await master(app, param);
return true;
case "r":
slave(example, param);
slave(app, param);
return true;
default:
console.log(`'${input[0].charAt(0)}' is an unrecognized input`);
return true;
case "q":
example.radio.powerDown();
app.radio.powerDown();
return false;
}
}

export async function main() {
const example = await setup();
while (await setRole(example));
const app = await setup();
while (await setRole(app));
io.close();
example.radio.powerDown();
app.radio.powerDown();
}

main();
34 changes: 17 additions & 17 deletions examples/node/ts/gettingStarted.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,16 @@ export async function setup(): Promise<AppState> {
* The transmitting node's behavior.
* @param count The number of payloads to send
*/
export async function master(example: AppState, count: number | null) {
example.radio.stopListening();
export async function master(app: AppState, count: number | null) {
app.radio.stopListening();
for (let i = 0; i < (count || 5); i++) {
const start = process.hrtime.bigint();
const result = example.radio.send(example.payload);
const result = app.radio.send(app.payload);
const end = process.hrtime.bigint();
if (result) {
const elapsed = (end - start) / BigInt(1000);
console.log(`Transmission successful! Time to Transmit: ${elapsed} us`);
example.payload.writeFloatLE(example.payload.readFloatLE(0) + 0.01, 0);
app.payload.writeFloatLE(app.payload.readFloatLE(0) + 0.01, 0);
} else {
console.log("Transmission failed or timed out!");
}
Expand All @@ -98,28 +98,28 @@ export async function master(example: AppState, count: number | null) {
* The receiving node's behavior.
* @param duration The timeout duration (in seconds) to listen after receiving a payload.
*/
export function slave(example: AppState, duration: number | null) {
example.radio.startListening();
export function slave(app: AppState, duration: number | null) {
app.radio.startListening();
let timeout = Date.now() + (duration || 6) * 1000;
while (Date.now() < timeout) {
const hasRx = example.radio.availablePipe();
const hasRx = app.radio.availablePipe();
if (hasRx.available) {
const incoming = example.radio.read();
example.payload = incoming;
const incoming = app.radio.read();
app.payload = incoming;
const data = incoming.readFloatLE(0);
console.log(
`Received ${incoming.length} bytes on pipe ${hasRx.pipe}: ${data}`,
);
timeout = Date.now() + (duration || 6) * 1000;
}
}
example.radio.stopListening();
app.radio.stopListening();
}

/**
* This function prompts the user and performs the specified role for the radio.
*/
export async function setRole(example: AppState): Promise<boolean> {
export async function setRole(app: AppState): Promise<boolean> {
const prompt =
"*** Enter 'T' to transmit\n" +
"*** Enter 'R' to receive\n" +
Expand All @@ -131,25 +131,25 @@ export async function setRole(example: AppState): Promise<boolean> {
}
switch (input[0].charAt(0).toLowerCase()) {
case "t":
await master(example, param);
await master(app, param);
return true;
case "r":
slave(example, param);
slave(app, param);
return true;
default:
console.log(`'${input[0].charAt(0)}' is an unrecognized input`);
return true;
case "q":
example.radio.powerDown();
app.radio.powerDown();
return false;
}
}

export async function main() {
const example = await setup();
while (await setRole(example));
const app = await setup();
while (await setRole(app));
io.close();
example.radio.powerDown();
app.radio.powerDown();
}

main();
44 changes: 22 additions & 22 deletions examples/node/ts/scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ export function printHeader() {
/**
* The scanner behavior.
*/
export async function scan(example: AppState, duration: number | null) {
export async function scan(app: AppState, duration: number | null) {
printHeader();
const caches = [];
for (let i = 0; i < CHANNELS; i++) {
Expand All @@ -120,17 +120,17 @@ export async function scan(example: AppState, duration: number | null) {

const timeout = Date.now() + (duration || 30) * 1000;
while (Date.now() < timeout) {
example.radio.setChannel(channel);
example.radio.startListening();
app.radio.setChannel(channel);
app.radio.startListening();
await timer.setTimeout(0.13); // needs to be at least 130 microseconds
const rpd = example.radio.rpd;
example.radio.stopListening();
const foundSignal = example.radio.available();
const rpd = app.radio.rpd;
app.radio.stopListening();
const foundSignal = app.radio.available();

caches[channel] += Number(foundSignal || rpd || example.radio.rpd);
caches[channel] += Number(foundSignal || rpd || app.radio.rpd);

if (foundSignal) {
example.radio.flushRx(); // discard any packets (noise) saved in RX FIFO
app.radio.flushRx(); // discard any packets (noise) saved in RX FIFO
}
const total = caches[channel];
process.stdout.write(total > 0 ? total.toString(16) : "-");
Expand Down Expand Up @@ -164,29 +164,29 @@ export async function scan(example: AppState, duration: number | null) {
/**
* Sniff ambient noise and print it out as hexadecimal string.
*/
export function noise(example: AppState, duration: number | null) {
export function noise(app: AppState, duration: number | null) {
const timeout = Date.now() + (duration || 10) * 1000;
example.radio.startListening();
app.radio.startListening();
while (
example.radio.isListening ||
example.radio.getFifoState(false) != FifoState.Empty
app.radio.isListening ||
app.radio.getFifoState(false) != FifoState.Empty
) {
const payload = example.radio.read();
const payload = app.radio.read();
const hexArray = [];
for (let i = 0; i < payload.length; i++) {
hexArray.push(payload[i].toString(16).padStart(2, "0"));
}
console.log(hexArray.join(" "));
if (Date.now() > timeout && example.radio.isListening) {
example.radio.stopListening();
if (Date.now() > timeout && app.radio.isListening) {
app.radio.stopListening();
}
}
}

/**
* This function prompts the user and performs the specified role for the radio.
*/
export async function setRole(example: AppState): Promise<boolean> {
export async function setRole(app: AppState): Promise<boolean> {
const prompt =
"*** Enter 'S' to scan\n" +
"*** Enter 'N' to print noise\n" +
Expand All @@ -198,25 +198,25 @@ export async function setRole(example: AppState): Promise<boolean> {
}
switch (input[0].charAt(0).toLowerCase()) {
case "s":
await scan(example, param);
await scan(app, param);
return true;
case "n":
noise(example, param);
noise(app, param);
return true;
default:
console.log(`'${input[0].charAt(0)}' is an unrecognized input`);
return true;
case "q":
example.radio.powerDown();
app.radio.powerDown();
return false;
}
}

export async function main() {
const example = await setup();
while (await setRole(example));
const app = await setup();
while (await setRole(app));
io.close();
example.radio.powerDown();
app.radio.powerDown();
}

main();
Loading

0 comments on commit 2c76611

Please sign in to comment.