Skip to content

Commit

Permalink
Reorganising.
Browse files Browse the repository at this point in the history
  • Loading branch information
Onwrikbaar committed Oct 13, 2024
1 parent 101b9a4 commit d2be5c9
Show file tree
Hide file tree
Showing 16 changed files with 1,691 additions and 1,603 deletions.
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"files.associations": {
"matter.h": "c"
}
}
1 change: 1 addition & 0 deletions firmware/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ PROJ_COMMON_SRC += \
$(PROJ_DIR_SRC)/pattern_iter.c \
$(PROJ_DIR_SRC)/datalink.c \
$(PROJ_DIR_SRC)/debug_cli.c \
$(PROJ_DIR_SRC)/matter.c \

# Target-dependent include folders.
STM32G0xx_INC += \
Expand Down
2,876 changes: 1,438 additions & 1,438 deletions firmware/build/neodk_g071.hex

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions firmware/inc/app_event.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ enum {
ET_APP_HEARTBEAT, ET_ADC_DATA_AVAILABLE,
ET_DEBUG_SYNC, ET_DATAGRAM_SYNC, ET_INCOMING_PACKET,
ET_CONTROLLER_CONNECTED, ET_CONTROLLER_DISCONNECTED,
ET_CONTROLLER_SENT_REQUEST, ET_CONTROLLER_SENT_CHOICE,
ET_SEQUENCER_READY, ET_PLAY_PAUSE, ET_NEXT_ROUTINE,
ET_PLAY, ET_PAUSE, ET_STOP, ET_UNKNOWN_COMMAND, ET_TOGGLE_PLAY_PAUSE,
ET_NEXT_ROUTINE, ET_SET_INTENSITY, ET_SET_PULSE_WIDTH,
ET_BURST_REQUESTED, ET_BURST_REJECTED, ET_BURST_STARTED, ET_BURST_COMPLETED, ET_BURST_EXPIRED,
};

Expand Down
1 change: 0 additions & 1 deletion firmware/inc/bsp_app.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ int BSP_closeSerialPort(int fd);

// Pulse generation related functions.
uint16_t BSP_setPrimaryVoltage_mV(uint16_t V_prim_mV);
uint16_t BSP_changePrimaryVoltage_mV(int16_t delta_mV);
void BSP_primaryVoltageEnable(bool must_be_on);
bool BSP_startPulseTrain(PulseTrain const *);

Expand Down
17 changes: 17 additions & 0 deletions firmware/inc/matter.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#ifndef INC_MATTER_H_
#define INC_MATTER_H_

#include <stdint.h>

typedef enum {
OC_NONE, OC_STATUS_RESPONSE, OC_READ_REQUEST, OC_SUBSCRIBE_REQUEST, OC_SUBSCRIBE_RESPONSE, OC_REPORT_DATA,
OC_WRITE_REQUEST, OC_WRITE_RESPONSE, OC_INVOKE_REQUEST, OC_INVOKE_RESPONSE, OC_TIMED_REQUEST
Expand All @@ -27,4 +29,19 @@ typedef enum {
EE_NULL, EE_STRUCT, EE_ARRAY, EE_LIST, EE_END_OF_CONTAINER
} ElementEncoding;

#ifdef __cplusplus
extern "C" {
#endif

uint16_t Matter_encodedIntegerLength(uint8_t nr_of_octets);
uint16_t Matter_encodeUnsignedInteger(uint8_t dst[], uint8_t const *src, uint8_t nr_of_octets);
uint16_t Matter_encodedStringLength(char const *str);
uint16_t Matter_encodeString(uint8_t dst[], char const *str);
uint16_t Matter_encodedStringArrayLength(char const *strings[], uint8_t nr_of_strings);
uint16_t Matter_encodeStringArray(uint8_t dst[], char const *strings[], uint8_t nr_of_strings);

#ifdef __cplusplus
}
#endif

#endif
2 changes: 1 addition & 1 deletion firmware/inc/pattern_iter.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ typedef struct {
bool PatternIterator_checkPattern(uint8_t const pattern[][2], uint16_t nr_of_elcons);

// Instance methods.
void PatternIterator_init(PatternIterator *, PatternDescr const *);
void PatternIterator_init(PatternIterator *, PatternDescr const *, uint8_t pulse_width);
bool PatternIterator_done(PatternIterator *);
bool PatternIterator_getNextPulseTrain(PatternIterator *, PulseTrain *);

Expand Down
3 changes: 0 additions & 3 deletions firmware/inc/sequencer.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,8 @@

#include <stdint.h>

#define DEFAULT_PRIMARY_VOLTAGE_mV 1200

typedef struct _Sequencer Sequencer; // Opaque type.


// Class method.
Sequencer *Sequencer_new(void);

Expand Down
9 changes: 0 additions & 9 deletions firmware/src/bsp_stm32g071.c
Original file line number Diff line number Diff line change
Expand Up @@ -824,15 +824,6 @@ uint16_t BSP_setPrimaryVoltage_mV(uint16_t V_prim_mV)
}


uint16_t BSP_changePrimaryVoltage_mV(int16_t delta_mV)
{
int32_t soll_mV = (int32_t)bsp.V_prim_mV + delta_mV;
if (soll_mV < VPRIM_MIN_mV) soll_mV = VPRIM_MIN_mV;
else if (soll_mV > VPRIM_MAX_mV) soll_mV = VPRIM_MAX_mV;
return BSP_setPrimaryVoltage_mV(soll_mV);
}


bool BSP_startPulseTrain(PulseTrain const *pt)
{
M_ASSERT(pt->pace_ms >= 5); // Repetition rate <= 200 Hz.
Expand Down
126 changes: 38 additions & 88 deletions firmware/src/controller.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* controller.c
* controller.c -- the interface to the controlling client
*
* NOTICE (do not remove):
* This file is part of project NeoDK (https://github.com/Onwrikbaar/NeoDK).
Expand All @@ -12,7 +12,6 @@

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

#include "bsp_dbg.h"
#include "bsp_app.h"
Expand All @@ -30,9 +29,8 @@ typedef struct {
uint8_t message[0];
} PacketHeader;


typedef enum {
AI_ALL_PATTERN_NAMES = 5, AI_CURRENT_PATTERN_NAME, AI_INTENSITY
AI_ALL_PATTERN_NAMES = 5, AI_CURRENT_PATTERN_NAME, AI_INTENSITY_PERCENT, AI_PLAY_PAUSE_STOP
} AttributeId;

typedef struct {
Expand All @@ -58,78 +56,6 @@ struct _Controller {
static uint8_t const welcome_msg[] = "Push the button to play or pause :-)\n";


static uint16_t encodedIntegerLength(uint8_t nr_of_octets)
{
return 1 + nr_of_octets;
}


static uint16_t encodeUnsignedInteger(uint8_t dst[], uint8_t const *src, uint8_t nr_of_octets)
{
switch (nr_of_octets)
{
case 1: dst[0] = EE_UNSIGNED_INT_1; break;
case 2: dst[0] = EE_UNSIGNED_INT_2; break;
case 4: dst[0] = EE_UNSIGNED_INT_4; break;
case 8: dst[0] = EE_UNSIGNED_INT_8; break;
default:
nr_of_octets = 1;
dst[0] = EE_UNSIGNED_INT_1;
}
uint16_t nbe = 1;
for (uint8_t i = 0; i < nr_of_octets; i++) {
dst[nbe++] = src[i];
}
return nbe;
}


static uint16_t encodedStringLength(char const *str)
{
uint16_t nbe = strlen(str);
return nbe + (nbe < 256 ? 2 : 3);
}


static uint16_t encodeString(uint8_t dst[], char const *str)
{
uint16_t nbe = 0;
uint16_t str_len = strlen(str);
if (str_len < 256) {
dst[nbe++] = EE_UTF8_1LEN;
dst[nbe++] = str_len;
} else {
dst[nbe++] = EE_UTF8_2LEN;
dst[nbe++] = str_len;
dst[nbe++] = str_len >> 8;
}
memcpy(dst + nbe, str, str_len);
return nbe + str_len;
}


static uint16_t encodedStringArrayLength(char const *strings[], uint8_t nr_of_strings)
{
uint16_t nbe = 2; // 2 bytes to encode array.
for (uint8_t i = 0; i < nr_of_strings; i++) {
nbe += encodedStringLength(strings[i]);
}
return nbe;
}


static uint16_t encodeStringArray(uint8_t dst[], char const *strings[], uint8_t nr_of_strings)
{
uint16_t nbe = 0;
dst[nbe++] = EE_ARRAY;
for (uint8_t i = 0; i < nr_of_strings; i++) {
nbe += encodeString(dst + nbe, strings[i]);
}
dst[nbe++] = EE_END_OF_CONTAINER;
return nbe;
}


static void initResponsePacket(PacketHeader *ph, AttributeAction const *req_aa)
{
ph->flags = 0x00;
Expand All @@ -148,11 +74,11 @@ static void readPatternNames(Controller *me, AttributeAction const *aa)
char const *pattern_names[nr_of_patterns]; // Reserve enough space.
Sequencer_getPatternNames(me->sequencer, pattern_names, nr_of_patterns);
uint16_t nbtw = sizeof(PacketHeader) + sizeof(AttributeAction);
uint16_t packet_size = nbtw + encodedStringArrayLength(pattern_names, nr_of_patterns);
uint16_t packet_size = nbtw + Matter_encodedStringArrayLength(pattern_names, nr_of_patterns);
// TODO Ensure packet_size does not exceed max frame payload size.
uint8_t packet[packet_size];
initResponsePacket((PacketHeader *)packet, aa);
nbtw += encodeStringArray(packet + nbtw, pattern_names, nr_of_patterns);
nbtw += Matter_encodeStringArray(packet + nbtw, pattern_names, nr_of_patterns);
DataLink_sendDatagram(me->datalink, packet, nbtw);
}

Expand All @@ -161,22 +87,22 @@ static void readCurrentPatternName(Controller *me, AttributeAction const *aa)
{
char const *cpn = Sequencer_getCurrentPatternName(me->sequencer);
uint16_t nbtw = sizeof(PacketHeader) + sizeof(AttributeAction);
uint16_t packet_size = nbtw + encodedStringLength(cpn);
uint16_t packet_size = nbtw + Matter_encodedStringLength(cpn);
uint8_t packet[packet_size];
initResponsePacket((PacketHeader *)packet, aa);
nbtw += encodeString(packet + nbtw, cpn);
nbtw += Matter_encodeString(packet + nbtw, cpn);
DataLink_sendDatagram(me->datalink, packet, nbtw);
}


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

Expand All @@ -191,7 +117,7 @@ static void handleReadRequest(Controller *me, AttributeAction const *aa)
case AI_CURRENT_PATTERN_NAME:
readCurrentPatternName(me, aa);
break;
case AI_INTENSITY:
case AI_INTENSITY_PERCENT:
readIntensityPercentage(me, aa);
break;
default:
Expand All @@ -201,12 +127,28 @@ static void handleReadRequest(Controller *me, AttributeAction const *aa)
}


static EventType eventTypeForCommand(uint8_t const *cs, uint16_t len)
{
if (len == 4 && memcmp(cs, "play", len) == 0) return ET_PLAY;
if (len == 5 && memcmp(cs, "pause", len) == 0) return ET_PAUSE;
if (len == 4 && memcmp(cs, "stop", len) == 0) return ET_STOP;
return ET_UNKNOWN_COMMAND;
}


static void handleWriteRequest(Controller *me, AttributeAction const *aa)
{
switch (aa->attribute_id)
{
case AI_INTENSITY:
// TODO Send the new intensity to the sequencer.
case AI_INTENSITY_PERCENT:
if (aa->data[0] == EE_UNSIGNED_INT_1) {
EventQueue_postEvent((EventQueue *)me->sequencer, ET_SET_INTENSITY, &aa->data[1], sizeof 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);
}
break;
default:
BSP_logf("%s: unknown attribute id=%hu\n", __func__, aa->attribute_id);
Expand All @@ -215,6 +157,12 @@ static void handleWriteRequest(Controller *me, AttributeAction const *aa)
}


static void handleInvokeRequest(Controller *me, AttributeAction const *aa)
{
BSP_logf("%s not implemented yet\n", __func__);
}


static void handleRequest(Controller *me, AttributeAction const *aa)
{
switch (aa->opcode)
Expand All @@ -227,6 +175,10 @@ static void handleRequest(Controller *me, AttributeAction const *aa)
BSP_logf("Transaction %hu: write attribute %hu\n", aa->transaction_id, aa->attribute_id);
handleWriteRequest(me, aa);
break;
case OC_INVOKE_REQUEST:
BSP_logf("Transaction %hu: invoke %hu\n", aa->transaction_id, aa->attribute_id);
handleInvokeRequest(me, aa);
break;
default:
BSP_logf("%s, unknown opcode 0x%02hhx\n", __func__, aa->opcode);
break;
Expand Down Expand Up @@ -308,8 +260,6 @@ void Controller_start(Controller *me)
{
BSP_logf("Starting NeoDK!\n");
BSP_logf("%s", welcome_msg);
BSP_setPrimaryVoltage_mV(DEFAULT_PRIMARY_VOLTAGE_mV);
BSP_primaryVoltageEnable(true);
}


Expand Down
9 changes: 4 additions & 5 deletions firmware/src/datalink.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,22 +109,21 @@ static void respondWithAckFrame(DataLink *me, uint8_t ack_nr, NetworkServiceType
{
uint8_t ack_frame[me->header_size];
PhysFrame_initHeaderWithAck((PhysFrame *)ack_frame, FT_ACK, 0, ack_nr, nst);
BSP_logf("%s(%hhu) to controller\n", __func__, ack_nr);
// BSP_logf("%s(%hhu) to controller\n", __func__, ack_nr);
writeFrame(me, ack_frame, sizeof ack_frame);
}


static void handleIncomingDataFrame(DataLink *me, PhysFrame const *frame)
{
NetworkServiceType nst = PhysFrame_serviceType(frame);
uint8_t rx_seq_nr = PhysFrame_seqNr(frame);
uint8_t const *payload = PhysFrame_payload(frame);
uint16_t payload_size = PhysFrame_payloadSize(frame);
if (nst == NST_DEBUG) {
BSP_logf("Got debug frame, seq_nr=%hhu, command length=%hu\n", rx_seq_nr, payload_size);
// BSP_logf("Got debug frame, seq_nr=%hhu, command length=%hu\n", PhysFrame_seqNr(frame), payload_size);
CLI_handleRemoteInput(payload, payload_size);
} else {
BSP_logf("Got packet frame, seq_nr=%hhu, packet size=%hu\n", rx_seq_nr, payload_size);
// BSP_logf("Got packet frame, seq_nr=%hhu, packet size=%hu\n", PhysFrame_seqNr(frame), payload_size);
EventQueue_postEvent(me->delegate_queue, ET_INCOMING_PACKET, payload, payload_size);
}
}
Expand All @@ -140,7 +139,7 @@ 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);
// BSP_logf("Got ACK for frame %hhu\n", ack_nr);
me->tx_seq_nr = (ack_nr + 1) & 0x7;
return;
}
Expand Down
Loading

0 comments on commit d2be5c9

Please sign in to comment.