My Project
Loading...
Searching...
No Matches
BLEHub.h
Go to the documentation of this file.
1#pragma once
2
3#include <Arduino.h>
4#include <NimBLEDevice.h>
5
6#include "BLEHubChannel.h"
9#include "MCLocoAction.h"
10
11// The priority at which the task should run.
12// Systems that include MPU support can optionally create tasks in a privileged (system) mode by setting bit portPRIVILEGE_BIT of the priority parameter.
13// For example, to create a privileged task at priority 2 the uxPriority parameter should be set to ( 2 | portPRIVILEGE_BIT ).
14#define BLE_TaskPriority 1
15
16// If the value is tskNO_AFFINITY, the created task is not pinned to any CPU, and the scheduler can run it on any core available.
17// Values 0 or 1 indicate the index number of the CPU which the task should be pinned to.
18// Specifying values larger than (portNUM_PROCESSORS - 1) will cause the function to fail.
19#define BLE_CoreID CONFIG_BT_NIMBLE_PINNED_TO_CORE
20
21// The size of the task stack specified as the number of bytes.
22#define BLE_StackDepth 2048
23
24// The number of seconds to wait for a Hub to connect.
25#define ConnectDelayInSeconds 5
26
53
54// Abstract Bluetooth Low Energy (BLE) hub base class.
55class BLEHub
56{
57 public:
59
60 // Returns a boolean value indicating whether we have discovered the BLE hub.
61 bool IsDiscovered();
62
63 // Returns a boolean value indicating whether we are connected to the BLE hub.
64 bool IsConnected();
65
66 // Sets
67 void SetConnectCallback(std::function<void(bool)> callback);
68
69 // Returns the hub's raw address.
70 std::string GetRawAddress();
71
72 // Returns the hub's address.
73 NimBLEAddress GetAddress();
74
75 // Sets the given target power perc for all motor channels.
76 void Drive(const int16_t minPwrPerc, const int16_t pwrPerc);
77
78 // Gets the current power perc for any motor channel.
79 int16_t GetCurrentDrivePwrPerc();
80
81 // Executes the given action.
82 void Execute(MCLocoAction *action);
83
84 // Makes all channels with lights attached blink for the given duration.
85 void BlinkLights(int durationInMs);
86
87 // Set the initial color of the Hub's onboard LED.
88 void SetHubLedColor(HubLedColor color);
89
90 // If true, immediately sets the current speed for all channels to zero.
91 // If false, releases the manual brake.
92 void SetManualBrake(const bool enabled);
93
94 // If true, immediately sets the current speed for all channels to zero.
95 // If false, releases the emergency brake.
96 void SetEmergencyBrake(const bool enabled);
97
98 // Method used to connect to the BLE hub.
99 bool Connect(const uint8_t watchdogTimeOutInTensOfSeconds);
100
101 // Abstract method used to set the watchdog timeout.
102 virtual bool SetWatchdogTimeout(const uint8_t watchdogTimeOutInTensOfSeconds) = 0;
103
104 // Abstract method used to periodically send drive commands to the BLE hub.
105 virtual void DriveTaskLoop() = 0;
106
107 // Abstract method used to map a speed percentile (-100% - 100%) to a raw speed value.
108 virtual int16_t MapPwrPercToRaw(int pwrPerc) = 0;
109
110 // Abstract callback method used to handle hub notifications.
111 virtual void NotifyCallback(NimBLERemoteCharacteristic *pBLERemoteCharacteristic, uint8_t *pData, size_t length, bool isNotify) = 0;
112
113 protected:
115 void dumpPData(uint8_t *pData, size_t length);
116 void setTargetPwrPercByAttachedDevice(DeviceType device, int16_t minPwrPerc, int16_t pwrPerc);
120
121 bool attachCharacteristic(NimBLEUUID serviceUUID, NimBLEUUID characteristicUUID);
122 bool startDriveTask();
123 static void driveTaskImpl(void *);
124 void connected();
125 void disconnected();
126
127 std::function<void(bool)> _onConnectionChangedCallback;
128
130 std::vector<BLEHubChannelController *> _channelControllers;
131
132 TaskHandle_t _driveTaskHandle;
133 NimBLEAdvertisedDevice *_advertisedDevice;
134 NimBLEAdvertisedDeviceCallbacks *_advertisedDeviceCallback;
135 NimBLEClient *_hub;
136 NimBLEClientCallbacks *_clientCallback;
144 NimBLERemoteService *_remoteControlService;
145 NimBLERemoteCharacteristic *_remoteControlCharacteristic;
146 // NimBLERemoteCharacteristic *_genericAccessCharacteristic;
147 // NimBLERemoteCharacteristic *_deviceInformationCharacteristic;
148
149 // The following classes can access private members of BLEHub.
150 friend class BLEClientCallback;
151 friend class BLEDeviceCallbacks;
152};
BLEHubChannel
Definition BLEHubChannel.h:5
MessageType
Definition BLEHub.h:27
@ PORT_MODE_INFORMATION
@ PORT_INPUT_FORMAT_SINGLE
@ PORT_INPUT_FORMAT_SETUP_COMBINEDMODE
@ PORT_OUTPUT_COMMAND_FEEDBACK
@ PORT_INPUT_FORMAT_COMBINEDMODE
@ PORT_MODE_INFORMATION_REQUEST
@ GENERIC_ERROR_MESSAGES
@ PORT_VALUE_COMBINEDMODE
@ PORT_INPUT_FORMAT_SETUP_SINGLE
@ PORT_INFORMATION_REQUEST
@ FW_UPDATE_GO_INTO_BOOT_MODE
@ FW_UPDATE_LOCK_STATUS_REQUEST
@ FW_UPDATE_LOCK_MEMORY
Definition BLEClientCallback.h:7
Definition BLEDeviceCallbacks.h:7
Definition BLEHubChannelController.h:10
Definition BLEHubConfiguration.h:46
Definition BLEHub.h:56
bool _ebrake
Definition BLEHub.h:138
NimBLEAdvertisedDeviceCallbacks * _advertisedDeviceCallback
Definition BLEHub.h:134
NimBLERemoteService * _remoteControlService
Definition BLEHub.h:144
void SetHubLedColor(HubLedColor color)
Definition BLEHub.cpp:91
std::string GetRawAddress()
Definition BLEHub.cpp:47
void SetConnectCallback(std::function< void(bool)> callback)
Definition BLEHub.cpp:42
void initChannelControllers()
Definition BLEHub.cpp:218
bool attachCharacteristic(NimBLEUUID serviceUUID, NimBLEUUID characteristicUUID)
Definition BLEHub.cpp:270
void SetManualBrake(const bool enabled)
Definition BLEHub.cpp:102
void Execute(MCLocoAction *action)
Definition BLEHub.cpp:73
NimBLEAddress GetAddress()
Definition BLEHub.cpp:52
uint16_t _watchdogTimeOutInTensOfSeconds
Definition BLEHub.h:143
NimBLERemoteCharacteristic * _remoteControlCharacteristic
Definition BLEHub.h:145
void setTargetPwrPercByAttachedDevice(DeviceType device, int16_t minPwrPerc, int16_t pwrPerc)
Definition BLEHub.cpp:229
bool _blinkLights
Definition BLEHub.h:139
virtual void DriveTaskLoop()=0
bool _isConnected
Definition BLEHub.h:142
TaskHandle_t _driveTaskHandle
Definition BLEHub.h:132
BLEHub(BLEHubConfiguration *config)
Definition BLEHub.cpp:11
bool Connect(const uint8_t watchdogTimeOutInTensOfSeconds)
Definition BLEHub.cpp:136
HubLedColor getRawLedColorForController(BLEHubChannelController *controller)
Definition BLEHub.cpp:239
virtual void NotifyCallback(NimBLERemoteCharacteristic *pBLERemoteCharacteristic, uint8_t *pData, size_t length, bool isNotify)=0
void dumpPData(uint8_t *pData, size_t length)
Definition BLEHub.cpp:321
void Drive(const int16_t minPwrPerc, const int16_t pwrPerc)
Definition BLEHub.cpp:57
virtual int16_t MapPwrPercToRaw(int pwrPerc)=0
bool IsDiscovered()
Definition BLEHub.cpp:32
static void driveTaskImpl(void *)
Definition BLEHub.cpp:297
BLEHubChannelController * findControllerByChannel(BLEHubChannel channel)
Definition BLEHub.cpp:259
ulong _blinkUntil
Definition BLEHub.h:140
void connected()
Definition BLEHub.cpp:302
BLEHubConfiguration * _config
Definition BLEHub.h:129
std::function< void(bool)> _onConnectionChangedCallback
Definition BLEHub.h:127
virtual bool SetWatchdogTimeout(const uint8_t watchdogTimeOutInTensOfSeconds)=0
bool IsConnected()
Definition BLEHub.cpp:37
std::vector< BLEHubChannelController * > _channelControllers
Definition BLEHub.h:130
bool _mbrake
Definition BLEHub.h:137
NimBLEClient * _hub
Definition BLEHub.h:135
uint8_t getRawChannelPwrForController(BLEHubChannelController *controller)
Definition BLEHub.cpp:249
NimBLEAdvertisedDevice * _advertisedDevice
Definition BLEHub.h:133
int16_t GetCurrentDrivePwrPerc()
Definition BLEHub.cpp:62
bool startDriveTask()
Definition BLEHub.cpp:287
bool _isDiscovered
Definition BLEHub.h:141
void BlinkLights(int durationInMs)
Definition BLEHub.cpp:86
void disconnected()
Definition BLEHub.cpp:310
void SetEmergencyBrake(const bool enabled)
Definition BLEHub.cpp:120
NimBLEClientCallbacks * _clientCallback
Definition BLEHub.h:136
Definition MCLocoAction.h:9
HubLedColor
Definition enums.h:40
DeviceType
Definition enums.h:20
MTC4BTController * controller
Definition main.cpp:17