From 71a794cf39480185b9c46495d6a2df612898d132 Mon Sep 17 00:00:00 2001 From: Martijn van der Marel Date: Fri, 26 Aug 2022 14:02:16 +0200 Subject: [PATCH 1/3] add presence and power usage classes and examples --- examples/power.ino | 15 +++++++++++++++ examples/presence.ino | 21 +++++++++++++++++++++ include/PowerUsage.h | 16 ++++++++++++++++ include/Presence.h | 30 ++++++++++++++++++++++++++++++ src/PowerUsage.cpp | 14 ++++++++++++++ src/Presence.cpp | 23 +++++++++++++++++++++++ 6 files changed, 119 insertions(+) create mode 100644 examples/power.ino create mode 100644 examples/presence.ino create mode 100644 include/PowerUsage.h create mode 100644 include/Presence.h create mode 100644 src/PowerUsage.cpp create mode 100644 src/Presence.cpp diff --git a/examples/power.ino b/examples/power.ino new file mode 100644 index 0000000..20865e8 --- /dev/null +++ b/examples/power.ino @@ -0,0 +1,15 @@ +#include +#include + +PowerUsage powerUsage; + +void setup() { + Serial.begin(); +} + +void loop() { + int powerUsageNow = powerUsage.getPowerUsageMilliWatts(); + Serial.print("Power usage is "); + Serial.print(powerUsageNow); + Serial.println(" mW"); +} \ No newline at end of file diff --git a/examples/presence.ino b/examples/presence.ino new file mode 100644 index 0000000..c4f0f12 --- /dev/null +++ b/examples/presence.ino @@ -0,0 +1,21 @@ +#include +#include + +const uint8_t profileId = 0; // a profile is a group of users. 0 indicates all users +const uint8_t roomId = 0; // 0 is a special case indicating the entire sphere + +Presence presence; + +void setup() { + Serial.begin(); +} + +void loop() { + bool isInRoom = presence.isPresent(profileId, roomId); + if (isInRoom) { + Serial.println("Profile is in sphere"); + } + else { + Serial.println("Profile is not in sphere"); + } +} \ No newline at end of file diff --git a/include/PowerUsage.h b/include/PowerUsage.h new file mode 100644 index 0000000..c3d277c --- /dev/null +++ b/include/PowerUsage.h @@ -0,0 +1,16 @@ +#pragma once + +#include + +class PowerUsage{ +public: + // Empty constructor + PowerUsage(){}; + + /** + * Request the filtered power usage in milliwatts from bluenet + * + * @return uint32_t Filtered power usage in milliwatts + */ + int32_t getPowerUsageMilliWatts(); +}; diff --git a/include/Presence.h b/include/Presence.h new file mode 100644 index 0000000..7712f31 --- /dev/null +++ b/include/Presence.h @@ -0,0 +1,30 @@ +#pragma once + +#include + +const uint8_t MAX_ROOMS = 64; + +class Presence { +private: + /** + * Request the presence bitmask of a profile from bluenet + * + * @param profileId The id of the profile for which to request presence + * @return bitmask A 64-bit bitmask for a single profile. If the Nth bit is set, + * the profile is present in the Nth room. Bit 0 represents the sphere + */ + uint64_t getPresence(uint8_t profileId); + +public: + // Empty constructor + Presence(){}; + + /** + * Check if a profile is present in a room or in the sphere + * + * @param profileId Id of the profile. Use 0 for all users + * @param roomId Id of the room. When 0, check for the whole sphere + * @return True if profile present in the room, false if not + */ + bool isPresent(uint8_t profileId, uint8_t roomId); +}; diff --git a/src/PowerUsage.cpp b/src/PowerUsage.cpp new file mode 100644 index 0000000..633a5f2 --- /dev/null +++ b/src/PowerUsage.cpp @@ -0,0 +1,14 @@ +#include + +int32_t PowerUsage::getPowerUsageMilliWatts() { + uint8_t* out = getOutgoingMessagePayload(); + microapp_sdk_power_usage_t* powerRequest = reinterpret_cast(out); + powerRequest->header.messageType = CS_MICROAPP_SDK_TYPE_POWER_USAGE; + powerRequest->header.ack = CS_MICROAPP_SDK_ACK_REQUEST; + powerRequest->type = CS_MICROAPP_SDK_POWER_USAGE_POWER; + sendMessage(); + if (powerRequest->header.ack != CS_MICROAPP_SDK_ACK_SUCCESS) { + return -1; + } + return powerRequest->powerUsage; +} \ No newline at end of file diff --git a/src/Presence.cpp b/src/Presence.cpp new file mode 100644 index 0000000..cff0602 --- /dev/null +++ b/src/Presence.cpp @@ -0,0 +1,23 @@ +#include + +uint64_t Presence::getPresence(uint8_t profileId) { + uint8_t* out = getOutgoingMessagePayload(); + microapp_sdk_presence_t* presenceRequest = reinterpret_cast(out); + presenceRequest->header.messageType = CS_MICROAPP_SDK_TYPE_PRESENCE; + presenceRequest->header.ack = CS_MICROAPP_SDK_ACK_REQUEST; + presenceRequest->profileId = profileId; + presenceRequest->presenceBitmask = 0; + sendMessage(); + if (presenceRequest->header.ack != CS_MICROAPP_SDK_ACK_SUCCESS) { + return 0; + } + return presenceRequest->presenceBitmask; +} + +bool Presence::isPresent(uint8_t profileId, uint8_t roomId) { + if (roomId >= MAX_ROOMS) { + return false; + } + uint64_t presenceBitmask = getPresence(profileId); + return presenceBitmask & (1 << roomId); +} \ No newline at end of file From 9018305e55ad606d841dbf60d012cbaef4b86266 Mon Sep 17 00:00:00 2001 From: Martijn van der Marel Date: Fri, 26 Aug 2022 14:02:35 +0200 Subject: [PATCH 2/3] removed unused include --- src/CrownstoneRelay.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/CrownstoneRelay.cpp b/src/CrownstoneRelay.cpp index 2351ea6..fc91fd6 100644 --- a/src/CrownstoneRelay.cpp +++ b/src/CrownstoneRelay.cpp @@ -1,5 +1,4 @@ #include -#include void CrownstoneRelay::switchOff() { setSwitch(CS_MICROAPP_SDK_SWITCH_OFF); From 166cb2b17fdaad23e7876a15db07a9855e59b6d4 Mon Sep 17 00:00:00 2001 From: Martijn van der Marel Date: Fri, 26 Aug 2022 14:10:29 +0200 Subject: [PATCH 3/3] formatting --- include/PowerUsage.h | 2 +- src/PowerUsage.cpp | 8 ++++---- src/Presence.cpp | 10 +++++----- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/include/PowerUsage.h b/include/PowerUsage.h index c3d277c..a8e21c3 100644 --- a/include/PowerUsage.h +++ b/include/PowerUsage.h @@ -2,7 +2,7 @@ #include -class PowerUsage{ +class PowerUsage { public: // Empty constructor PowerUsage(){}; diff --git a/src/PowerUsage.cpp b/src/PowerUsage.cpp index 633a5f2..372b8d9 100644 --- a/src/PowerUsage.cpp +++ b/src/PowerUsage.cpp @@ -1,11 +1,11 @@ #include int32_t PowerUsage::getPowerUsageMilliWatts() { - uint8_t* out = getOutgoingMessagePayload(); + uint8_t* out = getOutgoingMessagePayload(); microapp_sdk_power_usage_t* powerRequest = reinterpret_cast(out); - powerRequest->header.messageType = CS_MICROAPP_SDK_TYPE_POWER_USAGE; - powerRequest->header.ack = CS_MICROAPP_SDK_ACK_REQUEST; - powerRequest->type = CS_MICROAPP_SDK_POWER_USAGE_POWER; + powerRequest->header.messageType = CS_MICROAPP_SDK_TYPE_POWER_USAGE; + powerRequest->header.ack = CS_MICROAPP_SDK_ACK_REQUEST; + powerRequest->type = CS_MICROAPP_SDK_POWER_USAGE_POWER; sendMessage(); if (powerRequest->header.ack != CS_MICROAPP_SDK_ACK_SUCCESS) { return -1; diff --git a/src/Presence.cpp b/src/Presence.cpp index cff0602..866cb4a 100644 --- a/src/Presence.cpp +++ b/src/Presence.cpp @@ -1,12 +1,12 @@ #include uint64_t Presence::getPresence(uint8_t profileId) { - uint8_t* out = getOutgoingMessagePayload(); + uint8_t* out = getOutgoingMessagePayload(); microapp_sdk_presence_t* presenceRequest = reinterpret_cast(out); - presenceRequest->header.messageType = CS_MICROAPP_SDK_TYPE_PRESENCE; - presenceRequest->header.ack = CS_MICROAPP_SDK_ACK_REQUEST; - presenceRequest->profileId = profileId; - presenceRequest->presenceBitmask = 0; + presenceRequest->header.messageType = CS_MICROAPP_SDK_TYPE_PRESENCE; + presenceRequest->header.ack = CS_MICROAPP_SDK_ACK_REQUEST; + presenceRequest->profileId = profileId; + presenceRequest->presenceBitmask = 0; sendMessage(); if (presenceRequest->header.ack != CS_MICROAPP_SDK_ACK_SUCCESS) { return 0;