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 nrf51xx battery status #117

Open
wants to merge 16 commits into
base: dev
Choose a base branch
from
Open
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
41 changes: 41 additions & 0 deletions firmware/nrf5x/battery.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#define STATUS_FLAG_BATTERY_MASK 0b11000000
#define STATUS_FLAG_COUNTER_MASK 0b00111111
#define STATUS_FLAG_MEDIUM_BATTERY 0b01000000
#define STATUS_FLAG_LOW_BATTERY 0b10000000
#define STATUS_FLAG_CRITICALLY_LOW_BATTERY 0b11000000
#define STATUS_FLAG_BATTERY_UPDATES_SUPPORT 0b00100000

#ifdef S130
#include "nrf51_battery.h"
#else
uint8_t get_current_level() {return 100;};
#endif

void updateBatteryLevel(uint8_t * data)
{
uint8_t * status_flag_ptr = data + 6;
#ifdef S130 // If the board supports battery updates
*status_flag_ptr |= STATUS_FLAG_BATTERY_UPDATES_SUPPORT;
#endif

/*
static uint16_t battery_counter = BATTERY_COUNTER_THRESHOLD;
if((++battery_counter) < BATTERY_COUNTER_THRESHOLD){
return;
}
battery_counter = 0;
*/

uint8_t battery_level = get_current_level();

*status_flag_ptr &= (~STATUS_FLAG_BATTERY_MASK);
if(battery_level > 80){
// do nothing
}else if(battery_level > 50){
*status_flag_ptr |= STATUS_FLAG_MEDIUM_BATTERY;
}else if(battery_level > 30){
*status_flag_ptr |= STATUS_FLAG_LOW_BATTERY;
}else{
*status_flag_ptr |= STATUS_FLAG_CRITICALLY_LOW_BATTERY;
}
}
125 changes: 0 additions & 125 deletions firmware/nrf5x/main copy

This file was deleted.

44 changes: 38 additions & 6 deletions firmware/nrf5x/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,40 @@
#include "ble_stack.h"
#include "openhaystack.h"
#include "app_timer.h"
#include "battery.h"


#define ADVERTISING_INTERVAL 5000 // advertising interval in milliseconds
#define KEY_CHANGE_INTERVAL_MINUTES 30 // how often to rotate to new key in minutes
#define KEY_CHANGE_INTERVAL_DAYS 14 // how often to update battery status in days
#define MAX_KEYS 20 // maximum number of keys to rotate through

#define KEY_CHANGE_INTERVAL_MS (KEY_CHANGE_INTERVAL_MINUTES * 60 * 1000)
#define BATTERY_STATUS_UPDATES_INTERVAL_MS (KEY_CHANGE_INTERVAL_DAYS * 24 * 60 * 60 * 1000)

#define APP_TIMER_PRESCALER 0
#define APP_TIMER_MAX_TIMERS 1
#define TIMER_TICKS APP_TIMER_TICKS(KEY_CHANGE_INTERVAL_MS, APP_TIMER_PRESCALER)
#define APP_TIMER_MAX_TIMERS 2
#define KEY_CHANGE_TIMER_TICKS APP_TIMER_TICKS(KEY_CHANGE_INTERVAL_MS, APP_TIMER_PRESCALER)
#define BATTERY_STATUS_UPDATE_TIMER_TICKS APP_TIMER_TICKS(BATTERY_STATUS_UPDATES_INTERVAL_MS, APP_TIMER_PRESCALER)
#define APP_TIMER_OP_QUEUE_SIZE 4

int last_filled_index = -1;
int current_index = 0;

APP_TIMER_DEF(m_key_change_timer_id);
APP_TIMER_DEF(m_battery_status_timer_id);

// Create space for MAX_KEYS public keys
static char public_key[MAX_KEYS][28] = {
"OFFLINEFINDINGPUBLICKEYHERE!",
};

uint8_t *raw_data; // Initialized by setAndAdvertiseNextKey() -> setAdvertisementKey()

void setAndAdvertiseNextKey()
{
// Variable to hold the data to advertise
uint8_t *ble_address;
uint8_t *raw_data;
uint8_t data_len;

// Disable advertising
Expand All @@ -48,6 +55,9 @@ void setAndAdvertiseNextKey()
// Set bluetooth address
setMacAddress(ble_address);

// Update battery information
updateBatteryLevel(raw_data);

// Set advertisement data
setAdvertisementData(raw_data, data_len);

Expand All @@ -60,7 +70,13 @@ void key_change_timeout_handler(void *p_context)
setAndAdvertiseNextKey();
}

static void timer_config(void)
void battery_status_update_timeout_handler(void *p_context)
{
updateBatteryLevel(raw_data);
}


static void key_change_timer_config(void)
{
uint32_t err_code;

Expand All @@ -71,7 +87,22 @@ static void timer_config(void)
APP_ERROR_CHECK(err_code);

// Set timer interval
err_code = app_timer_start(m_key_change_timer_id, TIMER_TICKS, NULL);
err_code = app_timer_start(m_key_change_timer_id, KEY_CHANGE_TIMER_TICKS, NULL);
APP_ERROR_CHECK(err_code);
}

static void battery_status_update_timer_config(void)
{
uint32_t err_code;

APP_TIMER_INIT(APP_TIMER_PRESCALER, APP_TIMER_OP_QUEUE_SIZE, NULL);

// Create timer
err_code = app_timer_create(&m_battery_status_timer_id, APP_TIMER_MODE_REPEATED, battery_status_update_timeout_handler);
APP_ERROR_CHECK(err_code);

// Set timer interval
err_code = app_timer_start(m_battery_status_timer_id, BATTERY_STATUS_UPDATE_TIMER_TICKS, NULL);
APP_ERROR_CHECK(err_code);
}

Expand All @@ -95,11 +126,12 @@ int main(void) {

// Only use the app_timer to rotate keys if we need to
if (last_filled_index > 0){
timer_config();
key_change_timer_config();
}

if (last_filled_index >= 0) {
setAndAdvertiseNextKey();
battery_status_update_timer_config();
}

while (1){
Expand Down
Loading