mirror of
https://github.com/portapack-mayhem/mayhem-firmware.git
synced 2025-08-13 14:55:29 +00:00
@@ -23,47 +23,86 @@
|
||||
#include "portapack.hpp"
|
||||
#include <optional>
|
||||
|
||||
#define SENSORUPDATETIME 10
|
||||
|
||||
extern "C" {
|
||||
void complete_i2chost_to_device_transfer(uint8_t* data, size_t length);
|
||||
void create_shell_i2c(EventDispatcher* evtd);
|
||||
}
|
||||
|
||||
namespace i2cdev {
|
||||
|
||||
bool I2cDev_PPmod::init(uint8_t addr_) {
|
||||
if (addr_ != I2CDEV_PPMOD_ADDR_1) return false;
|
||||
addr = addr_;
|
||||
model = I2CDECMDL_PPMOD;
|
||||
query_interval = 10;
|
||||
query_interval = 1; // self timer will handle the update interval per subdevice
|
||||
mask = get_features_mask();
|
||||
if (mask & (uint64_t)SupportedFeatures::FEAT_SHELL) {
|
||||
create_shell_i2c(I2CDevManager::get_event_dispatcher());
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void I2cDev_PPmod::update() {
|
||||
auto mask = get_features_mask();
|
||||
if (mask & (uint64_t)SupportedFeatures::FEAT_GPS) {
|
||||
// mask = get_features_mask(); //saved on init. replug device if something changed. //needs to revise when modules come out.
|
||||
if (mask & (uint64_t)SupportedFeatures::FEAT_GPS && self_timer % SENSORUPDATETIME == 0) {
|
||||
auto data = get_gps_data();
|
||||
if (data.has_value()) {
|
||||
GPSPosDataMessage msg{data.value().latitude, data.value().longitude, (int32_t)data.value().altitude, (int32_t)data.value().speed, data.value().sats_in_use};
|
||||
EventDispatcher::send_message(msg);
|
||||
}
|
||||
}
|
||||
if (mask & (uint64_t)SupportedFeatures::FEAT_ORIENTATION) {
|
||||
if (mask & (uint64_t)SupportedFeatures::FEAT_ORIENTATION && self_timer % SENSORUPDATETIME == 0) {
|
||||
auto data = get_orientation_data();
|
||||
if (data.has_value()) {
|
||||
OrientationDataMessage msg{(uint16_t)data.value().angle, (int16_t)data.value().tilt};
|
||||
EventDispatcher::send_message(msg);
|
||||
}
|
||||
}
|
||||
if (mask & (uint64_t)SupportedFeatures::FEAT_ENVIRONMENT) {
|
||||
if (mask & (uint64_t)SupportedFeatures::FEAT_ENVIRONMENT && self_timer % SENSORUPDATETIME == 0) {
|
||||
auto data = get_environment_data();
|
||||
if (data.has_value()) {
|
||||
EnvironmentDataMessage msg{data.value().temperature, data.value().humidity, data.value().pressure};
|
||||
EventDispatcher::send_message(msg);
|
||||
}
|
||||
}
|
||||
if (mask & (uint64_t)SupportedFeatures::FEAT_LIGHT) {
|
||||
if (mask & (uint64_t)SupportedFeatures::FEAT_LIGHT && self_timer % SENSORUPDATETIME == 0) {
|
||||
auto data = get_light_data();
|
||||
if (data.has_value()) {
|
||||
LightDataMessage msg{data.value()};
|
||||
EventDispatcher::send_message(msg);
|
||||
}
|
||||
}
|
||||
if (mask & (uint64_t)SupportedFeatures::FEAT_SHELL) {
|
||||
auto commcnt = get_shell_buffer_bytes();
|
||||
if (commcnt > 0) {
|
||||
bool has_more = false;
|
||||
uint8_t buff[65]; // 0 th byte is the has_more flag, and size sent
|
||||
do {
|
||||
if (get_shell_get_buffer_data(buff, 65)) {
|
||||
if (buff[0] == 0xff) {
|
||||
break; // error
|
||||
}
|
||||
has_more = buff[0] & 0x80;
|
||||
size_t size = buff[0] & 0x7F;
|
||||
complete_i2chost_to_device_transfer(&buff[1], size);
|
||||
} else {
|
||||
has_more = false;
|
||||
}
|
||||
} while (has_more);
|
||||
}
|
||||
}
|
||||
self_timer++;
|
||||
if (self_timer >= 250) {
|
||||
self_timer = 0; // rounding bc of uint8_t overflow
|
||||
}
|
||||
}
|
||||
|
||||
bool I2cDev_PPmod::get_shell_get_buffer_data(uint8_t* buff, size_t len) {
|
||||
Command cmd = Command::COMMAND_SHELL_MODTOPP_DATA;
|
||||
return i2c_read((uint8_t*)&cmd, 2, buff, len);
|
||||
}
|
||||
|
||||
std::optional<orientation_t> I2cDev_PPmod::get_orientation_data() {
|
||||
@@ -106,18 +145,28 @@ std::optional<uint16_t> I2cDev_PPmod::get_light_data() {
|
||||
return data;
|
||||
}
|
||||
|
||||
uint16_t I2cDev_PPmod::get_shell_buffer_bytes() {
|
||||
Command cmd = Command::COMMAND_SHELL_MODTOPP_DATA_SIZE;
|
||||
uint16_t data;
|
||||
bool success = i2c_read((uint8_t*)&cmd, 2, (uint8_t*)&data, sizeof(uint16_t));
|
||||
if (success == false) {
|
||||
return 0;
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
uint64_t I2cDev_PPmod::get_features_mask() {
|
||||
uint64_t mask = 0;
|
||||
uint64_t mask_ = 0;
|
||||
Command cmd = Command::COMMAND_GETFEATURE_MASK;
|
||||
bool success = i2c_read((uint8_t*)&cmd, 2, (uint8_t*)&mask, sizeof(mask));
|
||||
bool success = i2c_read((uint8_t*)&cmd, 2, (uint8_t*)&mask_, sizeof(mask_));
|
||||
if (success == false) {
|
||||
return 0;
|
||||
}
|
||||
// sanity check
|
||||
if (mask == UINT64_MAX) {
|
||||
if (mask_ == UINT64_MAX) {
|
||||
return 0;
|
||||
}
|
||||
return mask;
|
||||
return mask_;
|
||||
}
|
||||
|
||||
std::optional<I2cDev_PPmod::device_info> I2cDev_PPmod::readDeviceInfo() {
|
||||
|
@@ -55,6 +55,10 @@ class I2cDev_PPmod : public I2cDev {
|
||||
COMMAND_GETFEAT_DATA_ORIENTATION,
|
||||
COMMAND_GETFEAT_DATA_ENVIRONMENT,
|
||||
COMMAND_GETFEAT_DATA_LIGHT,
|
||||
// Shell specific communication
|
||||
COMMAND_SHELL_PPTOMOD_DATA, // pp shell to esp. size not defined
|
||||
COMMAND_SHELL_MODTOPP_DATA_SIZE, // how many bytes the esp has to send to pp's shell
|
||||
COMMAND_SHELL_MODTOPP_DATA, // the actual bytes sent by esp. 1st byte's 1st bit is the "hasmore" flag, the remaining 7 bits are the size of the data. exactly 64 byte follows.
|
||||
|
||||
};
|
||||
|
||||
@@ -85,6 +89,12 @@ class I2cDev_PPmod : public I2cDev {
|
||||
std::optional<orientation_t> get_orientation_data();
|
||||
std::optional<environment_t> get_environment_data();
|
||||
std::optional<uint16_t> get_light_data();
|
||||
uint16_t get_shell_buffer_bytes();
|
||||
bool get_shell_get_buffer_data(uint8_t* buff, size_t len);
|
||||
|
||||
private:
|
||||
uint8_t self_timer = 0;
|
||||
uint64_t mask = 0; // feauture mask, that indicates what the device can do. this will determinate what we will query from the device
|
||||
};
|
||||
|
||||
} /* namespace i2cdev */
|
||||
|
@@ -4,14 +4,15 @@
|
||||
#include <cstdint>
|
||||
|
||||
enum class SupportedFeatures : uint64_t {
|
||||
FEAT_NONE = 0,
|
||||
FEAT_EXT_APP = 1 << 0,
|
||||
FEAT_UART = 1 << 1,
|
||||
FEAT_GPS = 1 << 2,
|
||||
FEAT_ORIENTATION = 1 << 3,
|
||||
FEAT_ENVIRONMENT = 1 << 4,
|
||||
FEAT_LIGHT = 1 << 5,
|
||||
FEAT_DISPLAY = 1 << 6
|
||||
FEAT_NONE = 0, // no polling needed
|
||||
FEAT_EXT_APP = 1 << 0, // supplies external app
|
||||
FEAT_UART = 1 << 1, // supplies UART communication
|
||||
FEAT_GPS = 1 << 2, // can be queried for GPS info
|
||||
FEAT_ORIENTATION = 1 << 3, // can be queried for orientation info
|
||||
FEAT_ENVIRONMENT = 1 << 4, // can be queried for environment info
|
||||
FEAT_LIGHT = 1 << 5, // can be queried for light info
|
||||
FEAT_DISPLAY = 1 << 6, // can handle special display output
|
||||
FEAT_SHELL = 1 << 7, // can handle shell commands (polling needed for that)
|
||||
};
|
||||
|
||||
typedef struct
|
||||
|
@@ -41,6 +41,7 @@ bool I2CDevManager::force_scan = false;
|
||||
Thread* I2CDevManager::thread;
|
||||
std::vector<I2DevListElement> I2CDevManager::devlist;
|
||||
Mutex I2CDevManager::mutex_list{};
|
||||
EventDispatcher* I2CDevManager::_eventDispatcher;
|
||||
|
||||
/*
|
||||
DEAR DEVELOPERS!
|
||||
@@ -343,3 +344,11 @@ msg_t I2CDevManager::timer_fn(void* arg) {
|
||||
}
|
||||
|
||||
}; // namespace i2cdev
|
||||
|
||||
extern "C" int oNofityI2cFromShell(uint8_t* buff, size_t len) {
|
||||
i2cdev::I2cDev* dev = i2cdev::I2CDevManager::get_dev_by_model(I2C_DEVMDL::I2CDECMDL_PPMOD);
|
||||
if (!dev) return 0; // nothing to send to, so /dev/null
|
||||
uint16_t reg = 9; // COMMAND_SHELL_PPTOMOD_DATA;
|
||||
if (dev->i2c_write((uint8_t*)®, 2, buff, len)) return 0;
|
||||
return 0; // shoud have an error handler
|
||||
}
|
||||
|
@@ -87,6 +87,8 @@ class I2CDevManager {
|
||||
static I2cDev* get_dev_by_model(I2C_DEVMDL model); // caller function needs to cast to the specific device!
|
||||
static std::vector<I2C_DEVMDL> get_dev_list_by_model(); // returns the currently discovered
|
||||
static std::vector<uint8_t> get_gev_list_by_addr(); // returns the currently discovered
|
||||
static void setEventDispatcher(EventDispatcher* ed) { _eventDispatcher = ed; }
|
||||
static EventDispatcher* get_event_dispatcher() { return _eventDispatcher; }
|
||||
|
||||
private:
|
||||
static uint16_t scan_interval;
|
||||
@@ -99,6 +101,8 @@ class I2CDevManager {
|
||||
static msg_t timer_fn(void* arg);
|
||||
static Thread* thread;
|
||||
static Mutex mutex_list;
|
||||
static EventDispatcher* _eventDispatcher;
|
||||
};
|
||||
}; // namespace i2cdev
|
||||
|
||||
#endif
|
13
firmware/common/i2cdevmanager_c_api.h
Normal file
13
firmware/common/i2cdevmanager_c_api.h
Normal file
@@ -0,0 +1,13 @@
|
||||
#ifndef I2CDEVMANAGER_C_API_H
|
||||
#define I2CDEVMANAGER_C_API_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
int oNofityI2cFromShell(uint8_t* buff, size_t len);
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
Reference in New Issue
Block a user