Skip to content

Commit

Permalink
Allow read of virtual attributes.
Browse files Browse the repository at this point in the history
  • Loading branch information
Onwrikbaar committed Nov 20, 2024
1 parent df3f216 commit 3d015e6
Show file tree
Hide file tree
Showing 9 changed files with 1,551 additions and 1,503 deletions.
2,947 changes: 1,480 additions & 1,467 deletions firmware/build/neodk_g071.hex

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions firmware/inc/attributes.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@


typedef enum {
AI_FIRMWARE_VERSION = 2, AI_BATTERY_LEVEL, AI_CLOCK_MICROS,
AI_ALL_PATTERN_NAMES, AI_CURRENT_PATTERN_NAME, AI_INTENSITY_PERCENT, AI_PLAY_PAUSE_STOP
AI_FIRMWARE_VERSION = 2, AI_VOLTAGES, AI_CLOCK_MICROS,
AI_ALL_PATTERN_NAMES, AI_CURRENT_PATTERN_NAME, AI_INTENSITY_PERCENT, AI_PLAY_PAUSE_STOP,
AI_BOX_NAME
} AttributeId;

typedef void (*AttrNotifier)(void *target, AttributeId, ElementEncoding, uint8_t const *data, uint16_t size);
Expand All @@ -27,6 +28,7 @@ typedef void (*AttrNotifier)(void *target, AttributeId, ElementEncoding, uint8_t
extern "C" {
#endif

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

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

#ifndef INC_CIRCBUFFER_H_
Expand Down
6 changes: 2 additions & 4 deletions firmware/maolib/inc/matter.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ typedef enum {
EE_UNSIGNED_INT_1, EE_UNSIGNED_INT_2, EE_UNSIGNED_INT_4, EE_UNSIGNED_INT_8,
EE_BOOLEAN_FALSE, EE_BOOLEAN_TRUE, EE_FLOAT_4, EE_FLOAT_8,
EE_UTF8_1LEN, EE_UTF8_2LEN, EE_UTF8_4LEN, EE_UTF8_8LEN,
EE_BYTE_1LEN, EE_BYTE_2LEN, EE_BYTE_4LEN, EE_BYTE_8LEN,
EE_BYTES_1LEN, EE_BYTES_2LEN, EE_BYTES_4LEN, EE_BYTES_8LEN,
EE_NULL, EE_STRUCT, EE_ARRAY, EE_LIST, EE_END_OF_CONTAINER
} ElementEncoding;

Expand All @@ -35,14 +35,12 @@ typedef uint16_t SubscriptionId;
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);
uint16_t Matter_encodedDataLength(ElementEncoding enc, uint16_t nr_of_octets);
uint16_t Matter_encodeScalarData(uint8_t dst[], ElementEncoding, uint8_t const *src, uint16_t nr_of_octets);
uint16_t Matter_encode(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.
45 changes: 31 additions & 14 deletions firmware/src/attributes.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,49 +18,66 @@


typedef struct {
uint16_t active;
AttributeId ai; // Key.
ElementEncoding encoding;
AttrNotifier notify;
void *target;
} Subscription;


static Subscription subscriptions[4];
static Subscription subscriptions[10];
static uint8_t nr_of_subs = 0;


static Subscription const *findSubForId(AttributeId ai)
static Subscription *findSubForId(AttributeId ai)
{
for (uint8_t i = 0; i < M_DIM(subscriptions); i++) {
Subscription const *sub = &subscriptions[i];
Subscription *sub = &subscriptions[i];
if (ai == sub->ai) return sub;
}

return NULL;
}


static SubscriptionId setSubForId(AttributeId ai, AttrNotifier notify, void *target, uint16_t count)
{
Subscription *sub = findSubForId(ai);
if (sub == NULL) {
if (nr_of_subs == M_DIM(subscriptions)) return 0;
sub = &subscriptions[nr_of_subs++];
sub->ai = ai;
}
sub->notify = notify;
sub->target = target;
sub->active = count;
return 256 + (sub - subscriptions);
}

/*
* Below are the functions implementing this module's interface.
*/

SubscriptionId Attribute_subscribe(AttributeId ai, AttrNotifier notify, void *target)
SubscriptionId Attribute_awaitRead(AttributeId ai, AttrNotifier notify, void *target)
{
if (nr_of_subs == M_DIM(subscriptions)) return 0;
BSP_logf("%s for id=%hu)\n", __func__, ai);
return setSubForId(ai, notify, target, 1);
}


SubscriptionId Attribute_subscribe(AttributeId ai, AttrNotifier notify, void *target)
{
// BSP_logf("%s for id=%hu\n", __func__, ai);
Subscription *sub = &subscriptions[nr_of_subs];
sub->ai = ai;
sub->notify = notify;
sub->target = target;
return 256 + nr_of_subs++;
return setSubForId(ai, notify, target, ~0);
}


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) {
BSP_logf("%s(%hu)\n", __func__, ai);
Subscription *sub = findSubForId(ai);
if (sub != NULL && sub->active) {
sub->notify(sub->target, ai, enc, data, size);
sub->active -= 1;
}
}
29 changes: 21 additions & 8 deletions firmware/src/controller.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ static void attributeChanged(Controller *me, AttributeId ai, ElementEncoding enc
aa->reserved = 0;
aa->attribute_id = ai;
// TODO Add subscription Id?
nbtw += Matter_encodeScalarData(packet + nbtw, enc, data, data_size);
nbtw += Matter_encode(packet + nbtw, enc, data, data_size);
DataLink_sendDatagram(me->datalink, packet, nbtw);
}

Expand All @@ -111,6 +111,13 @@ static void handleReadRequest(Controller *me, AttributeAction const *aa)
{
switch (aa->attribute_id)
{
case AI_FIRMWARE_VERSION:
// TODO Implement.
break;
case AI_VOLTAGES:
Attribute_awaitRead(aa->attribute_id, (AttrNotifier)&attributeChanged, me);
BSP_triggerADC();
break;
case AI_ALL_PATTERN_NAMES:
readPatternNames(me, aa);
break;
Expand All @@ -123,18 +130,21 @@ static void handleReadRequest(Controller *me, AttributeAction const *aa)
case AI_PLAY_PAUSE_STOP:
Sequencer_notifyPlayState(me->sequencer);
break;
case AI_BOX_NAME:
// TODO Implement.
break;
default:
BSP_logf("%s: unknown attribute id=%hu\n", __func__, aa->attribute_id);
// TODO Respond with NOT_FOUND code.
// TODO Respond with NOT_FOUND code to the UI.
}
}


static EventType eventTypeForCommand(uint8_t const *cs, uint16_t len)
{
if (len == 4 && memcmp(cs, "stop", len) == 0) return ET_STOP;
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;
}

Expand All @@ -143,21 +153,24 @@ static void handleWriteRequest(Controller *me, AttributeAction const *aa)
{
switch (aa->attribute_id)
{
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_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_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;
case AI_BOX_NAME:
// TODO Implement.
break;
default:
BSP_logf("%s: unknown attribute id=%hu\n", __func__, aa->attribute_id);
// TODO Respond with NOT_FOUND code.
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.37-beta\n");
CLI_logf("Firmware v0.38-beta\n");
break;
case 'w': // Allow rediscovery by Dweeb.
DataLink_waitForSync(me->datalink);
Expand Down
17 changes: 11 additions & 6 deletions firmware/src/sequencer.c
Original file line number Diff line number Diff line change
Expand Up @@ -189,12 +189,17 @@ static void setPlayState(Sequencer *me, PlayState play_state)
}


static void printAdcValues(uint16_t const *v)
static void handleAdcValues(uint16_t const *v)
{
uint32_t Iprim_mA = ((uint32_t)v[0] * 2063UL) / 1024;
uint32_t Vcap_mV = ((uint32_t)v[1] * 52813UL) / 16384;
uint32_t Vbat_mV = ((uint32_t)v[2] * 52813UL) / 16384;
CLI_logf("Iprim=%u mA, Vcap=%u mV, Vbat=%u mV\n", Iprim_mA, Vcap_mV, Vbat_mV);
struct {
uint16_t Vbat_mV, Vcap_mV, Iprim_mA;
} vi = {
.Vbat_mV = ((uint32_t)v[2] * 52813UL) / 16384,
.Vcap_mV = ((uint32_t)v[1] * 52813UL) / 16384,
.Iprim_mA = ((uint32_t)v[0] * 2063UL) / 1024,
};
CLI_logf("Vbat=%hu mV, Vcap=%hu mV, Iprim=%hu mA\n", vi.Vbat_mV, vi.Vcap_mV, vi.Iprim_mA);
Attribute_changed(AI_VOLTAGES, EE_BYTES_1LEN, (uint8_t const *)&vi, sizeof vi);
}


Expand All @@ -210,7 +215,7 @@ static void *stateCanopy(Sequencer *me, AOEvent const *evt)
switch (AOEvent_type(evt))
{
case ET_ADC_DATA_AVAILABLE:
printAdcValues((uint16_t const *)AOEvent_data(evt));
handleAdcValues((uint16_t const *)AOEvent_data(evt));
break;
case ET_SELECT_NEXT_PATTERN:
if (++me->pattern_index == me->nr_of_patterns) me->pattern_index = 0;
Expand Down

0 comments on commit 3d015e6

Please sign in to comment.