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..a8e21c3 --- /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/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); diff --git a/src/PowerUsage.cpp b/src/PowerUsage.cpp new file mode 100644 index 0000000..372b8d9 --- /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..866cb4a --- /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