/*
Arduino-MAX30100 oximetry / heart rate integrated sensor library
Copyright (C) 2016 OXullo Intersecans <x@brainrapers.org>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// This example must be used in conjunction with the Processing sketch located
// in extras/rolling_graph
#include <Wire.h>
#include "MAX30100_PulseOximeter.h"
#include <TFT_eSPI.h>
#include "font.h"
#define I2C_SDA 21
#define I2C_SCL 22
#define REPORTING_PERIOD_MS 1000
TFT_eSPI tft = TFT_eSPI();
//全局变量
int sWidth = TFT_WIDTH; //屏幕宽度
int sHeight = TFT_HEIGHT; //屏幕高度
int bgColor = TFT_BLACK; //背景颜色
int fgColor = TFT_ORANGE; //标题颜色
String redxg ; //血氧
String heart ; //标题颜色
// PulseOximeter is the higher level interface to the sensor
// it offers:
// * beat detection reporting
// * heart rate calculation
// * SpO2 (oxidation level) calculation
PulseOximeter pox;
uint32_t tsLastReport = 0;
// Callback (registered below) fired when a pulse is detected
void onBeatDetected()
{
Serial.println("B:1");
}
void showNum(String text, int16_t x, int16_t y, uint16_t color, uint16_t bg) {
tft.setTextSize(1);
tft.setTextColor(color, bg);
tft.drawString(text, x, y, 6);
//tft.setFreeFont(&Orbitron_Light_32);
//tft.drawString(text, x, y);
}
void showText(String text, int32_t x, int32_t y, uint32_t color) {
int i = 0;
int x1 = x;
while (i < text.length()) {
char t1 = text.charAt(i);
if (t1 >= 0 && t1 <= 127) {
//英文字符
tft.setTextSize(1);
tft.setTextColor(color);
tft.drawString((String)t1, x1, y+2, 2);
i ++;
x1 += 10;
} else {
//全角字符
char t2 = text.charAt(i+1);
char t3 = text.charAt(i+2);
//全角占3个字符
for (int k = 0; k < sizeof(FONT_CN) / sizeof(FONT_CN[0]); k++){
if (FONT_CN[k].cnIndex[0] == t1 && FONT_CN[k].cnIndex[1] == t2 && FONT_CN[k].cnIndex[2] == t3)
tft.drawBitmap(x1, y, FONT_CN[k].cnName, FONT_CN[k].cnPixel, FONT_CN[k].cnPixel, color);
}
i += 3;
x1 += 20;
}
}
}
//获取字符宽度
int textWidth(String text) {
int i = 0;
int x1 = 0;
while (i < text.length()) {
char t1 = text.charAt(i);
if (t1 >= 0 && t1 <= 127) {
i ++;
x1 += 10;
} else {
i += 3;
x1 += 20;
}
}
return x1;
}
void setup()
{
Serial.begin(115200);
//初始化TFT
tft.init();
//缺少这个位图颜色会反转
tft.setSwapBytes(true);
tft.fillScreen(TFT_RED);
// Initialize the PulseOximeter instance and register a beat-detected callback
// The parameter passed to the begin() method changes the samples flow that
// the library spews to the serial.
// Options:
// * PULSEOXIMETER_DEBUGGINGMODE_PULSEDETECT : filtered samples and beat detection threshold
// * PULSEOXIMETER_DEBUGGINGMODE_RAW_VALUES : sampled values coming from the sensor, with no processing
// * PULSEOXIMETER_DEBUGGINGMODE_AC_VALUES : sampled values after the DC removal filter
// Initialize the PulseOximeter instance
// Failures are generally due to an improper I2C wiring, missing power supply
// or wrong target chip
if (!pox.begin(PULSEOXIMETER_DEBUGGINGMODE_PULSEDETECT)) {
Serial.println("ERROR: Failed to initialize pulse oximeter");
for(;;);
}
pox.setOnBeatDetectedCallback(onBeatDetected);
}
void loop()
{
// Make sure to call update as fast as possible
pox.update();
// Asynchronously dump heart rate and oxidation levels to the serial
// For both, a value of 0 means "invalid"
if (millis() - tsLastReport > REPORTING_PERIOD_MS) {
Serial.print("H:");
Serial.println(pox.getHeartRate());
Serial.print("O:");
Serial.println(pox.getSpO2());
tsLastReport = millis();
heart =String(pox.getHeartRate());
redxg =String(pox.getSpO2());
}
showNum(redxg, 10, 10, fgColor,bgColor);
showNum(heart, 10, 90,fgColor,bgColor);
}
记录之 esp32血氧+tft显示
最新推荐文章于 2024-10-16 11:01:07 发布