Skip to content

Commit

Permalink
Merge branch 'develop' of JF/PineTime into master
Browse files Browse the repository at this point in the history
  • Loading branch information
JF002 authored and Gitea committed Feb 14, 2021
2 parents f534fb0 + 324c7da commit 9d7955b
Show file tree
Hide file tree
Showing 33 changed files with 959 additions and 506 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.10)
project(pinetime VERSION 0.12.1 LANGUAGES C CXX ASM)
project(pinetime VERSION 0.13.0 LANGUAGES C CXX ASM)

set(NRF_TARGET "nrf52")

Expand Down
1 change: 1 addition & 0 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ RUN apt-get update -qq \
&& rm -rf /var/cache/apt/* /var/lib/apt/lists/*;

RUN pip3 install adafruit-nrfutil
RUN pip3 install -Iv cryptography==3.3

# build.sh knows how to compile
COPY build.sh /opt/
Expand Down
6 changes: 4 additions & 2 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@ set(SDK_SOURCE_FILES
# Base SDK
"${NRF5_SDK_PATH}/components/boards/boards.c"
"${NRF5_SDK_PATH}/integration/nrfx/legacy/nrf_drv_clock.c"
"${NRF5_SDK_PATH}/integration/nrfx/legacy/nrf_drv_clock.h"
"${NRF5_SDK_PATH}/modules/nrfx/drivers/src/nrfx_clock.c"
"${NRF5_SDK_PATH}/modules/nrfx/drivers/src/nrfx_gpiote.c"
"${NRF5_SDK_PATH}/modules/nrfx/soc/nrfx_atomic.c"
"${NRF5_SDK_PATH}/modules/nrfx/drivers/src/nrfx_saadc.c"
"${NRF5_SDK_PATH}/components/libraries/timer/app_timer.h"

# FreeRTOS
${NRF5_SDK_PATH}/external/freertos/source/croutine.c
Expand Down Expand Up @@ -426,7 +428,6 @@ list(APPEND SOURCE_FILES
displayapp/screens/InfiniPaint.cpp
displayapp/screens/Paddle.cpp
displayapp/screens/DropDownDemo.cpp
displayapp/screens/Modal.cpp
displayapp/screens/BatteryIcon.cpp
displayapp/screens/BleIcon.cpp
displayapp/screens/NotificationIcon.cpp
Expand Down Expand Up @@ -469,6 +470,7 @@ list(APPEND SOURCE_FILES
components/ble/ServiceDiscovery.cpp
components/ble/HeartRateService.cpp
components/firmwarevalidator/FirmwareValidator.cpp
components/motor/MotorController.cpp
drivers/Cst816s.cpp
FreeRTOS/port.c
FreeRTOS/port_cmsis_systick.c
Expand Down Expand Up @@ -520,7 +522,6 @@ set(INCLUDE_FILES
displayapp/screens/InfiniPaint.h
displayapp/screens/Paddle.h
displayapp/screens/DropDownDemo.h
displayapp/screens/Modal.h
displayapp/screens/BatteryIcon.h
displayapp/screens/BleIcon.h
displayapp/screens/NotificationIcon.h
Expand Down Expand Up @@ -579,6 +580,7 @@ set(INCLUDE_FILES
components/heartrate/Biquad.h
components/heartrate/Ptagc.h
components/heartrate/HeartRateController.h
components/motor/MotorController.h
)

include_directories(
Expand Down
20 changes: 20 additions & 0 deletions src/FreeRTOS/port_cmsis.c
Original file line number Diff line number Diff line change
Expand Up @@ -354,4 +354,24 @@ static void vPortEnableVFP( void )
configASSERT( NVIC_GetPriorityGrouping() <= ulMaxPRIGROUPValue );
}

uint32_t ulSetInterruptMaskFromISR( void )
{
__asm volatile (
" mrs r0, PRIMASK \n"
" cpsid i \n"
" bx lr "
::: "memory"
);
}
/*-----------------------------------------------------------*/

void vClearInterruptMaskFromISR( __attribute__( ( unused ) ) uint32_t ulMask )
{
__asm volatile (
" msr PRIMASK, r0 \n"
" bx lr "
::: "memory"
);
}

#endif /* configASSERT_DEFINED */
6 changes: 4 additions & 2 deletions src/FreeRTOS/portmacro_cmsis.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,10 @@ typedef unsigned long UBaseType_t;
/* Critical section management. */
extern void vPortEnterCritical( void );
extern void vPortExitCritical( void );
#define portSET_INTERRUPT_MASK_FROM_ISR() ulPortRaiseBASEPRI()
#define portCLEAR_INTERRUPT_MASK_FROM_ISR(x) vPortSetBASEPRI(x)
extern uint32_t ulSetInterruptMaskFromISR( void ) __attribute__( ( naked ) );
extern void vClearInterruptMaskFromISR( uint32_t ulMask ) __attribute__( ( naked ) );
#define portSET_INTERRUPT_MASK_FROM_ISR() ulSetInterruptMaskFromISR()
#define portCLEAR_INTERRUPT_MASK_FROM_ISR(x) vClearInterruptMaskFromISR( x )
#define portDISABLE_INTERRUPTS() __asm volatile ( " cpsid i " ::: "memory" )
#define portENABLE_INTERRUPTS() __asm volatile ( " cpsie i " ::: "memory" )
#define portENTER_CRITICAL() vPortEnterCritical()
Expand Down
2 changes: 1 addition & 1 deletion src/components/ble/AlertNotificationClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,4 @@ namespace Pinetime {
bool isDescriptorFound = false;
};
}
}
}
65 changes: 62 additions & 3 deletions src/components/ble/AlertNotificationService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ using namespace Pinetime::Controllers;

constexpr ble_uuid16_t AlertNotificationService::ansUuid;
constexpr ble_uuid16_t AlertNotificationService::ansCharUuid;
constexpr ble_uuid128_t AlertNotificationService::notificationEventUuid;


int AlertNotificationCallback(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg) {
Expand All @@ -33,6 +34,13 @@ AlertNotificationService::AlertNotificationService ( System::SystemTask& systemT
.arg = this,
.flags = BLE_GATT_CHR_F_WRITE
},
{
.uuid = (ble_uuid_t *) &notificationEventUuid,
.access_cb = AlertNotificationCallback,
.arg = this,
.flags = BLE_GATT_CHR_F_NOTIFY,
.val_handle = &eventHandle
},
{
0
}
Expand Down Expand Up @@ -61,14 +69,65 @@ int AlertNotificationService::OnAlert(uint16_t conn_handle, uint16_t attr_handle
const auto dbgPacketLen = OS_MBUF_PKTLEN(ctxt->om);
size_t bufferSize = std::min(dbgPacketLen + stringTerminatorSize, maxBufferSize);
auto messageSize = std::min(maxMessageSize, (bufferSize-headerSize));
Categories category;

NotificationManager::Notification notif;
os_mbuf_copydata(ctxt->om, headerSize, messageSize-1, notif.message.data());
os_mbuf_copydata(ctxt->om, 0, 1, &category);
notif.message[messageSize-1] = '\0';
notif.category = Pinetime::Controllers::NotificationManager::Categories::SimpleAlert;
notificationManager.Push(std::move(notif));

systemTask.PushMessage(Pinetime::System::SystemTask::Messages::OnNewNotification);
// TODO convert all ANS categories to NotificationController categories
switch(category) {
case Categories::Call:
notif.category = Pinetime::Controllers::NotificationManager::Categories::IncomingCall;
break;
default:
notif.category = Pinetime::Controllers::NotificationManager::Categories::SimpleAlert;
break;
}

auto event = Pinetime::System::SystemTask::Messages::OnNewNotification;
notificationManager.Push(std::move(notif));
systemTask.PushMessage(event);
}
return 0;
}

void AlertNotificationService::AcceptIncomingCall() {
auto response = IncomingCallResponses::Answer;
auto *om = ble_hs_mbuf_from_flat(&response, 1);

uint16_t connectionHandle = systemTask.nimble().connHandle();

if (connectionHandle == 0 || connectionHandle == BLE_HS_CONN_HANDLE_NONE) {
return;
}

ble_gattc_notify_custom(connectionHandle, eventHandle, om);
}

void AlertNotificationService::RejectIncomingCall() {
auto response = IncomingCallResponses::Reject;
auto *om = ble_hs_mbuf_from_flat(&response, 1);

uint16_t connectionHandle = systemTask.nimble().connHandle();

if (connectionHandle == 0 || connectionHandle == BLE_HS_CONN_HANDLE_NONE) {
return;
}

ble_gattc_notify_custom(connectionHandle, eventHandle, om);
}

void AlertNotificationService::MuteIncomingCall() {
auto response = IncomingCallResponses::Mute;
auto *om = ble_hs_mbuf_from_flat(&response, 1);

uint16_t connectionHandle = systemTask.nimble().connHandle();

if (connectionHandle == 0 || connectionHandle == BLE_HS_CONN_HANDLE_NONE) {
return;
}

ble_gattc_notify_custom(connectionHandle, eventHandle, om);
}
35 changes: 34 additions & 1 deletion src/components/ble/AlertNotificationService.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
#undef max
#undef min

//00020001-78fc-48fe-8e23-433b3a1942d0
#define NOTIFICATION_EVENT_SERVICE_UUID_BASE {0xd0, 0x42, 0x19, 0x3a, 0x3b, 0x43, 0x23, 0x8e, 0xfe, 0x48, 0xfc, 0x78, 0x01, 0x00, 0x02, 0x00}

namespace Pinetime {

namespace System {
Expand All @@ -24,8 +27,31 @@ namespace Pinetime {
int OnAlert(uint16_t conn_handle, uint16_t attr_handle,
struct ble_gatt_access_ctxt *ctxt);

void AcceptIncomingCall();
void RejectIncomingCall();
void MuteIncomingCall();

enum class IncomingCallResponses : uint8_t {
Reject = 0x00,
Answer = 0x01,
Mute = 0x02
};

private:
enum class Categories : uint8_t {
SimpleAlert = 0x00,
Email = 0x01,
News = 0x02,
Call = 0x03,
MissedCall = 0x04,
MmsSms = 0x05,
VoiceMail = 0x06,
Schedule = 0x07,
HighPrioritizedAlert = 0x08,
InstantMessage = 0x09,
All = 0xff
};

static constexpr uint16_t ansId {0x1811};
static constexpr uint16_t ansCharId {0x2a46};

Expand All @@ -39,11 +65,18 @@ namespace Pinetime {
.value = ansCharId
};

struct ble_gatt_chr_def characteristicDefinition[2];
static constexpr ble_uuid128_t notificationEventUuid {
.u { .type = BLE_UUID_TYPE_128 },
.value = NOTIFICATION_EVENT_SERVICE_UUID_BASE
};

struct ble_gatt_chr_def characteristicDefinition[3];
struct ble_gatt_svc_def serviceDefinition[2];

Pinetime::System::SystemTask &systemTask;
NotificationManager &notificationManager;

uint16_t eventHandle;
};
}
}
4 changes: 1 addition & 3 deletions src/components/ble/HeartRateService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,7 @@ void HeartRateService::Init() {
int HeartRateService::OnHeartRateRequested(uint16_t connectionHandle, uint16_t attributeHandle,
ble_gatt_access_ctxt *context) {
if(attributeHandle == heartRateMeasurementHandle) {
NRF_LOG_INFO("BATTERY : handle = %d", heartRateMeasurementHandle);
static uint8_t batteryValue = heartRateController.HeartRate();

NRF_LOG_INFO("HEARTRATE : handle = %d", heartRateMeasurementHandle);
uint8_t buffer[2] = {0, heartRateController.HeartRate()}; // [0] = flags, [1] = hr value

int res = os_mbuf_append(context->om, buffer, 2);
Expand Down
4 changes: 2 additions & 2 deletions src/components/ble/NimbleController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ NimbleController::NimbleController(Pinetime::System::SystemTask& systemTask,
navService{systemTask},
batteryInformationService{batteryController},
immediateAlertService{systemTask, notificationManager},
serviceDiscovery({&currentTimeClient, &alertNotificationClient}),
heartRateService{systemTask, heartRateController} {
heartRateService{systemTask, heartRateController},
serviceDiscovery({&currentTimeClient, &alertNotificationClient}) {
}

int GAPEventCallback(struct ble_gap_event *event, void *arg) {
Expand Down
1 change: 1 addition & 0 deletions src/components/ble/NimbleController.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ namespace Pinetime {

Pinetime::Controllers::MusicService& music() {return musicService;};
Pinetime::Controllers::NavigationService& navigation() {return navService;};
Pinetime::Controllers::AlertNotificationService& alertService() {return anService;};

uint16_t connHandle();

Expand Down
8 changes: 8 additions & 0 deletions src/components/ble/NotificationManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@ bool NotificationManager::AreNewNotificationsAvailable() {
return newNotification;
}

bool NotificationManager::IsVibrationEnabled() {
return vibrationEnabled;
}

void NotificationManager::ToggleVibrations() {
vibrationEnabled = !vibrationEnabled;
}

bool NotificationManager::ClearNewNotificationFlag() {
return newNotification.exchange(false);
}
Expand Down
3 changes: 3 additions & 0 deletions src/components/ble/NotificationManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ namespace Pinetime {
Notification GetPrevious(Notification::Id id);
bool ClearNewNotificationFlag();
bool AreNewNotificationsAvailable();
bool IsVibrationEnabled();
void ToggleVibrations();

static constexpr size_t MaximumMessageSize() { return MessageSize; };
size_t NbNotifications() const;
Expand All @@ -40,6 +42,7 @@ namespace Pinetime {
uint8_t writeIndex = 0;
bool empty = true;
std::atomic<bool> newNotification{false};
bool vibrationEnabled = true;
};
}
}
2 changes: 1 addition & 1 deletion src/components/heartrate/Ppg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ using namespace Pinetime::Controllers;
namespace {
int Compare(int* d1, int* d2, size_t count) {
int e = 0;
for(int i = 0; i < count; i++) {
for(size_t i = 0; i < count; i++) {
auto d = d1[i] - d2[i];
e += d * d;
}
Expand Down
25 changes: 25 additions & 0 deletions src/components/motor/MotorController.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include "MotorController.h"
#include <hal/nrf_gpio.h>
#include "systemtask/SystemTask.h"
#include "app_timer.h"

APP_TIMER_DEF(vibTimer);

using namespace Pinetime::Controllers;

void MotorController::Init() {
nrf_gpio_cfg_output(pinMotor);
nrf_gpio_pin_set(pinMotor);
app_timer_init();
app_timer_create(&vibTimer, APP_TIMER_MODE_SINGLE_SHOT, vibrate);
}

void MotorController::SetDuration(uint8_t motorDuration) {
nrf_gpio_pin_clear(pinMotor);
/* Start timer for motorDuration miliseconds and timer triggers vibrate() when it finishes*/
app_timer_start(vibTimer, APP_TIMER_TICKS(motorDuration), NULL);
}

void MotorController::vibrate(void * p_context) {
nrf_gpio_pin_set(pinMotor);
}
19 changes: 19 additions & 0 deletions src/components/motor/MotorController.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#pragma once

#include <cstdint>
#include "app_timer.h"

namespace Pinetime {
namespace Controllers {
static constexpr uint8_t pinMotor = 16;

class MotorController {
public:
void Init();
void SetDuration(uint8_t motorDuration);

private:
static void vibrate(void * p_context);
};
}
}
Loading

0 comments on commit 9d7955b

Please sign in to comment.