Skip to content

Commit

Permalink
Prep for Dweeb.
Browse files Browse the repository at this point in the history
  • Loading branch information
Onwrikbaar committed Oct 14, 2024
1 parent d2be5c9 commit fe11fd0
Show file tree
Hide file tree
Showing 7 changed files with 1,374 additions and 1,347 deletions.
2,640 changes: 1,319 additions & 1,321 deletions firmware/build/neodk_g071.hex

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion firmware/inc/app_event.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ enum {
ET_DEBUG_SYNC, ET_DATAGRAM_SYNC, ET_INCOMING_PACKET,
ET_CONTROLLER_CONNECTED, ET_CONTROLLER_DISCONNECTED,
ET_PLAY, ET_PAUSE, ET_STOP, ET_UNKNOWN_COMMAND, ET_TOGGLE_PLAY_PAUSE,
ET_NEXT_ROUTINE, ET_SET_INTENSITY, ET_SET_PULSE_WIDTH,
ET_SELECT_NEXT_PATTERN, ET_SELECT_PATTERN_BY_NAME, ET_SET_INTENSITY, ET_SET_PULSE_WIDTH,
ET_BURST_REQUESTED, ET_BURST_REJECTED, ET_BURST_STARTED, ET_BURST_COMPLETED, ET_BURST_EXPIRED,
};

Expand Down
23 changes: 13 additions & 10 deletions firmware/src/controller.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,12 @@ static void readCurrentPatternName(Controller *me, AttributeAction const *aa)

static void readIntensityPercentage(Controller *me, AttributeAction const *aa)
{
uint8_t intensity_percent = Sequencer_getIntensityPercentage(me->sequencer);
uint8_t intensity = Sequencer_getIntensityPercentage(me->sequencer);
uint16_t nbtw = sizeof(PacketHeader) + sizeof(AttributeAction);
uint16_t packet_size = nbtw + Matter_encodedIntegerLength(sizeof intensity_percent);
uint16_t packet_size = nbtw + Matter_encodedIntegerLength(sizeof intensity);
uint8_t packet[packet_size];
initResponsePacket((PacketHeader *)packet, aa);
nbtw += Matter_encodeUnsignedInteger(packet + nbtw, &intensity_percent, sizeof intensity_percent);
nbtw += Matter_encodeUnsignedInteger(packet + nbtw, &intensity, sizeof intensity);
DataLink_sendDatagram(me->datalink, packet, nbtw);
}

Expand Down Expand Up @@ -145,6 +145,11 @@ static void handleWriteRequest(Controller *me, AttributeAction const *aa)
EventQueue_postEvent((EventQueue *)me->sequencer, ET_SET_INTENSITY, &aa->data[1], sizeof aa->data[1]);
}
break;
case AI_CURRENT_PATTERN_NAME:
if (aa->data[0] == EE_UTF8_1LEN) {
EventQueue_postEvent((EventQueue *)me->sequencer, ET_SELECT_PATTERN_BY_NAME, aa->data + 2, aa->data[1]);
}
break;
case AI_PLAY_PAUSE_STOP:
if (aa->data[0] == EE_UTF8_1LEN) {
EventQueue_postEvent((EventQueue *)me->sequencer, eventTypeForCommand(aa->data + 2, aa->data[1]), NULL, 0);
Expand Down Expand Up @@ -248,18 +253,16 @@ void Controller_init(Controller *me, Sequencer *sequencer, DataLink *datalink)
{
me->sequencer = sequencer;
me->datalink = datalink;
BSP_logf("%s\n", __func__);
me->state = &stateIdle;
me->state(me, AOEvent_newEntryEvent());
DataLink_open(me->datalink, &me->event_queue);
DataLink_waitForSync(me->datalink);
}


void Controller_start(Controller *me)
{
BSP_logf("Starting NeoDK!\n");
BSP_logf("%s", welcome_msg);
me->state = &stateIdle;
me->state(me, AOEvent_newEntryEvent());
DataLink_open(me->datalink, &me->event_queue);
DataLink_waitForSync(me->datalink);
BSP_logf("Starting NeoDK!\n%s", welcome_msg);
}


Expand Down
12 changes: 8 additions & 4 deletions firmware/src/datalink.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@
// This module implements:
#include "datalink.h"

#define MAX_PAYLOAD_SIZE 2048
#define MAX_PAYLOAD_SIZE 256

struct _DataLink {
EventQueue *delegate_queue;
uint8_t tx_buf_store[200];
CircBuffer output_buffer;
uint32_t frame_timestamp_µs[8];
uint8_t *rx_frame_buffer;
uint8_t rx_nb;
DeviceId channel_fd;
Expand Down Expand Up @@ -138,9 +139,10 @@ static void handleIncomingFrame(DataLink *me, PhysFrame const *frame)

FrameType frame_type = PhysFrame_type(frame);
if (frame_type == FT_ACK) {
uint8_t ack_nr = PhysFrame_ackNr(frame);
// BSP_logf("Got ACK for frame %hhu\n", ack_nr);
me->tx_seq_nr = (ack_nr + 1) & 0x7;
// uint8_t ack_nr = PhysFrame_ackNr(frame);
// uint32_t ack_delay = BSP_microsecondsSinceBoot() - me->frame_timestamp_µs[ack_nr];
// BSP_logf("Got ACK for frame %hhu after %u µs\n", ack_nr, ack_delay);
// TODO Slide the window.
return;
}

Expand Down Expand Up @@ -226,6 +228,8 @@ static bool sendPacket(DataLink *me, NetworkServiceType nst, uint8_t const *pack
{
uint8_t frame_store[me->header_size + nb];
PhysFrame_init((PhysFrame *)frame_store, FT_DATA, me->tx_seq_nr, nst, packet, nb);
me->frame_timestamp_µs[me->tx_seq_nr] = BSP_microsecondsSinceBoot();
me->tx_seq_nr = (me->tx_seq_nr + 1) & 0x7;
// BSP_logf("Writing frame with size %hu to buffer\n", sizeof frame_store);
return writeFrame(me, frame_store, sizeof frame_store) == sizeof frame_store;
}
Expand Down
11 changes: 7 additions & 4 deletions firmware/src/debug_cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,15 @@ static void interpretCommand(CmndInterp *me, char ch)
switch (ch)
{
case '?':
CLI_logf("Commands: /? /a /b /d /l /n /q /u /v /0 /1../9\n");
CLI_logf("Commands: /? /a /b /d /l /n /q /u /v /w /0 /1../9\n");
break;
case '0':
BSP_primaryVoltageEnable(false);
setPrimaryVoltage_mV(me, 0);
break;
case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
BSP_primaryVoltageEnable(true); // Turn on the buck converter.
setPrimaryVoltage_mV(me, (ch - '0') * 1000U);
BSP_primaryVoltageEnable(true); // Turn on the buck converter.
break;
case 'a':
BSP_triggerADC();
Expand All @@ -92,7 +92,7 @@ static void interpretCommand(CmndInterp *me, char ch)
BSP_toggleTheLED();
break;
case 'n':
EventQueue_postEvent(me->delegate_queue, ET_NEXT_ROUTINE, NULL, 0);
EventQueue_postEvent(me->delegate_queue, ET_SELECT_NEXT_PATTERN, NULL, 0);
break;
case 'q': { // Quit.
int sig = 2; // Simulate Ctrl-C.
Expand All @@ -103,7 +103,10 @@ static void interpretCommand(CmndInterp *me, char ch)
changePrimaryVoltage_mV(me, +100);
break;
case 'v':
CLI_logf("Firmware v0.31-beta\n");
CLI_logf("Firmware v0.32-beta\n");
break;
case 'w': // Allow rediscovery by Dweeb.
DataLink_waitForSync(me->datalink);
break;
default:
CLI_logf("Unknown command '/%c'\n", ch);
Expand Down
4 changes: 2 additions & 2 deletions firmware/src/neodk_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ static void Boss_init(Boss *me)
EventQueue_init(&me->event_queue, me->event_storage, sizeof me->event_storage);
me->controller = Controller_new();
me->sequencer = Sequencer_new();
me->prev_micros = 0;
me->prev_micros = 0ULL;
me->keep_running = true;
}

Expand Down Expand Up @@ -84,7 +84,7 @@ static void dispatchEvent(Boss *me, AOEvent const *evt)
case ET_BUTTON_RELEASED:
// Ignore for now.
break;
case ET_NEXT_ROUTINE:
case ET_SELECT_NEXT_PATTERN:
case ET_SET_PULSE_WIDTH:
EventQueue_repostEvent((EventQueue *)me->sequencer, evt);
break;
Expand Down
29 changes: 24 additions & 5 deletions firmware/src/sequencer.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
*/

#include <stdlib.h>
#include <string.h>

#include "bsp_dbg.h"
#include "bsp_mao.h"
Expand Down Expand Up @@ -127,6 +128,19 @@ static PatternDescr const pattern_descriptors[] =
};


void selectPatternByName(Sequencer *me, char const *name, EventSize len)
{
for (uint8_t i = 0; i < me->nr_of_patterns; i++) {
char const *pattern_name = me->pattern_descr[i].name;
if (len == strlen(pattern_name) && memcmp(name, pattern_name, len) == 0) {
BSP_logf("%s '%s'\n", __func__, pattern_name);
me->pattern_index = i;
return;
}
}
}


static void setPulseWidth(Sequencer *me, uint8_t width_µs)
{
BSP_logf("Setting pulse width to %hhu µs\n", width_µs);
Expand All @@ -143,11 +157,10 @@ static void setIntensityPercentage(Sequencer *me, uint8_t perc)
}


static void selectNextRoutine(Sequencer *me)
static void switchPattern(Sequencer *me)
{
setIntensityPercentage(me, DEFAULT_INTENSITY_PERCENT);
if (++me->pattern_index == me->nr_of_patterns) me->pattern_index = 0;
PatternDescr const *pd = &me->pattern_descr[me->pattern_index];
setIntensityPercentage(me, DEFAULT_INTENSITY_PERCENT);
CLI_logf("Switching to '%s'\n", pd->name);
PatternIterator_init(&me->pi, pd, me->pulse_width);
}
Expand Down Expand Up @@ -176,8 +189,13 @@ static void *stateCanopy(Sequencer *me, AOEvent const *evt)
case ET_ADC_DATA_AVAILABLE:
printAdcValues((uint16_t const *)AOEvent_data(evt));
break;
case ET_NEXT_ROUTINE:
selectNextRoutine(me);
case ET_SELECT_NEXT_PATTERN:
if (++me->pattern_index == me->nr_of_patterns) me->pattern_index = 0;
switchPattern(me);
break;
case ET_SELECT_PATTERN_BY_NAME:
selectPatternByName(me, (char const *)AOEvent_data(evt), AOEvent_dataSize(evt));
switchPattern(me);
break;
case ET_SET_INTENSITY:
setIntensityPercentage(me, *(uint8_t const *)AOEvent_data(evt));
Expand Down Expand Up @@ -284,6 +302,7 @@ static void *statePulsing(Sequencer *me, AOEvent const *evt)
BSP_logf("%s EXIT\n", __func__);
break;
case ET_TOGGLE_PLAY_PAUSE:
case ET_PAUSE:
return &statePaused; // Transition.
case ET_STOP:
return &stateIdle; // Transition.
Expand Down

0 comments on commit fe11fd0

Please sign in to comment.