Skip to content

Commit

Permalink
allow opt-out of ack
Browse files Browse the repository at this point in the history
  • Loading branch information
balvig committed Sep 16, 2020
1 parent 9cf1fe3 commit bb26c8b
Show file tree
Hide file tree
Showing 15 changed files with 30 additions and 28 deletions.
12 changes: 6 additions & 6 deletions examples/README.md
Expand Up @@ -13,12 +13,12 @@

### Inbox

| key | description | values |
|---------|--------------------------------------------------------------------|----------------------------|
| sleep | Start sleep cycles lasting `value` seconds each | 0 (stop sleeping), 1-13612 |
| battery | Ask for battery value | |
| ota | `pio run -t upload --upload-port 192.168.4.1` when connected to AP | |
| skills | Get a list of actions the Brick responds to | |
| key | description | values |
|------------|--------------------------------------------------------------------|----------------------------|
| setSleep | Start sleep cycles lasting `value` seconds each | 0 (stop sleeping), 1-13612 |
| setOta | `pio run -t upload --upload-port 192.168.4.1` when connected to AP | |
| getBattery | Ask for battery value | |
| getSkills | Get a list of actions the Brick responds to | |

### Outbox

Expand Down
4 changes: 2 additions & 2 deletions examples/ble/src/main.cpp
Expand Up @@ -28,13 +28,13 @@ void scan(const uint8_t scanTime, const bool activeScan = false) {
Log.notice("BLES: Scan done" CR);
}

void passiveScan(const uint8_t *macAddr, const Message message) {
void passiveScan(BRICKS_CALLBACK_SIGNATURE) {
const uint8_t scanTime = atoi(message.value);
scan(scanTime, false);
}

// active scan required to get names?
void activeScan(const uint8_t *macAddr, const Message message) {
void activeScan(BRICKS_CALLBACK_SIGNATURE) {
const uint8_t scanTime = atoi(message.value);
scan(scanTime, true);
}
Expand Down
2 changes: 1 addition & 1 deletion examples/buzzer/src/main.cpp
Expand Up @@ -7,7 +7,7 @@ using namespace Bricks;
const int BUZZER = D5;

// Bricks callbacks
void playTone(const uint8_t *macAddr, const Message message) {
void playTone(BRICKS_CALLBACK_SIGNATURE) {
int frequency;
int duration;
sscanf(message.value, "%d,%d", &frequency, &duration);
Expand Down
2 changes: 1 addition & 1 deletion examples/ir/src/main.cpp
Expand Up @@ -24,7 +24,7 @@ decode_results results;
IRCode *code;

// Bricks callbacks
void sendCode(const uint8_t *macAddr, const Message message) {
void sendCode(BRICKS_CALLBACK_SIGNATURE) {
code = codes[atoi(message.value)];
}

Expand Down
2 changes: 1 addition & 1 deletion examples/led-matrix/src/main.cpp
Expand Up @@ -9,7 +9,7 @@ using namespace Bricks;
MLED matrix(7);

// Bricks callbacks
void setValue(const uint8_t *macAddr, const Message message) {
void setValue(BRICKS_CALLBACK_SIGNATURE) {
matrix.clear();
matrix.setCursor(2, 1);
matrix.print(message.value);
Expand Down
6 changes: 3 additions & 3 deletions examples/led-rgb/src/main.cpp
Expand Up @@ -10,15 +10,15 @@ using namespace Bricks;
Lumi::Animator animator;

// Bricks callbacks
void setPattern(const uint8_t *macAddr, const Message message) {
void setPattern(BRICKS_CALLBACK_SIGNATURE) {
animator.currentPattern = atoi(message.value);
}

void setVariation(const uint8_t *macAddr, const Message message) {
void setVariation(BRICKS_CALLBACK_SIGNATURE) {
animator.currentVariation = atoi(message.value);
}

void setDelay(const uint8_t *macAddr, const Message message) {
void setDelay(BRICKS_CALLBACK_SIGNATURE) {
animator.currentDelay = atoi(message.value);
}

Expand Down
2 changes: 1 addition & 1 deletion examples/led/src/main.cpp
Expand Up @@ -5,7 +5,7 @@ using namespace Bricks;

const int ledPin = LED_BUILTIN;

void setPattern(const uint8_t *macAddr, const Message message) {
void setPattern(BRICKS_CALLBACK_SIGNATURE) {
if(atoi(message.value)) {
digitalWrite(ledPin, LOW);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Bricks.BatterySkill.cpp
@@ -1,7 +1,7 @@
#include <Bricks.BatterySkill.h>

namespace Bricks {
BatterySkill::BatterySkill(const uint8_t pin) : Skill("battery") {
BatterySkill::BatterySkill(const uint8_t pin) : Skill("getBattery") {
this->pin = pin;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Bricks.ListSkill.cpp
@@ -1,7 +1,7 @@
#include "Bricks.ListSkill.h"

namespace Bricks {
ListSkill::ListSkill() : Skill("skills") {
ListSkill::ListSkill() : Skill("getSkills") {
}

void ListSkill::callback(BRICKS_CALLBACK_SIGNATURE) {
Expand Down
2 changes: 1 addition & 1 deletion src/Bricks.OtaSkill.cpp
@@ -1,7 +1,7 @@
#include "Bricks.OtaSkill.h"

namespace Bricks {
OtaSkill::OtaSkill() : Skill("ota") {
OtaSkill::OtaSkill() : Skill("setOta") {
}

void OtaSkill::loop() {
Expand Down
2 changes: 1 addition & 1 deletion src/Bricks.PublishSkill.cpp
@@ -1,7 +1,7 @@
#include "Bricks.PublishSkill.h"

namespace Bricks {
PublishSkill::PublishSkill() : Skill("*") {
PublishSkill::PublishSkill() : Skill("*", nullptr, false) {
gEvents.publish(BRICKS_MESSAGES_IN "/gateway/awake");
}

Expand Down
9 changes: 5 additions & 4 deletions src/Bricks.Skill.cpp
@@ -1,9 +1,10 @@
#include <Bricks.Skill.h>

namespace Bricks {
Skill::Skill(const char *key, std::function<void(const uint8_t *macAddr, const Message message)> customCallback) {
Skill::Skill(const char *key, std::function<void(BRICKS_CALLBACK_SIGNATURE)> customCallback, bool sendAck) {
this->key = key;
this->customCallback = customCallback;
this->sendAck = sendAck;
}

bool Skill::respondsTo(const char *compareKey) {
Expand All @@ -12,16 +13,16 @@ namespace Bricks {

void Skill::process(const uint8_t *macAddr, const Message message) {
char response[100];
strcpy(response, message.value);
callback(macAddr, message, response);

// Doesn't work yet. Always true.
if(response) {
if(sendAck) {
ack(response);
}
}

void Skill::callback(BRICKS_CALLBACK_SIGNATURE) {
customCallback(macAddr, message);
customCallback(macAddr, message, response);
}

void Skill::loop() {
Expand Down
5 changes: 3 additions & 2 deletions src/Bricks.Skill.h
Expand Up @@ -14,15 +14,16 @@ namespace Bricks {
const char *ACK_PREFIX = "ack";

public:
Skill(const char *key = "", std::function<void(const uint8_t *macAddr, const Message message)> customCallback = nullptr);
Skill(const char *key = "", std::function<void(BRICKS_CALLBACK_SIGNATURE)> customCallback = nullptr, bool sendAck = true);
bool respondsTo(const char* compareKey);
void process(const uint8_t *macAddr, const Message message);
virtual void callback(BRICKS_CALLBACK_SIGNATURE);
virtual void loop();
const char *key;
private:
void ack(const char *response);
std::function<void(const uint8_t *macAddr, const Message message)> customCallback;
bool sendAck;
std::function<void(BRICKS_CALLBACK_SIGNATURE)> customCallback;
};
}
#endif
2 changes: 1 addition & 1 deletion src/Bricks.SleepSkill.cpp
Expand Up @@ -5,7 +5,7 @@ namespace Bricks {
RTC_DATA_ATTR uint32_t rtcSleepTime = 0;
#endif

SleepSkill::SleepSkill(const char *name) : Skill("sleep") {
SleepSkill::SleepSkill(const char *name) : Skill("setSleep") {
this->name = name;
sendAwakeMessage();

Expand Down
4 changes: 2 additions & 2 deletions test/inbox/test_inbox.cpp
Expand Up @@ -13,7 +13,7 @@ void setUp(void) {
void test_battery() {
const uint8_t macAddr[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
Message message;
strcpy(message.key, "battery");
strcpy(message.key, "getBattery");

gInbox.skills[0] = new BatterySkill();
gInbox.process(macAddr, message);
Expand All @@ -22,7 +22,7 @@ void test_battery() {
void test_list_skills() {
gInbox.skills[0] = new BatterySkill();

TEST_ASSERT_EQUAL_STRING("battery", gInbox.listSkills().c_str());
TEST_ASSERT_EQUAL_STRING("getBattery", gInbox.listSkills().c_str());
}

void setup() {
Expand Down

0 comments on commit bb26c8b

Please sign in to comment.