Skip to content

Commit

Permalink
Merge branch 'p8-acc-hfble' into p8b
Browse files Browse the repository at this point in the history
  • Loading branch information
StarGate01 committed Nov 30, 2022
2 parents a650728 + 7a67517 commit c122753
Show file tree
Hide file tree
Showing 16 changed files with 689 additions and 108 deletions.
12 changes: 12 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -443,9 +443,11 @@ list(APPEND SOURCE_FILES
drivers/DebugPins.cpp
drivers/InternalFlash.cpp
drivers/Hrs3300.cpp
drivers/AccelerationSensor.cpp
drivers/Bma421.cpp
drivers/Bma421_C/bma4.c
drivers/Bma421_C/bma423.c
drivers/SC7A20.cpp
components/battery/BatteryController.cpp
components/ble/BleController.cpp
components/ble/NotificationManager.cpp
Expand Down Expand Up @@ -510,9 +512,11 @@ list(APPEND RECOVERY_SOURCE_FILES
drivers/DebugPins.cpp
drivers/InternalFlash.cpp
drivers/Hrs3300.cpp
drivers/AccelerationSensor.cpp
drivers/Bma421.cpp
drivers/Bma421_C/bma4.c
drivers/Bma421_C/bma423.c
drivers/SC7A20.cpp
components/battery/BatteryController.cpp
components/ble/BleController.cpp
components/ble/NotificationManager.cpp
Expand Down Expand Up @@ -626,9 +630,12 @@ set(INCLUDE_FILES
drivers/InternalFlash.h
drivers/Hrs3300.h
drivers/PinMap.h
drivers/AccelerationSensor.h
drivers/Bma421.h
drivers/Bma421_C/bma4.c
drivers/Bma421_C/bma423.c
drivers/SC7A20.h
drivers/SC7A20_registers.h
components/battery/BatteryController.h
components/ble/BleController.h
components/ble/NotificationManager.h
Expand Down Expand Up @@ -799,27 +806,32 @@ add_definitions(-DTARGET_DEVICE_NAME="${TARGET_DEVICE}")
if(TARGET_DEVICE STREQUAL "PINETIME")
add_definitions(-DDRIVER_PINMAP_PINETIME)
add_definitions(-DCLOCK_CONFIG_LF_SRC=1) # XTAL
add_definitions(-DDRIVER_ACC_BMA421)
add_definitions(-DDRIVER_TOUCH_DYNAMIC)
elseif(TARGET_DEVICE STREQUAL "MOY-TFK5") # P8a
add_definitions(-DDRIVER_PINMAP_P8)
add_definitions(-DCLOCK_CONFIG_LF_SRC=1) # XTAL
add_definitions(-DDRIVER_ACC_BMA421)
add_definitions(-DDRIVER_TOUCH_GESTURE)
elseif(TARGET_DEVICE STREQUAL "MOY-TIN5") # P8a variant 2
add_definitions(-DDRIVER_PINMAP_P8)
add_definitions(-DCLOCK_CONFIG_LF_SRC=1) # XTAL
add_definitions(-DDRIVER_ACC_SC7A20)
add_definitions(-DDRIVER_TOUCH_GESTURE)
elseif(TARGET_DEVICE STREQUAL "MOY-TON5") # P8b
add_definitions(-DDRIVER_PINMAP_P8)
add_definitions(-DCLOCK_CONFIG_LF_SRC=0) # RC
add_definitions(-DMYNEWT_VAL_BLE_LL_SCA=500)
add_definitions(-DCLOCK_CONFIG_LF_CAL_ENABLED=1)
add_definitions(-DDRIVER_ACC_SC7A20)
add_definitions(-DDRIVER_TOUCH_REPORT)
elseif(TARGET_DEVICE STREQUAL "MOY-UNK") # P8b mirrored
add_definitions(-DDRIVER_PINMAP_P8)
add_definitions(-DCLOCK_CONFIG_LF_SRC=0) # RC
add_definitions(-DMYNEWT_VAL_BLE_LL_SCA=500)
add_definitions(-DCLOCK_CONFIG_LF_CAL_ENABLED=1)
add_definitions(-DDRIVER_DISPLAY_MIRROR)
add_definitions(-DDRIVER_ACC_SC7A20)
add_definitions(-DDRIVER_TOUCH_REPORT)
else()
message(FATAL_ERROR "Invalid TARGET_DEVICE")
Expand Down
18 changes: 10 additions & 8 deletions src/components/ble/MotionService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,20 +90,22 @@ void MotionService::OnNewStepCountValue(uint32_t stepCount) {

ble_gattc_notify_custom(connectionHandle, stepCountHandle, om);
}
void MotionService::OnNewMotionValues(int16_t x, int16_t y, int16_t z) {

void MotionService::OnNewMotionValues(int16_t* samples, uint16_t samples_length) {
if (!motionValuesNoficationEnabled)
return;

int16_t buffer[3] = {motionController.X(), motionController.Y(), motionController.Z()};
auto* om = ble_hs_mbuf_from_flat(buffer, 3 * sizeof(int16_t));
if (samples_length > 0 && samples != nullptr) {
auto* om = ble_hs_mbuf_from_flat(samples, samples_length * 3 * sizeof(int16_t));

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

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

ble_gattc_notify_custom(connectionHandle, motionValuesHandle, om);
ble_gattc_notify_custom(connectionHandle, motionValuesHandle, om);
}
}

void MotionService::SubscribeNotification(uint16_t connectionHandle, uint16_t attributeHandle) {
Expand Down
2 changes: 1 addition & 1 deletion src/components/ble/MotionService.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace Pinetime {
void Init();
int OnStepCountRequested(uint16_t connectionHandle, uint16_t attributeHandle, ble_gatt_access_ctxt* context);
void OnNewStepCountValue(uint32_t stepCount);
void OnNewMotionValues(int16_t x, int16_t y, int16_t z);
void OnNewMotionValues(int16_t* samples, uint16_t samples_length);

void SubscribeNotification(uint16_t connectionHandle, uint16_t attributeHandle);
void UnsubscribeNotification(uint16_t connectionHandle, uint16_t attributeHandle);
Expand Down
21 changes: 7 additions & 14 deletions src/components/motion/MotionController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
#include "os/os_cputime.h"
using namespace Pinetime::Controllers;

void MotionController::Update(int16_t x, int16_t y, int16_t z, uint32_t nbSteps) {
void MotionController::Update(uint32_t nbSteps, int16_t x, int16_t y, int16_t z, int16_t* samples, uint16_t samples_length) {
if (this->nbSteps != nbSteps && service != nullptr) {
service->OnNewStepCountValue(nbSteps);
}

if (service != nullptr && (this->x != x || this->y != y || this->z != z)) {
service->OnNewMotionValues(x, y, z);
service->OnNewMotionValues(samples, samples_length);
}

this->x = x;
Expand Down Expand Up @@ -62,26 +62,19 @@ bool MotionController::Should_ShakeWake(uint16_t thresh) {
lastZForShake = z;
return wake;
}

int32_t MotionController::currentShakeSpeed() {
return accumulatedspeed;
}

void MotionController::IsSensorOk(bool isOk) {
isSensorOk = isOk;
}
void MotionController::Init(Pinetime::Drivers::Bma421::DeviceTypes types) {
switch (types) {
case Drivers::Bma421::DeviceTypes::BMA421:
this->deviceType = DeviceTypes::BMA421;
break;
case Drivers::Bma421::DeviceTypes::BMA425:
this->deviceType = DeviceTypes::BMA425;
break;
default:
this->deviceType = DeviceTypes::Unknown;
break;
}

void MotionController::Init(Pinetime::Drivers::AccelerationDeviceTypes types) {
this->deviceType = types;
}

void MotionController::SetService(Pinetime::Controllers::MotionService* service) {
this->service = service;
}
14 changes: 4 additions & 10 deletions src/components/motion/MotionController.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,7 @@ namespace Pinetime {
namespace Controllers {
class MotionController {
public:
enum class DeviceTypes {
Unknown,
BMA421,
BMA425,
};

void Update(int16_t x, int16_t y, int16_t z, uint32_t nbSteps);
void Update(uint32_t nbSteps, int16_t x, int16_t y, int16_t z, int16_t* samples, uint16_t samples_length);

int16_t X() const {
return x;
Expand Down Expand Up @@ -44,11 +38,11 @@ namespace Pinetime {
return isSensorOk;
}

DeviceTypes DeviceType() const {
Pinetime::Drivers::AccelerationDeviceTypes DeviceType() const {
return deviceType;
}

void Init(Pinetime::Drivers::Bma421::DeviceTypes types);
void Init(Pinetime::Drivers::AccelerationDeviceTypes types);
void SetService(Pinetime::Controllers::MotionService* service);

private:
Expand All @@ -59,7 +53,7 @@ namespace Pinetime {
int16_t z;
int16_t lastYForWakeUp = 0;
bool isSensorOk = false;
DeviceTypes deviceType = DeviceTypes::Unknown;
Pinetime::Drivers::AccelerationDeviceTypes deviceType = Pinetime::Drivers::AccelerationDeviceTypes::Unknown;
Pinetime::Controllers::MotionService* service = nullptr;

int16_t lastXForShake = 0;
Expand Down
10 changes: 6 additions & 4 deletions src/displayapp/screens/SystemInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@
using namespace Pinetime::Applications::Screens;

namespace {
const char* ToString(const Pinetime::Controllers::MotionController::DeviceTypes deviceType) {
const char* ToString(const Pinetime::Drivers::AccelerationDeviceTypes deviceType) {
switch (deviceType) {
case Pinetime::Controllers::MotionController::DeviceTypes::BMA421:
case Pinetime::Drivers::AccelerationDeviceTypes::BMA421:
return "BMA421";
case Pinetime::Controllers::MotionController::DeviceTypes::BMA425:
case Pinetime::Drivers::AccelerationDeviceTypes::BMA425:
return "BMA425";
case Pinetime::Controllers::MotionController::DeviceTypes::Unknown:
case Pinetime::Drivers::AccelerationDeviceTypes::SC7A20:
return "SC7A20";
case Pinetime::Drivers::AccelerationDeviceTypes::Unknown:
return "???";
}
return "???";
Expand Down
39 changes: 39 additions & 0 deletions src/drivers/AccelerationSensor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include "AccelerationSensor.h"

namespace Pinetime {
namespace Drivers {

AccelerationSensor::AccelerationSensor(TwiMaster& twiMaster, uint8_t twiAddress) : twiMaster(twiMaster), deviceAddress(twiAddress) {
}

void AccelerationSensor::SoftReset() {
}

void AccelerationSensor::Init() {
}

AccelerationValues AccelerationSensor::Process() {
return {0};
}

void AccelerationSensor::ResetStepCounter() {
}

void AccelerationSensor::Read(uint8_t registerAddress, uint8_t* buffer, size_t size) {
twiMaster.Read(deviceAddress, registerAddress, buffer, size);
}

void AccelerationSensor::Write(uint8_t registerAddress, const uint8_t* data, size_t size) {
twiMaster.Write(deviceAddress, registerAddress, data, size);
}

AccelerationDeviceTypes AccelerationSensor::DeviceType() const {
return deviceType;
}

bool AccelerationSensor::IsInitialized() const {
return isInitialized;
}

}
}
47 changes: 47 additions & 0 deletions src/drivers/AccelerationSensor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#pragma once

#include "drivers/TwiMaster.h"

namespace Pinetime {
namespace Drivers {

enum class AccelerationDeviceTypes : uint8_t { Unknown, BMA421, BMA425, SC7A20 };

struct AccelerationValues {
uint32_t steps;
int16_t x;
int16_t y;
int16_t z;
int16_t* samples = nullptr;
uint16_t samples_length = 0;
};

class AccelerationSensor {
public:
AccelerationSensor(TwiMaster& twiMaster, uint8_t twiAddress);
AccelerationSensor(const AccelerationSensor&) = delete;
AccelerationSensor& operator=(const AccelerationSensor&) = delete;
AccelerationSensor(AccelerationSensor&&) = delete;
AccelerationSensor& operator=(AccelerationSensor&&) = delete;

virtual void SoftReset();
virtual void Init();
virtual AccelerationValues Process();
virtual void ResetStepCounter();

void Read(uint8_t registerAddress, uint8_t* buffer, size_t size);
void Write(uint8_t registerAddress, const uint8_t* data, size_t size);

bool IsInitialized() const;
AccelerationDeviceTypes DeviceType() const;

protected:
TwiMaster& twiMaster;
uint8_t deviceAddress;
bool isInitialized = false;
AccelerationDeviceTypes deviceType = AccelerationDeviceTypes::Unknown;
int16_t fifo[32][3] = {0};
};

}
}
Loading

0 comments on commit c122753

Please sign in to comment.