Skip to content

Commit

Permalink
Merge pull request #19 from martijnvandermarel/feature/power-presence
Browse files Browse the repository at this point in the history
Add power usage and presence classes and example apps
  • Loading branch information
vliedel authored Aug 26, 2022
2 parents 39e1ec7 + 166cb2b commit 1266ee9
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 1 deletion.
15 changes: 15 additions & 0 deletions examples/power.ino
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");
}
21 changes: 21 additions & 0 deletions examples/presence.ino
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");
}
}
16 changes: 16 additions & 0 deletions include/PowerUsage.h
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();
};
30 changes: 30 additions & 0 deletions include/Presence.h
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);
};
1 change: 0 additions & 1 deletion src/CrownstoneRelay.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include <CrownstoneRelay.h>
#include <Serial.h>

void CrownstoneRelay::switchOff() {
setSwitch(CS_MICROAPP_SDK_SWITCH_OFF);
Expand Down
14 changes: 14 additions & 0 deletions src/PowerUsage.cpp
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;
}
23 changes: 23 additions & 0 deletions src/Presence.cpp
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);
}

0 comments on commit 1266ee9

Please sign in to comment.