This commit is contained in:
2020-06-11 11:25:19 +02:00
parent 5cb02880b3
commit 5177cd5cbd
15 changed files with 1090 additions and 411 deletions

View File

@@ -41,7 +41,8 @@ public:
/** get the pixel-width of the given char */
uint8_t getCharWidht(char c) const {
if (c == 32) {return 3;} // whitespace
return offsets[c-off+1] - offsets[c-off];
const int i = c-off;
return offsets[i+1] - offsets[i];
}
uint8_t getHeight() const {
@@ -57,8 +58,8 @@ public:
return sum;
}
/** draw the given char at the given position */
template <typename Scalar, typename Destination> void draw(const char* c, Scalar dx, Scalar dy, Destination& dst) {
/** draw the given string at the given position */
template <typename Scalar, typename Destination> void draw(const char* c, Scalar dx, Scalar dy, Destination& dst) const {
while(*c) {
draw(*c, dx, dy, dst);
dx += getCharWidht(*c);// + 1;
@@ -67,11 +68,10 @@ public:
}
/** draw the given char at the given position */
template <typename Scalar, typename Destination> void draw(unsigned char c, Scalar dx, Scalar dy, Destination& dst) {
template <typename Scalar, typename Destination> void draw(unsigned char c, Scalar dx, Scalar dy, Destination& dst) const {
if (c == 32) {return;} // skip whitespace
const uint16_t x1 = offsets[c-off];
const uint16_t x2 = offsets[c-off+1];
const uint16_t y1 = 0;
@@ -80,8 +80,8 @@ public:
for (uint16_t y = y1; y < y2; ++y) {
for (uint16_t x = x1; x < x2; ++x) {
const uint16_t idx = (x/8) + (y*this->w/8);
const uint8_t pixel = data[idx] & (1<<(x&7));
if (pixel) {
const uint8_t pxInFont = data[idx] & (1<<(x&7)); // pixel from font glyph
if (pxInFont) {
dst.setPixel(dx+x-x1, dy+y);
}
}

View File

@@ -70,17 +70,23 @@
class SSD1306 {
template <typename I2C> class SSD1306 {
private:
static constexpr uint8_t ADDR7 = 0b0111100;
bool inited = false;
I2C& i2c;
public:
SSD1306(I2C& i2c) : i2c(i2c) {
}
bool isPresent() {
return i2c::query(ADDR7);
return i2c.query(ADDR7);
}
void initOnce() {
@@ -137,15 +143,15 @@ public:
// i2c::stop();
// }
i2c::startWrite(ADDR7);
bool ok = i2c::writeByteAndCheck(0x40);
i2c.startWrite(ADDR7);
bool ok = i2c.writeByteAndCheck(0x40);
if (!ok) {os_printf("failed write data\n");}
for (uint16_t i=0; i < (SSD1306_LCDWIDTH*SSD1306_LCDHEIGHT/8); i++) {
i2c::writeByteAndCheck(data[i]);
i2c.writeByteAndCheck(data[i]);
}
i2c::stop();
i2c.stop();
}
@@ -240,13 +246,13 @@ private:
void sendCommand(uint8_t cmd) {
bool ok;
ok = i2c::startWrite(ADDR7);
ok = i2c.startWrite(ADDR7);
if (!ok) {os_printf("failed start write\n");}
ok = i2c::writeByteAndCheck(0x00); // command
ok = i2c.writeByteAndCheck(0x00); // command
if (!ok) {os_printf("failed command mode\n");}
ok = i2c::writeByteAndCheck(cmd);
ok = i2c.writeByteAndCheck(cmd);
if (!ok) {os_printf("failed command\n");}
i2c::stop();
i2c.stop();
}
};

View File

@@ -1,11 +1,10 @@
#ifndef WS2812B_H
#define WS2812B_H
#include "../../data/Color.h"
#include "../../Platforms.h"
#include "../../Debug.h"
#include "../../io/GPIO.h"
@@ -17,9 +16,9 @@
static constexpr const char* NAME = "WS2812B";
// #define LED_SET_PIN_TO_OUTPUT GPIO5_OUTPUT_SET
// #define LED_SET_PIN_H GPIO5_H
// #define LED_SET_PIN_L GPIO5_L
#define LED_SET_PIN_TO_OUTPUT GPIO5_OUTPUT_SET
#define LED_SET_PIN_H GPIO5_H
#define LED_SET_PIN_L GPIO5_L
//#define NS_PER_TICK ( (1000ul*1000ul*1000ul) / (80ul*1000ul*1000ul) )
@@ -453,36 +452,21 @@
};
#elif false //ESP32
#elif ESP32
#include <driver/gpio.h>
#include <rom/ets_sys.h>
#include <xtensa/core-macros.h>
#include "driver/timer.h"
#include "../../io/I2S.h"
static constexpr timer_group_t grp = TIMER_GROUP_0;
static constexpr timer_idx_t idx = TIMER_0;
template <int PIN_DATA, int NUM_LEDS> class WS2812B {
template <int numLEDs, gpio_num_t outPin> class WS2812B;
static IRAM_ATTR void myISR(void* arg) {
printf("interrupt\n");
//WS2812B* leds = (WS2812B*) arg;
//timer_pause(grp, idx);
//leds->flushOLD();
}
template <int numLEDs, gpio_num_t outPin> class WS2812B {
static constexpr const char* TAG = "WS2812";
/** enable/disable each led */
bool enabled[numLEDs] = {true};
static constexpr const char* TAG = "WS2818B";
/** color-value for each attached LED */
Color colors[numLEDs];
Color colors[NUM_LEDS];
public:
/** enable/disable each led */
bool enabled[NUM_LEDS] = {true};
public:
/** ctor */
WS2812B() {
@@ -490,13 +474,11 @@
}
void init() {
ESP_LOGI(TAG, "init()");
gpio_set_direction(outPin, GPIO_MODE_OUTPUT);
debugMod1(TAG, "init with %d leds", NUM_LEDS);
cfg();
}
/** set the color for the given LED */
void setColor(const uint8_t idx, const Color rgb) {
colors[idx] = rgb;
@@ -504,7 +486,7 @@
/** set the color for all LEDs */
void setColor(const Color rgb) {
for (int idx = 0; idx < numLEDs; ++idx) {
for (int idx = 0; idx < NUM_LEDS; ++idx) {
colors[idx] = rgb;
}
}
@@ -516,7 +498,7 @@
/** enable/disable all LEDs */
void setEnabled(const bool en) {
for (int idx = 0; idx < numLEDs; ++idx) {
for (int idx = 0; idx < NUM_LEDS; ++idx) {
enabled[idx] = en;
}
}
@@ -527,78 +509,49 @@
}
Color& getColor(const uint8_t idx) {
return colors[idx];
return colors[idx];
}
timer_config_t config;
/** flush configured changes */
IRAM_ATTR void flush() {
printf("flush()\n");
config.alarm_en = 1;
config.auto_reload = 1;
config.counter_dir = TIMER_COUNT_UP;
config.divider = 2;
config.intr_type = TIMER_INTR_LEVEL;
config.counter_en = TIMER_PAUSE;
ESP_ERROR_CHECK(timer_init(grp, idx, &config));
ESP_ERROR_CHECK(timer_set_counter_value(grp, idx, 0));
ESP_ERROR_CHECK(timer_set_alarm_value(grp, idx, 2));
ESP_ERROR_CHECK(timer_enable_intr(grp, idx));
ESP_ERROR_CHECK(timer_isr_register(grp, idx, myISR, (void*)this, ESP_INTR_FLAG_IRAM, NULL));
printf("start()\n");
ESP_ERROR_CHECK(timer_start(grp, idx));
void flush() {
flushBrightness(255);
}
void flushBrightness(const uint8_t brightness) {
portMUX_TYPE mux = portMUX_INITIALIZER_UNLOCKED;
portENTER_CRITICAL(&mux);
IRAM_ATTR void flushOLD() {
ESP_LOGI(TAG, "sending %d LEDs", numLEDs);
// important! otherwise often interrupted within
portDISABLE_INTERRUPTS();
//portMUX_TYPE myMutex = portMUX_INITIALIZER_UNLOCKED;
//portENTER_CRITICAL(&myMutex);
//for (volatile int i = 0; i < 256; ++i) {;}
//while (_getCycleCount() > 10000000) {;}
volatile uint8_t _x;
for (int i = 4; i >= 0; --i) {_x = colors[i].r;}
// process each LED
for (int i = 0; i < numLEDs; ++i) {
for (int i = 0; i < NUM_LEDS; ++i) {
portDISABLE_INTERRUPTS();
const volatile Color c = colors[i];
_x = colors[i+1].r;
if (enabled[i]) {
sendByte(c.g);
sendByte(c.r);
sendByte(c.b);
} else {
sendByte(0);
sendByte(0);
sendByte(0);
}
// send each LEDs 24-bit GRB data
const Color rgb = colors[i].brightness(brightness);
sendByte(rgb.g);
sendByte(rgb.r);
sendByte(rgb.b);
}
//portEXIT_CRITICAL(&myMutex);
portENABLE_INTERRUPTS();
portEXIT_CRITICAL(&mux);
delay5000();
}
private:
static inline uint32_t _getCycleCount(void) {
uint32_t ccount;
__asm__ __volatile__("rsr %0,ccount":"=a" (ccount));
return ccount;
}
/** send one whole byte */
inline void sendByte(const uint8_t b) {
if (b & 0b10000000) {send1();} else {send0();}
if (b & 0b01000000) {send1();} else {send0();}
@@ -610,300 +563,111 @@
if (b & 0b00000001) {send1();} else {send0();}
}
/** send a 0 (short 1 pulse, long 0 pulse) */
inline void send0() {
gpio_set_level(outPin, 1);
delayShort();
gpio_set_level(outPin, 0);
delayLong();
}
/** send a 1 (long 1 pulse, short 0 pulse) */
inline void send1() {
gpio_set_level(outPin, 1);
delayLong();
gpio_set_level(outPin, 0);
delayShort();
MyGPIO::set(PIN_DATA);
delay800();
MyGPIO::clear(PIN_DATA);
delay100();
}
inline void delayShort() {
volatile int i;
i = 0; i = 1; i = 2; i = 3;
(void) i;
inline void send0() {
MyGPIO::set(PIN_DATA);
delay100();
MyGPIO::clear(PIN_DATA);
delay800();
}
inline void delayLong() {
volatile int j;
j = 0; j = 1; j = 2; j = 3;
j = 4; j = 5; j = 6; j = 7;
j = 8; j = 9; j = 1; j = 2;
(void) j;
// for (size_t i = 0; i < 16; ++i) {
// asm volatile("nop");
// }
void delay100() {
//for (volatile uint8_t i = 0; i < 1; ++i) {;}
const uint32_t start = _getCycleCount();
while (_getCycleCount() < start + 10) {;}
}
void delay800() {
//for (volatile uint8_t i = 0; i < 10; ++i) {;}
const uint32_t start = _getCycleCount();
while (_getCycleCount() < start + 100) {;}
}
void delay5000() {
//for (volatile uint8_t i = 0; i < 10; ++i) {;}
const uint32_t start = _getCycleCount();
while (_getCycleCount() < start + 1000) {;}
}
void cfg() {
MyGPIO::setOutput(PIN_DATA);
}
};
#ifdef xxx
#elif false //othertest
/** comm via I2S */
using MyI2S = I2S<PIN_DATA,0,0>;
MyI2S i2s;
//#include <driver/gpio.h>
#include <rom/ets_sys.h>
#include <xtensa/core-macros.h>
#include <driver/rmt.h>
#include <soc/rmt_struct.h>
template <int numLEDs, gpio_num_t outPin> class WS2812B {
static constexpr const char* TAG = "WS2812";
static constexpr rmt_channel_t chan = RMT_CHANNEL_0;
//#define portDISABLE_INTERRUPTS() do { XTOS_SET_INTLEVEL(XCHAL_EXCM_LEVEL); portbenchmarkINTERRUPT_DISABLE(); } while (0)
//#define portENABLE_INTERRUPTS() do { portbenchmarkINTERRUPT_RESTORE(0); XTOS_SET_INTLEVEL(0); } while (0)
//#define ENABLE_INTERRUPTS() portENABLE_INTERRUPTS()
//#define DISABLE_INTERRUPTS() portDISABLE_INTERRUPTS()
static constexpr int numBits = (3*8) * 2; // (r,g,b) each 8 bit NOTE! must be < 64 to fit into the buffer!
//static constexpr int numItems = 256;//(numLEDs*3)*8;
//static constexpr int numItems = (numLEDs*3)*8;
rmt_item32_t items[numBits+1]; // + one entry has two bits + 0-terminator (just like within strings)
/** enable/disable each led */
bool enabled[numLEDs] = {true};
/** color-value for each attached LED */
Color colors[numLEDs];
public:
/** ctor */
WS2812B() {
init();
}
void init() {
ESP_LOGI(TAG, "init()");
rmt_config_t cfg;
cfg.rmt_mode = RMT_MODE_TX;
cfg.channel = chan;
cfg.gpio_num = outPin;
cfg.mem_block_num = 1;//chan+1;//8-chan;//chan+1;//8 - chan; //chan+1;//
cfg.clk_div = 8;
cfg.tx_config.loop_en = false;
cfg.tx_config.carrier_en = false;
cfg.tx_config.idle_output_en = true;
cfg.tx_config.idle_level = (rmt_idle_level_t)0;
cfg.tx_config.carrier_freq_hz = 10000;
cfg.tx_config.carrier_level = (rmt_carrier_level_t)1;
cfg.tx_config.carrier_duty_percent = 50;
ESP_ERROR_CHECK(rmt_config(&cfg));
// disable when using custom ISR
//ESP_ERROR_CHECK(rmt_driver_install(chan, 0, 0));
// setup constant values once
for (int idx = 0; idx < numBits; ++idx) {
items[idx].duration0 = 0;
items[idx].level0 = 1;
items[idx].duration1 = 0;
items[idx].level1 = 0;
}
items[numBits].val = 0; // 0 terminator
ESP_LOGI(TAG, "init OK!");
}
/** set the color for the given LED */
void setColor(const uint8_t idx, const Color rgb) {
colors[idx] = rgb;
}
/** set the color for all LEDs */
void setColor(const Color rgb) {
for (int idx = 0; idx < numLEDs; ++idx) {
colors[idx] = rgb;
}
}
/** enable/disable the given LED */
void setEnabled(const uint8_t idx, const bool en) {
enabled[idx] = en;
}
/** enable/disable all LEDs */
void setEnabled(const bool en) {
for (int idx = 0; idx < numLEDs; ++idx) {
enabled[idx] = en;
}
}
/** is the given LED enabled? */
bool isEnabled(const uint8_t idx) const {
return enabled[idx];
}
Color& getColor(const uint8_t idx) {
return colors[idx];
}
int nextLED;
/** flush configured changes */
/*
void flush() {
nextLED = 0;
ESP_LOGI(TAG, "sending %d LEDs", numLEDs);
// important! otherwise often interrupted within
portDISABLE_INTERRUPTS();
for (int i = 0; i < 1024; ++i) {
asm volatile("nop");
void cfg() {
const uint32_t sRate = 1000*1000 / (1.0) / 2 / 2;
i2s.configure(sRate, I2S_BITS_PER_SAMPLE_16BIT, I2S_CHANNEL_STEREO);
}
//waitForTX();
/** flush configured changes, including global brightness */
void flushBrightness(const uint8_t brightness) {
const size_t toWrite = 8+8+8+1;// 8-bit R, 8-bit G, 8-bit B, null-terminator
// seems important for i2s ?
reset();
uint8_t tmp[8*3];
// add null terminator
items[toWrite-1].val = 0;
// process each LED
for (int i = 0; i < NUM_LEDS; ++i) {
// process each LED
for (int i = 0; i < numLEDs; i+=2) {
// send each LEDs 24-bit GRB data
if (enabled[i]) {
const Color rgb = colors[i].brightness(brightness);
sendByte(rgb.g, &tmp[0]);
sendByte(rgb.r, &tmp[8]);
sendByte(rgb.b, &tmp[16]);
} else {
sendByte(0, &tmp[0]);
sendByte(0, &tmp[8]);
sendByte(0, &tmp[16]);
}
enqueueNext();
i2s.add(tmp, sizeof(tmp));
// add null terminator
//items[idx].val = 0;
// wait for sending to complete using the RMT Unit's status
waitForTX();
ESP_ERROR_CHECK(rmt_fill_tx_items(this->chan, items, toWrite, 0));
ESP_ERROR_CHECK(rmt_tx_start(this->chan, true));
// reset for next round
//idx = 0;
}
reset();
}
//waitForTX();
portENABLE_INTERRUPTS();
}
*/
static constexpr int toWrite = 8+8+8+1;
rmt_isr_handle_t isrHandle;
void flush() {
nextLED = 0;
ESP_LOGI(TAG, "sending %d LEDs", numLEDs);
enqueueNext();
ESP_ERROR_CHECK(rmt_isr_register(onISR, this, ESP_INTR_FLAG_LEVEL1, &isrHandle));
ESP_ERROR_CHECK(rmt_set_tx_thr_intr_en(this->chan, true, toWrite/2));
//ESP_ERROR_CHECK(rmt_set_tx_intr_en(this->chan, true));
printf("_start\n");
ESP_ERROR_CHECK(rmt_tx_start(this->chan, true));
}
private:
static IRAM_ATTR void onISR(void* arg) {
printf(".\n");
WS2812B* led = (WS2812B*) arg;
if (led->nextLED < numLEDs) {
led->enqueueNext();
} else {
rmt_tx_stop(led->chan);
}
}
void IRAM_ATTR enqueueNext() {
printf("e\n");
int idx = 0;
if (enabled[nextLED]) {
emplaceByte(colors[nextLED].g, idx);
emplaceByte(colors[nextLED].r, idx);
emplaceByte(colors[nextLED].b, idx);
} else {
emplaceByte(0, idx);
emplaceByte(0, idx);
emplaceByte(0, idx);
void reset() {
const uint8_t lo[64] = {
0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
};
for (int i = 0; i < 8 ; ++i) {
i2s.add(lo, sizeof(lo));
}
}
ESP_ERROR_CHECK(rmt_fill_tx_items(this->chan, items, toWrite, 0));
static constexpr const uint8_t DATA0 = 0b11100000;
static constexpr const uint8_t DATA1 = 0b11111000;
++nextLED;
}
void waitForTX() {
uint32_t status; // status while sending: 16797696 ??
for (int i = 0; i < 2000; ++i) {
rmt_get_status(this->chan, &status);
if (status == 0) {break;}
//if (status != 16797696) {break;}
//if ( (status & (1<<24)) == 0 ) {break;} // somewhat OK
// send 1 LED byte. 1 bit to send = 8 byte i2s. also converts to little endian
inline void sendByte(const uint8_t b, uint8_t* dst) {
if (b & 0b10000000) {dst[1] = DATA1;} else {dst[1] = DATA0;}
if (b & 0b01000000) {dst[0] = DATA1;} else {dst[0] = DATA0;}
if (b & 0b00100000) {dst[3] = DATA1;} else {dst[3] = DATA0;}
if (b & 0b00010000) {dst[2] = DATA1;} else {dst[2] = DATA0;}
if (b & 0b00001000) {dst[5] = DATA1;} else {dst[5] = DATA0;}
if (b & 0b00000100) {dst[4] = DATA1;} else {dst[4] = DATA0;}
if (b & 0b00000010) {dst[7] = DATA1;} else {dst[7] = DATA0;}
if (b & 0b00000001) {dst[6] = DATA1;} else {dst[6] = DATA0;}
}
#endif
// for (int i = 0; i < 1000; ++i) {
// asm volatile("nop");
// }
}
/** send one whole byte */
inline void emplaceByte(const uint8_t b, int& idx) {
if (b & 0b10000000) {emplace1(idx);} else {emplace0(idx);}
if (b & 0b01000000) {emplace1(idx);} else {emplace0(idx);}
if (b & 0b00100000) {emplace1(idx);} else {emplace0(idx);}
if (b & 0b00010000) {emplace1(idx);} else {emplace0(idx);}
if (b & 0b00001000) {emplace1(idx);} else {emplace0(idx);}
if (b & 0b00000100) {emplace1(idx);} else {emplace0(idx);}
if (b & 0b00000010) {emplace1(idx);} else {emplace0(idx);}
if (b & 0b00000001) {emplace1(idx);} else {emplace0(idx);}
}
/** emplace a 0 (short 1 pulse, long 0 pulse) */
inline void emplace0(int& idx) {
items[idx].duration0 = 2;//1;
//items[idx].level0 = 1;
items[idx].duration1 = 8;//7;
//items[idx].level1 = 0;
++idx;
}
/** emplace a 1 (long 1 pulse, short 0 pulse) */
inline void emplace1(int& idx) {
items[idx].duration0 = 7;//6;
//items[idx].level0 = 1;
items[idx].duration1 = 3;//2;
//items[idx].level1 = 0;
++idx;
}
};
};
#endif

33
ext/poti/MCP42xxx.h Normal file
View File

@@ -0,0 +1,33 @@
#include "../../io/GPIO.h"
#include "../../Debug.h"
template <typename SPI, int PIN_CS> class MCP42xxx {
private:
SPI& spi;
public:
/** ctor */
MCP42xxx(SPI& spi) : spi(spi) {
;
}
void setVolume(const uint8_t vol) {
debugMod1(TAG, "setVolume(%d)", vol);
MyGPIO::setOutput(PIN_CS);
MyGPIO::clear(PIN_CS);
const uint8_t cmd = 0b00010011; // xx 01=write xx 11=both
spi.writeByte(cmd);
spi.writeByte(vol);
MyGPIO::set(PIN_CS);
}
};

9
ext/rf/SX1276.h Normal file
View File

@@ -0,0 +1,9 @@
#ifndef RF_SX1276
#define RF_SX1276
class SX1276 {
};
#endif

View File

@@ -1,9 +1,10 @@
#ifndef SENS_BME280
#define SENS_BME280
#include "../../io/SoftI2C.h"
#include "../../Platforms.h"
#include "../../Debug.h"
class BME280 {
template <typename I2C> class BME280 {
static constexpr const char* NAME = "BME280";
@@ -53,11 +54,15 @@ public:
} cal;
I2C& i2c;
BME280(I2C& i2c) : i2c(i2c) {
}
bool isPresent() {
return i2c::query(ADDR7);
return i2c.query(ADDR7);
}
private:
@@ -178,20 +183,20 @@ public:
bool ok;
// address the slave in write mode and select the first register to read
ok = i2c::startWrite(ADDR7);
ok = i2c.startWrite(ADDR7);
if (!ok) {os_printf("failed start write\n"); return false;}
ok = i2c::writeByteAndCheck(addr);
ok = i2c.writeByteAndCheck(addr);
if (!ok) {os_printf("failed to select register %d\n", addr); return false;}
//i2c::stop();
// address the slave in read mode and read [len] registers
ok = i2c::startRead(ADDR7);
ok = i2c.startRead(ADDR7);
if (!ok) {os_printf("failed start read\n"); return 0;}
i2c::readBytes(dst, len);
i2c.readBytes(dst, len);
// done
i2c::stop();
i2c.stop();
return true;
}
@@ -201,15 +206,15 @@ public:
bool ok;
// address the slave in write mode and select the first register to read
ok = i2c::startWrite(ADDR7);
ok = i2c.startWrite(ADDR7);
if (!ok) {os_printf("failed start write\n"); return false;}
ok = i2c::writeByteAndCheck(addr);
ok = i2c.writeByteAndCheck(addr);
if (!ok) {os_printf("failed to select register %d\n", addr); return false;}
ok = i2c::writeBytesAndCheck(src, len);
ok = i2c.writeBytesAndCheck(src, len);
if (!ok) {os_printf("failed to write register contents \n"); return false;}
// done
i2c::stop();
i2c.stop();
return true;
}

415
ext/sens/MAX30102.h Normal file
View File

@@ -0,0 +1,415 @@
#ifndef MAX30102_H
#define MAX30102_H
#include "../../Platforms.h"
//#include "../../io/HardI2C.h"
#include "../../io/SoftI2C.h"
// https://www.roboter-bausatz.de/media/pdf/bf/52/37/RBS12853_MAX30102-DS-917698.pdf
// https://github.com/sparkfun/SparkFun_MAX3010x_Sensor_Library/blob/master/src/MAX30105.cpp
// https://github.com/vrano714/max30102-tutorial-raspberrypi/blob/master/max30102.py
/** SPI-like touch controller */
template <typename I2C> class Max30102 {
public:
class Listener {
public:
virtual void onIR(const uint32_t ir) = 0;
virtual void onRed(const uint32_t red) = 0;
};
private:
I2C& i2c;
static constexpr const char* NAME = "Max30102";
static constexpr uint8_t ADDR = 0b1010111;
// Status Registers
static const uint8_t MAX30105_INTSTAT1 = 0x00;
static const uint8_t MAX30105_INTSTAT2 = 0x01;
static const uint8_t MAX30105_INTENABLE1 = 0x02;
static const uint8_t MAX30105_INTENABLE2 = 0x03;
// FIFO Registers
static const uint8_t MAX30105_FIFOWRITEPTR = 0x04;
static const uint8_t MAX30105_FIFOOVERFLOW = 0x05;
static const uint8_t MAX30105_FIFOREADPTR = 0x06;
static const uint8_t MAX30105_FIFODATA = 0x07;
// Configuration Registers
static const uint8_t MAX30105_FIFOCONFIG = 0x08;
static const uint8_t MAX30105_MODECONFIG = 0x09;
static const uint8_t MAX30105_PARTICLECONFIG = 0x0A; // Note, sometimes listed as "SPO2" config in datasheet (pg. 11)
static const uint8_t MAX30105_LED1_PULSEAMP = 0x0C;
static const uint8_t MAX30105_LED2_PULSEAMP = 0x0D;
//static const uint8_t MAX30105_LED3_PULSEAMP = 0x0E; // not available for MAX30102
static const uint8_t MAX30105_LED_PROX_AMP = 0x10;
static const uint8_t MAX30105_MULTILEDCONFIG1 = 0x11;
static const uint8_t MAX30105_MULTILEDCONFIG2 = 0x12;
// Die Temperature Registers
static const uint8_t MAX30105_DIETEMPINT = 0x1F;
static const uint8_t MAX30105_DIETEMPFRAC = 0x20;
static const uint8_t MAX30105_DIETEMPCONFIG = 0x21;
// Proximity Function Registers
static const uint8_t MAX30105_PROXINTTHRESH = 0x30;
// Part ID Registers
static const uint8_t MAX30105_REVISIONID = 0xFE;
static const uint8_t MAX30105_PARTID = 0xFF; // Should always be 0x15. Identical to MAX30102.
// MAX30105 Commands
// Interrupt configuration (pg 13, 14)
static const uint8_t MAX30105_INT_A_FULL_MASK = (uint8_t)~0b10000000;
static const uint8_t MAX30105_INT_A_FULL_ENABLE = 0x80;
static const uint8_t MAX30105_INT_A_FULL_DISABLE = 0x00;
static const uint8_t MAX30105_INT_DATA_RDY_MASK = (uint8_t)~0b01000000;
static const uint8_t MAX30105_INT_DATA_RDY_ENABLE = 0x40;
static const uint8_t MAX30105_INT_DATA_RDY_DISABLE = 0x00;
static const uint8_t MAX30105_INT_ALC_OVF_MASK = (uint8_t)~0b00100000;
static const uint8_t MAX30105_INT_ALC_OVF_ENABLE = 0x20;
static const uint8_t MAX30105_INT_ALC_OVF_DISABLE = 0x00;
static const uint8_t MAX30105_INT_PROX_INT_MASK = (uint8_t)~0b00010000;
static const uint8_t MAX30105_INT_PROX_INT_ENABLE = 0x10;
static const uint8_t MAX30105_INT_PROX_INT_DISABLE = 0x00;
static const uint8_t MAX30105_INT_DIE_TEMP_RDY_MASK = (uint8_t)~0b00000010;
static const uint8_t MAX30105_INT_DIE_TEMP_RDY_ENABLE = 0x02;
static const uint8_t MAX30105_INT_DIE_TEMP_RDY_DISABLE = 0x00;
static const uint8_t MAX30105_SAMPLEAVG_MASK = (uint8_t)~0b11100000;
static const uint8_t MAX30105_SAMPLEAVG_1 = 0x00;
static const uint8_t MAX30105_SAMPLEAVG_2 = 0x20;
static const uint8_t MAX30105_SAMPLEAVG_4 = 0x40;
static const uint8_t MAX30105_SAMPLEAVG_8 = 0x60;
static const uint8_t MAX30105_SAMPLEAVG_16 = 0x80;
static const uint8_t MAX30105_SAMPLEAVG_32 = 0xA0;
static const uint8_t MAX30105_ROLLOVER_MASK = 0xEF;
static const uint8_t MAX30105_ROLLOVER_ENABLE = 0x10;
static const uint8_t MAX30105_ROLLOVER_DISABLE = 0x00;
static const uint8_t MAX30105_A_FULL_MASK = 0xF0;
// Mode configuration commands (page 19)
static const uint8_t MAX30105_SHUTDOWN_MASK = 0x7F;
static const uint8_t MAX30105_SHUTDOWN = 0x80;
static const uint8_t MAX30105_WAKEUP = 0x00;
static const uint8_t MAX30105_RESET_MASK = 0xBF;
static const uint8_t MAX30105_RESET = 0x40;
static const uint8_t MAX30105_MODE_MASK = 0xF8;
static const uint8_t MAX30105_MODE_REDONLY = 0x02;
static const uint8_t MAX30105_MODE_REDIRONLY = 0x03;
static const uint8_t MAX30105_MODE_MULTILED = 0x07;
// Particle sensing configuration commands (pgs 19-20)
static const uint8_t MAX30105_ADCRANGE_MASK = 0x9F;
static const uint8_t MAX30105_ADCRANGE_2048 = 0x00;
static const uint8_t MAX30105_ADCRANGE_4096 = 0x20;
static const uint8_t MAX30105_ADCRANGE_8192 = 0x40;
static const uint8_t MAX30105_ADCRANGE_16384 = 0x60;
static const uint8_t MAX30105_SAMPLERATE_MASK = 0xE3;
static const uint8_t MAX30105_SAMPLERATE_50 = 0x00;
static const uint8_t MAX30105_SAMPLERATE_100 = 0x04;
static const uint8_t MAX30105_SAMPLERATE_200 = 0x08;
static const uint8_t MAX30105_SAMPLERATE_400 = 0x0C;
static const uint8_t MAX30105_SAMPLERATE_800 = 0x10;
static const uint8_t MAX30105_SAMPLERATE_1000 = 0x14;
static const uint8_t MAX30105_SAMPLERATE_1600 = 0x18;
static const uint8_t MAX30105_SAMPLERATE_3200 = 0x1C;
static const uint8_t MAX30105_PULSEWIDTH_MASK = 0xFC;
static const uint8_t MAX30105_PULSEWIDTH_69 = 0x00;
static const uint8_t MAX30105_PULSEWIDTH_118 = 0x01;
static const uint8_t MAX30105_PULSEWIDTH_215 = 0x02;
static const uint8_t MAX30105_PULSEWIDTH_411 = 0x03;
//Multi-LED Mode configuration (pg 22)
static const uint8_t MAX30105_SLOT1_MASK = 0xF8;
static const uint8_t MAX30105_SLOT2_MASK = 0x8F;
static const uint8_t MAX30105_SLOT3_MASK = 0xF8;
static const uint8_t MAX30105_SLOT4_MASK = 0x8F;
static const uint8_t SLOT_NONE = 0x00;
static const uint8_t SLOT_RED_LED = 0x01;
static const uint8_t SLOT_IR_LED = 0x02;
static const uint8_t SLOT_GREEN_LED = 0x03;
static const uint8_t SLOT_NONE_PILOT = 0x04;
static const uint8_t SLOT_RED_PILOT = 0x05;
static const uint8_t SLOT_IR_PILOT = 0x06;
static const uint8_t SLOT_GREEN_PILOT = 0x07;
static const uint8_t MAX_30105_EXPECTEDPARTID = 0x15;
Listener* listener = nullptr;
public:
Max30102(I2C& i2c) : i2c(i2c) {
;
}
void setListener(Listener* l) {
this->listener = l;
}
bool isPresent() {
return i2c.query(ADDR);
}
uint8_t getRevisionID() {
return i2c.readReg8(ADDR, MAX30105_REVISIONID);
}
uint8_t getPartID() {
return i2c.readReg8(ADDR, MAX30105_PARTID);
}
void softReset(void) {
bitMask(MAX30105_MODECONFIG, MAX30105_RESET_MASK, MAX30105_RESET);
// Poll for bit to clear, reset is then complete
for (int i = 0; i < 100; ++i) {
const uint8_t response = i2c.readReg8(ADDR, MAX30105_MODECONFIG);
if ((response & MAX30105_RESET) == 0) break; //We're done!
vTaskDelay(1 / portTICK_PERIOD_MS);
}
ESP_LOGI(NAME, "softReset done");
}
// void MAX30105::shutDown(void) {
// // Put IC into low power mode (datasheet pg. 19)
// // During shutdown the IC will continue to respond to I2C commands but will
// // not update with or take new readings (such as temperature)
// bitMask(MAX30105_MODECONFIG, MAX30105_SHUTDOWN_MASK, MAX30105_SHUTDOWN);
// }
void setModePulse() {
//wakeUp();
softReset();
//wakeUp();
writeReg8(MAX30105_INTENABLE1, 0x00);// | MAX30105_INT_DATA_RDY_ENABLE);//64);//16|64);//0xC0);
writeReg8(MAX30105_INTENABLE2, 0x00);
//writeReg8(MAX30105_PROXINTTHRESH, 0);
//writeReg8(MAX30105_PifROXINTTHRESH, 64);
//setLEDMode(MAX30105_MODE_REDONLY);
setLEDMode(MAX30105_MODE_REDIRONLY);
//setLEDMode(MAX30105_MODE_MULTILED);
writeReg8(MAX30105_FIFOCONFIG, MAX30105_SAMPLEAVG_8 | MAX30105_ROLLOVER_ENABLE);
//setFIFOAverage(MAX30105_SAMPLEAVG_8);
setADCRange(MAX30105_ADCRANGE_16384);
setSampleRate(MAX30105_SAMPLERATE_200);
setPulseWidth(MAX30105_PULSEWIDTH_215);
//Default is 0x1F which gets us 6.4mA
//powerLevel = 0x02, 0.4mA - Presence detection of ~4 inch
//powerLevel = 0x1F, 6.4mA - Presence detection of ~8 inch
//powerLevel = 0x7F, 25.4mA - Presence detection of ~8 inch
//powerLevel = 0xFF, 50.0mA - Presence detection of ~12 inch
const int powerLevel = 0x30;
setPulseAmplitudeRed(powerLevel);
setPulseAmplitudeIR(powerLevel);
setPulseAmplitudeProximity(powerLevel);
// THIS ONE ONLY WORKS FOR "MAX30105_MODE_MULTILED" ?!
// writeReg8(MAX30105_MULTILEDCONFIG1, (SLOT_RED_PILOT<<4) | (SLOT_RED_LED<<0) );
// writeReg8(MAX30105_MULTILEDCONFIG2, (SLOT_IR_PILOT<<4) | (SLOT_IR_LED<<0) );
// ------
// writeReg8(MAX30105_MULTILEDCONFIG1, (SLOT_IR_LED<<4) | (SLOT_RED_LED<<0) );
// writeReg8(MAX30105_MULTILEDCONFIG2, 0 );
clearFIFO();
}
void writeReg8(const uint8_t reg, const uint8_t val) {
ESP_LOGI(NAME, "write reg: 0x%02x <- val: %d", reg, val);
i2c.writeReg8(ADDR, reg, val);
}
uint8_t readReg8(const uint8_t reg) {
return i2c.readReg8(ADDR, reg);
}
// //Check for new data but give up after a certain amount of time
// //Returns true if new data was found
// //Returns false if new data was not found
// bool safeCheck(uint8_t maxTimeToCheck) {
// for (int i = 0; i < maxTimeToCheck; ++i) {
// if(check() == true) {
// return(true);
// }
// vTaskDelay(1 / portTICK_PERIOD_MS);
// }
// return false;
// }
void check(void) {
// const uint8_t intr1 = readReg8(MAX30105_INTSTAT1);
// const uint8_t intr2 = readReg8(MAX30105_INTSTAT2);
// // new data available?
// //if (intr1 & 64) { .. }
uint8_t readPointer = getReadPointer();
const uint8_t writePointer = getWritePointer();
//uint8_t readPointer = getReadPointer();
//const uint8_t writePointer = getWritePointer();
//ESP_LOGI(NAME, "intr. %d %d", readPointer, writePointer);
while (readPointer != writePointer) {
//printf(";%d;%d\n", readPointer, writePointer);
uint8_t tmp[6];
i2c.readReg(ADDR, MAX30105_FIFODATA, 6, tmp);
const uint32_t val1 = ((tmp[0] << 16) | (tmp[1] << 8) | (tmp[2] << 0)) & 0x3FFFF; //Zero out all but 18 bits
const uint32_t val2 = ((tmp[3] << 16) | (tmp[4] << 8) | (tmp[5] << 0)) & 0x3FFFF; //Zero out all but 18 bits
//printf(";%d;%d;%d;%d\n", readPointer, writePointer, val1, val2);//, val3, val4);
// inc
readPointer = (readPointer + 1) % 32;
// callback
if (listener) {
listener->onRed(val1);
listener->onIR(val2);
}
}
}
int8_t getTemp() {
// trigger temp acquire
writeReg8(MAX30105_DIETEMPCONFIG, 1);
int8_t full = readReg8(MAX30105_DIETEMPINT);
//uint8_f frac = i2c.readReg8(MAX30105_DIETEMPFRAC);
return full;
}
private:
void wakeUp(void) {
// Pull IC out of low power mode (datasheet pg. 19)
bitMask(MAX30105_MODECONFIG, MAX30105_SHUTDOWN_MASK, MAX30105_WAKEUP);
}
void setLEDMode(uint8_t mode) {
// Set which LEDs are used for sampling -- Red only, RED+IR only, or custom.
// See datasheet, page 18
bitMask(MAX30105_MODECONFIG, MAX30105_MODE_MASK, mode);
}
void setPulseAmplitudeRed(uint8_t amplitude) {
writeReg8( MAX30105_LED1_PULSEAMP, amplitude);
}
void setPulseAmplitudeIR(uint8_t amplitude) {
writeReg8(MAX30105_LED2_PULSEAMP, amplitude);
}
// void setPulseAmplitudeGreen(uint8_t amplitude) {
// writeReg8(MAX30105_LED3_PULSEAMP, amplitude);
// }
void setPulseAmplitudeProximity(uint8_t amplitude) {
writeReg8(MAX30105_LED_PROX_AMP, amplitude);
}
void setFIFOAverage(uint8_t numberOfSamples) {
bitMask(MAX30105_FIFOCONFIG, MAX30105_SAMPLEAVG_MASK, numberOfSamples);
}
void enableFIFORollover(void) {
bitMask(MAX30105_FIFOCONFIG, MAX30105_ROLLOVER_MASK, MAX30105_ROLLOVER_ENABLE);
}
void setADCRange(uint8_t adcRange) {
// adcRange: one of MAX30105_ADCRANGE_2048, _4096, _8192, _16384
bitMask(MAX30105_PARTICLECONFIG, MAX30105_ADCRANGE_MASK, adcRange);
}
void setSampleRate(uint8_t sampleRate) {
// sampleRate: one of MAX30105_SAMPLERATE_50, _100, _200, _400, _800, _1000, _1600, _3200
bitMask(MAX30105_PARTICLECONFIG, MAX30105_SAMPLERATE_MASK, sampleRate);
}
void setPulseWidth(uint8_t pulseWidth) {
// pulseWidth: one of MAX30105_PULSEWIDTH_69, _188, _215, _411
bitMask(MAX30105_PARTICLECONFIG, MAX30105_PULSEWIDTH_MASK, pulseWidth);
}
void clearFIFO(void) {
writeReg8(MAX30105_FIFOWRITEPTR, 0);
writeReg8(MAX30105_FIFOOVERFLOW, 0);
writeReg8(MAX30105_FIFOREADPTR, 0);
}
//Read the FIFO Write Pointer
uint8_t getWritePointer(void) {
return readReg8(MAX30105_FIFOWRITEPTR);
}
//Read the FIFO Read Pointer
uint8_t getReadPointer(void) {
return readReg8(MAX30105_FIFOREADPTR);
}
//Given a register, read it, mask it, and then set the thing
void bitMask(const uint8_t reg, const uint8_t mask, const uint8_t thing) {
const uint8_t orig = i2c.readReg8(ADDR, reg);
const uint8_t masked = orig & mask;
const uint8_t out = masked | thing;
ESP_LOGI(NAME, "reg: %d - orig: %d new: %d", reg, orig, out);
writeReg8(reg, out);
}
};
#endif // MAX30102_H