Skip to content

Commit

Permalink
Ready for Dweeb.
Browse files Browse the repository at this point in the history
  • Loading branch information
Onwrikbaar committed Oct 16, 2024
1 parent 2d7c368 commit dd141e7
Show file tree
Hide file tree
Showing 11 changed files with 1,524 additions and 1,515 deletions.
2,920 changes: 1,462 additions & 1,458 deletions firmware/build/neodk_g071.hex

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions firmware/inc/attributes.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ typedef enum {
typedef void (*AttrNotifier)(void *target, AttributeId, ElementEncoding, uint8_t const *data, uint16_t size);


bool Attribute_subscribe(AttributeId, ElementEncoding, AttrNotifier, void *target);
void Attribute_changed(AttributeId, uint8_t const *data, uint16_t size);
SubscriptionId Attribute_subscribe(AttributeId, AttrNotifier, void *target);
void Attribute_changed(AttributeId, ElementEncoding, uint8_t const *data, uint16_t size);

#endif
9 changes: 7 additions & 2 deletions firmware/inc/sequencer.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,15 @@ Sequencer *Sequencer_new(void);
Sequencer *Sequencer_init(Sequencer *);
void Sequencer_start(Sequencer *);
bool Sequencer_handleEvent(Sequencer *);
uint16_t Sequencer_getNrOfPatterns(Sequencer const *me);

uint16_t Sequencer_getNrOfPatterns(Sequencer const *);
void Sequencer_getPatternNames(Sequencer const *, char const *[], uint8_t);
char const *Sequencer_getCurrentPatternName(Sequencer const *);
uint8_t Sequencer_getIntensityPercentage(Sequencer const *);

void Sequencer_notifyIntensity(Sequencer const *);
void Sequencer_notifyPattern(Sequencer const *);
void Sequencer_notifyPlayState(Sequencer const *);

void Sequencer_stop(Sequencer *);
void Sequencer_delete(Sequencer *);

Expand Down
2 changes: 1 addition & 1 deletion firmware/maolib/inc/ao_event.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*
* Created on: 11 Apr 2021
* Author: mark
* Copyright 2021..2024 Neostim
* Copyright 2021..2024 Neostim
*/

#ifndef INC_AO_EVENT_H_
Expand Down
4 changes: 3 additions & 1 deletion firmware/maolib/inc/matter.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ typedef enum {
EE_NULL, EE_STRUCT, EE_ARRAY, EE_LIST, EE_END_OF_CONTAINER
} ElementEncoding;

typedef uint16_t SubscriptionId;

#ifdef __cplusplus
extern "C" {
#endif
Expand All @@ -40,7 +42,7 @@ 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);
uint16_t Matter_encodedDataLength(ElementEncoding enc, uint16_t nr_of_octets);
uint16_t Matter_encodeData(uint8_t dst[], ElementEncoding, uint8_t const *src, uint16_t nr_of_octets);
uint16_t Matter_encodeScalarData(uint8_t dst[], ElementEncoding, uint8_t const *src, uint16_t nr_of_octets);

#ifdef __cplusplus
}
Expand Down
Binary file modified firmware/maolib/libmao.a
Binary file not shown.
15 changes: 7 additions & 8 deletions firmware/src/attributes.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,25 +42,24 @@ static Subscription const *findSubForId(AttributeId ai)
* Below are the functions implementing this module's interface.
*/

bool Attribute_subscribe(AttributeId ai, ElementEncoding enc, AttrNotifier notify, void *target)
SubscriptionId Attribute_subscribe(AttributeId ai, AttrNotifier notify, void *target)
{
BSP_logf("%s for id=%hu\n", __func__, ai);
if (nr_of_subs == M_DIM(subscriptions)) return false;
// BSP_logf("%s for id=%hu\n", __func__, ai);
if (nr_of_subs == M_DIM(subscriptions)) return 0;

Subscription *sub = &subscriptions[nr_of_subs++];
Subscription *sub = &subscriptions[nr_of_subs];
sub->ai = ai;
sub->encoding = enc;
sub->notify = notify;
sub->target = target;
return true;
return 256 + nr_of_subs++;
}


void Attribute_changed(AttributeId ai, uint8_t const *data, uint16_t size)
void Attribute_changed(AttributeId ai, ElementEncoding enc, uint8_t const *data, uint16_t size)
{
// BSP_logf("%s(%hu)\n", __func__, ai);
Subscription const *sub = findSubForId(ai);
if (sub != NULL) {
sub->notify(sub->target, ai, sub->encoding, data, size);
sub->notify(sub->target, ai, enc, data, size);
}
}
6 changes: 4 additions & 2 deletions firmware/src/bsp_stm32g071.c
Original file line number Diff line number Diff line change
Expand Up @@ -816,8 +816,10 @@ void BSP_primaryVoltageEnable(bool must_be_on)

uint16_t BSP_setPrimaryVoltage_mV(uint16_t V_prim_mV)
{
BSP_logf("Setting Vcap to %hu mV\n", V_prim_mV);
bsp.V_prim_mV = V_prim_mV;
if (bsp.V_prim_mV != V_prim_mV) {
BSP_logf("Setting Vcap to %hu mV\n", V_prim_mV);
bsp.V_prim_mV = V_prim_mV;
}
// TODO Ensure a rising step on the buck regulator's feedback pin does not exceed 80 mV.
DAC1->DHR12R2 = Vcap_mV_ToDacVal(V_prim_mV);
return bsp.V_prim_mV;
Expand Down
50 changes: 17 additions & 33 deletions firmware/src/controller.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,32 +84,6 @@ static void readPatternNames(Controller *me, AttributeAction const *aa)
}


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 + Matter_encodedStringLength(cpn);
uint8_t packet[packet_size];
initResponsePacket((PacketHeader *)packet);
initAttributeAction((AttributeAction *)(packet + sizeof(PacketHeader)), aa);
nbtw += Matter_encodeString(packet + nbtw, cpn);
DataLink_sendDatagram(me->datalink, packet, nbtw);
}


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


static void attributeChanged(Controller *me, AttributeId ai, ElementEncoding enc, uint8_t const *data, uint16_t data_size)
{
// BSP_logf("Controller_%s(%hu) size=%hu\n", __func__, ai, data_size);
Expand All @@ -123,7 +97,7 @@ static void attributeChanged(Controller *me, AttributeId ai, ElementEncoding enc
aa->reserved = 0;
aa->attribute_id = ai;
// TODO Add subscription Id?
nbtw += Matter_encodeData(packet + nbtw, enc, data, data_size);
nbtw += Matter_encodeScalarData(packet + nbtw, enc, data, data_size);
DataLink_sendDatagram(me->datalink, packet, nbtw);
}

Expand All @@ -136,10 +110,13 @@ static void handleReadRequest(Controller *me, AttributeAction const *aa)
readPatternNames(me, aa);
break;
case AI_CURRENT_PATTERN_NAME:
readCurrentPatternName(me, aa);
Sequencer_notifyPattern(me->sequencer);
break;
case AI_INTENSITY_PERCENT:
readIntensityPercentage(me, aa);
Sequencer_notifyIntensity(me->sequencer);
break;
case AI_PLAY_PAUSE_STOP:
Sequencer_notifyPlayState(me->sequencer);
break;
default:
BSP_logf("%s: unknown attribute id=%hu\n", __func__, aa->attribute_id);
Expand Down Expand Up @@ -183,6 +160,13 @@ static void handleWriteRequest(Controller *me, AttributeAction const *aa)
}


static void handleSubscribeRequest(Controller *me, AttributeAction const *aa)
{
Attribute_subscribe(aa->attribute_id, (AttrNotifier)&attributeChanged, me);
handleReadRequest(me, aa);
}


static void handleInvokeRequest(Controller *me, AttributeAction const *aa)
{
BSP_logf("%s not implemented yet\n", __func__);
Expand All @@ -201,6 +185,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_SUBSCRIBE_REQUEST:
BSP_logf("Transaction %hu: subscribe to attribute %hu\n", aa->transaction_id, aa->attribute_id);
handleSubscribeRequest(me, aa);
break;
case OC_INVOKE_REQUEST:
BSP_logf("Transaction %hu: invoke %hu\n", aa->transaction_id, aa->attribute_id);
handleInvokeRequest(me, aa);
Expand Down Expand Up @@ -283,10 +271,6 @@ void Controller_start(Controller *me)
me->state(me, AOEvent_newEntryEvent());
DataLink_open(me->datalink, &me->event_queue);
DataLink_waitForSync(me->datalink);
// In practice these subscriptions will be made by the controlling client.
Attribute_subscribe(AI_CURRENT_PATTERN_NAME, EE_UTF8_1LEN, (AttrNotifier)&attributeChanged, me);
Attribute_subscribe(AI_INTENSITY_PERCENT, EE_UNSIGNED_INT_1, (AttrNotifier)&attributeChanged, me);
Attribute_subscribe(AI_PLAY_PAUSE_STOP, EE_UNSIGNED_INT_1, (AttrNotifier)&attributeChanged, me);
BSP_logf("Starting NeoDK!\n%s", welcome_msg);
}

Expand Down
2 changes: 1 addition & 1 deletion firmware/src/debug_cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ static void interpretCommand(CmndInterp *me, char ch)
changeIntensity(me, +2);
break;
case 'v':
CLI_logf("Firmware v0.33-beta\n");
CLI_logf("Firmware v0.34-beta\n");
break;
case 'w': // Allow rediscovery by Dweeb.
DataLink_waitForSync(me->datalink);
Expand Down
27 changes: 20 additions & 7 deletions firmware/src/sequencer.c
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ static void setIntensityPercentage(Sequencer *me, uint8_t perc)
BSP_logf("Setting intensity to %hhu%%\n", perc);
me->intensity_percent = perc;
// TODO Ramp up?
Attribute_changed(AI_INTENSITY_PERCENT, &me->intensity_percent, sizeof me->intensity_percent);
Sequencer_notifyIntensity(me);
BSP_setPrimaryVoltage_mV(perc * 100);
setPulseWidth(me, 40 + perc);
}
Expand All @@ -175,7 +175,7 @@ static void switchPattern(Sequencer *me)
{
PatternDescr const *pd = &me->pattern_descr[me->pattern_index];
CLI_logf("Switching to '%s'\n", pd->name);
Attribute_changed(AI_CURRENT_PATTERN_NAME, (uint8_t const *)pd->name, strlen(pd->name));
Sequencer_notifyPattern(me);
setIntensityPercentage(me, DEFAULT_INTENSITY_PERCENT);
PatternIterator_init(&me->pi, pd, me->pulse_width);
}
Expand All @@ -184,7 +184,7 @@ static void switchPattern(Sequencer *me)
static void setPlayState(Sequencer *me, PlayState play_state)
{
me->play_state = play_state;
Attribute_changed(AI_PLAY_PAUSE_STOP, &me->play_state, sizeof me->play_state);
Sequencer_notifyPlayState(me);
}


Expand Down Expand Up @@ -418,15 +418,28 @@ void Sequencer_getPatternNames(Sequencer const *me, char const *names[], uint8_t
}


char const *Sequencer_getCurrentPatternName(Sequencer const *me)
uint8_t Sequencer_getIntensityPercentage(Sequencer const *me)
{
return me->pattern_descr[me->pattern_index].name;
return me->intensity_percent;
}


uint8_t Sequencer_getIntensityPercentage(Sequencer const *me)
void Sequencer_notifyIntensity(Sequencer const *me)
{
return me->intensity_percent;
Attribute_changed(AI_INTENSITY_PERCENT, EE_UNSIGNED_INT_1, &me->intensity_percent, sizeof me->intensity_percent);
}


void Sequencer_notifyPattern(Sequencer const *me)
{
PatternDescr const *pd = &me->pattern_descr[me->pattern_index];
Attribute_changed(AI_CURRENT_PATTERN_NAME, EE_UTF8_1LEN, (uint8_t const *)pd->name, strlen(pd->name));
}


void Sequencer_notifyPlayState(Sequencer const *me)
{
Attribute_changed(AI_PLAY_PAUSE_STOP, EE_UNSIGNED_INT_1, &me->play_state, sizeof me->play_state);
}


Expand Down

0 comments on commit dd141e7

Please sign in to comment.