Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add power usage and presence classes and example apps #19

Merged
merged 3 commits into from
Aug 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}