-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from martijnvandermarel/feature/power-presence
Add power usage and presence classes and example apps
- Loading branch information
Showing
7 changed files
with
119 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#include <Arduino.h> | ||
#include <PowerUsage.h> | ||
|
||
PowerUsage powerUsage; | ||
|
||
void setup() { | ||
Serial.begin(); | ||
} | ||
|
||
void loop() { | ||
int powerUsageNow = powerUsage.getPowerUsageMilliWatts(); | ||
Serial.print("Power usage is "); | ||
Serial.print(powerUsageNow); | ||
Serial.println(" mW"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#include <Arduino.h> | ||
#include <Presence.h> | ||
|
||
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"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#pragma once | ||
|
||
#include <microapp.h> | ||
|
||
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(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#pragma once | ||
|
||
#include <microapp.h> | ||
|
||
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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#include <PowerUsage.h> | ||
|
||
int32_t PowerUsage::getPowerUsageMilliWatts() { | ||
uint8_t* out = getOutgoingMessagePayload(); | ||
microapp_sdk_power_usage_t* powerRequest = reinterpret_cast<microapp_sdk_power_usage_t*>(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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#include <Presence.h> | ||
|
||
uint64_t Presence::getPresence(uint8_t profileId) { | ||
uint8_t* out = getOutgoingMessagePayload(); | ||
microapp_sdk_presence_t* presenceRequest = reinterpret_cast<microapp_sdk_presence_t*>(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); | ||
} |