#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <FilterDerivative.h>
#include <FilterOnePole.h>
#include <Filters.h>
#include <FilterTwoPole.h>
#include <FloatDefine.h>
#include <RunningStatistics.h>
#include "ButtonKing.h"
#define TFT_DC 2
#define TFT_CS 15
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
#include <Fonts/FreeSans9pt7b.h>
#include <Fonts/FreeSans12pt7b.h>
#include <Fonts/FreeSerif12pt7b.h>
//
#include "Fonts/FreeSansBold9pt7b.h"//
// #include <FreeDefaultFonts.h>
#define BLACK 0x0000 /* 0, 0, 0 */
#define NAVY 0x000F /* 0, 0, 128 */
#define DARKGREEN 0x03E0 /* 0, 128, 0 */
#define DARKCYAN 0x03EF /* 0, 128, 128 */
#define MAROON 0x7800 /* 128, 0, 0 */
#define PURPLE 0x780F /* 128, 0, 128 */
#define OLIVE 0x7BE0 /* 128, 128, 0 */
#define LIGHTGREY 0xC618 /* 192, 192, 192 */
#define DARKGREY 0x7BEF /* 128, 128, 128 */
#define BLUE 0x001F /* 0, 0, 255 */
#define GREEN 0x07E0 /* 0, 255, 0 */
#define CYAN 0x07FF /* 0, 255, 255 */
#define RED 0xF800 /* 255, 0, 0 */
#define MAGENTA 0xF81F /* 255, 0, 255 */
#define YELLOW 0xFFE0 /* 255, 255, 0 */
#define WHITE 0xFFFF /* 255, 255, 255 */
#define ORANGE 0xFDA0 /* 255, 180, 0 */
#define GREENYELLOW 0xB7E0 /* 180, 255, 0 */
#define PINK 0xFC9F
int CENTER = 0, LIFT = 2, RIGHT = 1;
int height;
int width ;
//-------------Sound------------//
byte Mic_Pin1 = 34;
uint32_t Sound_VALUE_MAX = 10000, Sound_VALUE_MIN = 0;
uint32_t Time_Draw_Sound = 0;
const int numreadings_Sound = 250;
float readings_Sound[numreadings_Sound]; // the readings_Sound from the analog input
int readIndex_Sound = 0; // the index of the current reading
float total_Sound = 0; // the running total_Sound
double average_Sound = 0;
uint32_t time_read_mic = 0;
long shutdownTimer = 0;
byte Pin_on_off = 26;
//---------------DAC control sound vol---------------//
#define DAC_CH1 25
#define DAC_CH2 26
#define WINDOW_SIZE 15
int INDEX = 0;
int VALUE = 0;
int SUM = 0;
int READINGS[WINDOW_SIZE];
int AVERAGED = 0;
#define Record 1
#define Show 2
#define Stop 3
byte Record_State = Stop;
int Record_Array[10000];
int Record_Array_NO_Show = 0;
int Record_Array_NO = 0;
//__________________________________________________//
//-------HR Sound CTG -------------//
int a = 0;
int lasta = 0;
int lastb = 0;
int LastTime = 0;
int ThisTime;
bool BPMTiming = false;
bool BeatComplete = false;
int BPM = 0;
#define UpperThreshold 500
#define LowerThreshold 5
//-----------Filters----------//
float amplifiedSignal;
float filteredSignal;
// filter out frequencies below 1 Hz.
float highFilterFrequency = 1;
FilterOnePole filterOneHighpass( HIGHPASS, highFilterFrequency );
// filters out frequenceies greater than 3 Hz.
float lowFilterFrequency = 3;
// create a lowpass filter that only keeps frequencies below lowFilterFrequency
FilterOnePole filterOneLowpass(LOWPASS, lowFilterFrequency);
//-----------------HR read----------------//
float fScalingFactor = 0.0;
float fAmplificationFactor = 2.1;
int iPhototransistorSignalIR = 0; // Var used to hold the raw Phototransistor value
int iPulseValueIR = 0;
int iPulseValueIROld = 0;
char cStringArray[100]; // Temp string array used to print numbers to the TFT display
int lADC0MaxValue = 0; // The calculated pulse wave mimimum with finger in place and IR source turned on
int lADC0MinValue = 0; // The calculated pulse wave mimimum with finger in place and IR source turned on
int lADC0MaxValue2 = 0; // The calculated pulse wave mimimum with finger in place and IR source turned on
int lADC0MinValue2 = 0; // The calculated pulse wave mimimum with finger in place and IR source turned on
int iADC0SpanValue = 0; // The calculated pulse wave span (max-min) with finger in place and IR source turned on
#define ADC_MAX_VAL 5000
int iBPM = 0;
int iBPMOld = 0;
long lBPMTemp = 0;
int iPulseTriggerLevelHigh = 0;
int iPulseTriggerLevelLow = 0;
bool bRisingEdgeDetected = false;
bool bFallingEdgeDetected = false;
#define MAX_BPM_ARRAY_SIZE 25
long lBPMArray[MAX_BPM_ARRAY_SIZE]; // Holds moving window of BPMs to allow a rolling average to be taken.
int iBPMArrayIndex = 0;
int iBPMAverageIndex = 0;
#define UPPER_PULSE_RATE 220
int iSampleCount = 0; // Holds the incremental counter to be compared with that of below
int iSampleCountMax = 10; // This number dictates how many samples are thrown away before the screen is updated
#define SAMPLE_PERIOD_uS 1000
#define MAX_UL 4294967295UL
unsigned long ulSamplePeriod = SAMPLE_PERIOD_uS;
unsigned long ulCurrentTime = 0;
unsigned long ulOldTime = 0;
unsigned long ulElapsedTime = 0;
unsigned long ulPulseCurrentTime = 0;
unsigned long ulPulseOldTime = 0;
unsigned long Time_calabration = 0;
unsigned long Time_Print_HR = 0;
//-----------Vol Sig value-----------//
const int numreadings_Vol = 100;
float readings_Vol[numreadings_Vol]; // the readings_Vol from the analog input
int readIndex_Vol = 0; // the index of the current reading
float total_Vol = 0; // the running total_Vol
int average_Vol = 0;
uint32_t Time_Read_Vol = 0;
uint32_t Time_Print_Vol = 0;
#define Pin_Vol 35
int volume_New = 0;
int volume_Old = 0;
//______________________________//
const int numreadings_Sig = 100;//100 is true
float readings_Sig[numreadings_Sig]; // the readings_Sig from the analog input
int readIndex_Sig = 0; // the index of the current reading
float total_Sig = 0; // the running total_Sig
int average_Sig = 0;
uint32_t Time_Read_Sig = 0;
uint32_t Time_Print_Sig = 0;
#define Pin_Sig 25
int Signal_New = 0;
int Signal_Old = 0;
//-------------------Buttons--------------------//
#define Button_Incres_Pin 22
#define Button_Dicreses_Pin 21
#define Button_Select_Ok_Pin 26
#define Button_Power_On_Off_Pin 14
byte Button_on_off = 14;
ButtonKing Button_Incres(Button_Incres_Pin, true);
ButtonKing Button_Dicreses(Button_Dicreses_Pin, true);
ButtonKing Button_Select_Ok(Button_Select_Ok_Pin, true);
ButtonKing Button_Power_On_Off(Button_Power_On_Off_Pin, true);
#define Long_Press_Button_Select_Ok_Main_Screen 1
#define Long_Press_Button_Select_Ok_Setting_Screen 2
byte State_Button_Select_Ok = Long_Press_Button_Select_Ok_Main_Screen;
//----------------------------------------------//
//--------------------Screens windows----------------//
#define Main_Screen 1
#define Setting_Screen 2
byte Screen_Window_Current = Main_Screen;
//------------------Setting Vareble-------------------//
byte Avg_Readings_Sound_Current = 180;
byte Avg_Readings_Sound_Befor_setting = 100;
byte Avg_Readings_Sound_In_Setting_New = 100;
byte Avg_Readings_Sound_In_Setting_Old = 100;
byte Avg_Readings_Sound_In_Setting_Current = 100;
byte Avg_Readings_Sound_Max = 250;
byte Avg_Readings_Sound_Min = 100;
byte Time_Draw_Curve = 17;
byte Time_Draw_Curve_Max = 25;
byte Time_Draw_Curve_Min = 10;
byte Avg_Readings_HR = 15;
byte Avg_Readings_HR_Max = 25;
byte Avg_Readings_HR_Min = 5;
#define Non_Setting 1
#define ARS_Setting 2
#define AHR_Setting 3
#define TD_Setting 4
#define ARS_Setting_Active 5
#define AHR_Setting_Active 6
#define TD_Setting_Active 7
byte Mode_Setting = ARS_Setting;
#define No_Incres 0
#define Incres 1
byte Mode_Incres = No_Incres;
#define No_Dicreses 0
#define Dicreses 1
byte Mode_Dicreses = No_Dicreses;
uint32_t Time_Incres_Dicreses=0;
uint32_t Time_Incres_Dicreses_Set=0;
uint16_t Color_Start_Screen;
int colorIndex = 0;
void setup(void)
{
// dacWrite(DAC_CH1, volume );
Serial.begin(115200);
Init_Buttons();
// pinMode(Button_on_off, INPUT_PULLUP);
//analogReadResolution(10);
// hold the power mosfet ON
pinMode(Pin_on_off, OUTPUT);
digitalWrite(Pin_on_off, HIGH);
INIT_TFT_LCD();
Draw_Start_Screen();
INIT_OUTPUT_INPUT();
Draw_Sound_SCREEN();
}
void loop(void)
{
// if (millis() >= shutdownTimer + 300000) {
//// digitalWrite(Pin_on_off, LOW);
// // if the button is pressed
// // (pin Button_on_off is pulled to ground)
// // then extend shutdownTimer
// // and blink the on-board LED
//
// }
// if (!digitalRead(Button_on_off)) {
// shutdownTimer = millis();
// }
Read_Sound();
Read_HR();
Read_Vol();
Read_Sig();
Read_Button();
Test_Serial();
}
void INIT_TFT_LCD() {
tft.begin();
tft.setRotation(0);
tft.fillScreen(BLACK);
height = tft.height();
width = tft.width();
}
void INIT_OUTPUT_INPUT() {
pinMode(Mic_Pin1, INPUT); // Setup for leads off detection LO +
}
void Init_Buttons(){
Serial.println("Starting ButtonQueen...");
// link the button Button_Incres functions.
Button_Incres.setClick(Click_Button_Incres);
Button_Incres.setTimeLong(500);
Button_Incres.setTimeShort(500);
Button_Incres.setDoubleClick(Double_Click_Button_Incres);
Button_Incres.setShortDoubleStart(Short_Double_Press_Start_Button_Incres);
Button_Incres.setLongDoubleStart(Long_Double_Press_Start_Button_Incres);
Button_Incres.setLongDoubleStop(Long_Double_Press_Stop_Button_Incres);
Button_Incres.setShortClickStart(Short_Press_Start_Button_Incres);
Button_Incres.setLongClickStart(Long_Press_Start_Button_Incres);
Button_Incres.setLongClickStop(Long_Press_Stop_Button_Incres);
// link the button Button_Dicreses functions.
Button_Dicreses.setClick(Click_Button_Dicreses);
Button_Dicreses.setTimeLong(500);
Button_Dicreses.setTimeShort(500);
Button_Dicreses.setDoubleClick(Double_Click_Button_Dicreses);
Button_Dicreses.setShortDoubleStart(Short_Double_Press_Start_Button_Dicreses);
Button_Dicreses.setLongDoubleStart(Long_Double_Press_Start_Button_Dicreses);
Button_Dicreses.setLongDoubleStop(Long_Double_Press_Stop_Button_Dicreses);
Button_Dicreses.setShortClickStart(Short_Press_Start_Button_Dicreses);
Button_Dicreses.setLongClickStart(Long_Press_Start_Button_Dicreses);
Button_Dicreses.setLongClickStop(Long_Press_Stop_Button_Dicreses);
// link the button Button_Select_Ok functions.
// Button_Power_On_Off.setClick(Click_Button_Power_On_Off);
Button_Power_On_Off.setTimeLong(500);
Button_Power_On_Off.setTimeShort(500);
// Button_Power_On_Off.setDoubleClick(Double_Click_Button_Power_On_Off);
// Button_Power_On_Off.setShortDoubleStart(Short_Double_Press_Start_Button_Power_On_Off);
Button_Power_On_Off.setLongDoubleStart(Long_Double_Press_Start_Button_Power_On_Off);
Button_Power_On_Off.setLongDoubleStop(Long_Double_Press_Stop_Button_Power_On_Off);
// Button_Power_On_Off.setShortClickStart(Short_Press_Start_Button_Power_On_Off);
// Button_Power_On_Off.setLongClickStart(Long_Press_Start_Button_Power_On_Off);
// Button_Power_On_Off.setLongClickStop(Long_Press_Stop_Button_Power_On_Off);
}
void Draw_Start_Screen(){
for(int k=0;k<2;k++){
Serial.println("time draw "+String(k));
for (int i=0; i<320; i+=5)
{
int i2=320-i;
Color_Start_Screen = updateTextColor();
tft.drawLine(1, i, (i*1.44)-150, 320,Color_Start_Screen);
tft.drawLine(239, i2, (i2*1.44)-75, 0,Color_Start_Screen);
printMsg(20, 160,1,&FreeSans12pt7b,"Smart Stethoscope",BLACK,Color_Start_Screen);
// delay(10);
}}
}
void Draw_End_Screen(){
for(int k=0;k<4;k++){
Serial.println("time draw "+String(k));
for (int i=0; i<320; i+=5)
{
int i2=320-i;
Color_Start_Screen = updateTextColor();
tft.drawLine(1, i, (i*1.44)-150, 320,Color_Start_Screen);
tft.drawLine(239, i2, (i2*1.44)-75, 0,Color_Start_Screen);
printMsg(20, 160,1,&FreeSans12pt7b,"Power Off Device",BLACK,Color_Start_Screen);
// delay(10);
}}
tft.fillScreen(BLACK);
delay(1000);
digitalWrite(Pin_on_off, LOW);
while(1);
}
void Draw_Sound_SCREEN() {
tft.fillScreen(BLACK);
tft.drawRect(0, 0, width, height, WHITE);
Begin_signal_Sound();
Draw_Main_Screen();
labaleText(5, (height / 2) + 130, width - 10, 25, 10, 1, &FreeSansBold9pt7b, "Smart Stethoscope", LIGHTGREY, BLACK, CENTER, false);
}
void Draw_Main_Screen() {
tft.fillRoundRect(3, (height / 2) + 3, 234, 119, 5, BLACK);
tft.drawRoundRect(3, (height / 2) + 3, 234, 119, 5, CYAN);
// labaleText(5, height - 35, 210, 30, 10, 1, &FreeSansBold9pt7b, "( Sound Monitor (SCC) )", GREEN, BLACK, CENTER);
labaleText(5, (height / 2) + 5, 70, 35, 5, 1, &FreeSansBold9pt7b, "HR", ORANGE, BLACK, CENTER, false);
labaleText(5, (height / 2) + 45, 70, 35, 5, 1, &FreeSansBold9pt7b, "Vol", ORANGE, BLACK, CENTER, false);
labaleText(5, (height / 2) + 85, 70, 35, 5, 1, &FreeSansBold9pt7b, "Sig", ORANGE, BLACK, CENTER, false);
//
labaleText(85, (height / 2) + 5, 70, 35, 5, 1, &FreeSansBold9pt7b, "0", CYAN, BLACK, CENTER, false);
labaleText(85, (height / 2) + 45, 70, 35, 5, 1, &FreeSansBold9pt7b, "0", CYAN, BLACK, CENTER, false);
labaleText(85, (height / 2) + 85, 70, 35, 5, 1, &FreeSansBold9pt7b, "0", CYAN, BLACK, CENTER, false);
//
//
labaleText(165, (height / 2) + 5, 70, 35, 5, 1, &FreeSansBold9pt7b, "bpm", ORANGE, BLACK, CENTER, false);
labaleText(165, (height / 2) + 45, 70, 35, 5, 1, &FreeSansBold9pt7b, "%", ORANGE, BLACK, CENTER, false);
labaleText(165, (height / 2) + 85, 70, 35, 5, 1, &FreeSansBold9pt7b, "%", ORANGE, BLACK, CENTER, false);
//
Signal_Old = 0;
volume_Old = 0;
iBPMOld = 0;
}
void Draw_Seting_Screen() {
Mode_Dicreses=No_Dicreses;
Mode_Setting=ARS_Setting;
Mode_Incres=No_Incres;
Avg_Readings_Sound_Befor_setting=Avg_Readings_Sound_Current;
tft.fillRoundRect(3, (height / 2) + 3, 234, 119, 5, BLACK);
tft.drawRoundRect(3, (height / 2) + 3, 234, 119, 5, MAROON);
labaleText(5, (height / 2) + 5, 140, 35, 5, 1, &FreeSansBold9pt7b, "Avg Signal N.O", PINK, BLACK, CENTER, false);
labaleText(5, (height / 2) + 45, 140, 35, 5, 1, &FreeSansBold9pt7b, "Avg Hr N.O", PINK, BLACK, CENTER, false);
labaleText(5, (height / 2) + 85, 140, 35, 5, 1, &FreeSansBold9pt7b, "Time Draw(ms)", PINK, BLACK, CENTER, false);
labaleText(155, (height / 2) + 5, 77, 35, 5, 1, &FreeSansBold9pt7b, String(Avg_Readings_Sound_Current), BLUE, BLACK, CENTER, false);
labaleText(155, (height / 2) + 45, 77, 35, 5, 1, &FreeSansBold9pt7b, String(Avg_Readings_HR), WHITE, BLACK, CENTER, false);
labaleText(155, (height / 2) + 85, 77, 35, 5, 1, &FreeSansBold9pt7b, String(Time_Draw_Curve), WHITE, BLACK, CENTER, false);
}
void Read_Button() {
Button_Incres.isClick();
Button_Dicreses.isClick();
Button_Select_Ok.isClick();
Button_Power_On_Off.isClick();
}
// ----- Button_Incres callback functions
// This function will be called when the Button_Incres was pressed 1 time (and no 2. button press followed).
void Click_Button_Incres() {
if (Screen_Window_Current == Setting_Screen) {
if (Mode_Incres == No_Incres) {
if (Mode_Setting == ARS_Setting) {
Mode_Setting = TD_Setting;
labaleText(155, (height / 2) + 5, 77, 35, 5, 1, &FreeSansBold9pt7b, String(Avg_Readings_Sound_Current), WHITE, BLACK, CENTER, false);
labaleText(155, (height / 2) + 85, 77, 35, 5, 1, &FreeSansBold9pt7b, String(Time_Draw_Curve), BLUE, BLACK, CENTER, false);
}
else if (Mode_Setting == TD_Setting) {
Mode_Setting = AHR_Setting;
labaleText(155, (height / 2) + 45, 77, 35, 5, 1, &FreeSansBold9pt7b, String(Avg_Readings_HR), BLUE, BLACK, CENTER, false);
labaleText(155, (height / 2) + 85, 77, 35, 5, 1, &FreeSansBold9pt7b, String(Time_Draw_Curve), WHITE, BLACK, CENTER, false);
}
else if (Mode_Setting == AHR_Setting) {
Mode_Setting = ARS_Setting;
labaleText(155, (height / 2) + 45, 77, 35, 5, 1, &FreeSansBold9pt7b, String(Avg_Readings_HR), WHITE, BLACK, CENTER, false);
labaleText(155, (height / 2) + 5, 77, 35, 5, 1, &FreeSansBold9pt7b, String(Avg_Readings_Sound_Current), BLUE, BLACK, CENTER, false);
}
}
else if (Mode_Incres == Incres) {
if (Mode_Setting == ARS_Setting_Active) {
labaleText(155, (height / 2) + 5, 77, 35, 5, 1, &FreeSansBold9pt7b, String(Avg_Readings_Sound_Current), BLACK, BLACK, CENTER, false);
Avg_Readings_Sound_Current++;
if (Avg_Readings_Sound_Current > Avg_Readings_Sound_Max) {
Avg_Readings_Sound_Current = Avg_Readings_Sound_Min;
}
labaleText(155, (height / 2) + 5, 77, 35, 5, 1, &FreeSansBold9pt7b, String(Avg_Readings_Sound_Current), PINK, BLACK, CENTER, false);
}
else if (Mode_Setting == AHR_Setting_Active) {
labaleText(155, (height / 2) + 45, 77, 35, 5, 1, &FreeSansBold9pt7b, String(Avg_Readings_HR), BLACK, BLACK, CENTER, false);
Avg_Readings_HR++;
if (Avg_Readings_HR > Avg_Readings_HR_Max) {
Avg_Readings_HR = Avg_Readings_HR_Min;
}
labaleText(155, (height / 2) + 45, 77, 35, 5, 1, &FreeSansBold9pt7b, String(Avg_Readings_HR), PINK, BLACK, CENTER, false);
}
else if (Mode_Setting == TD_Setting_Active) {
labaleText(155, (height / 2) + 85, 77, 35, 5, 1, &FreeSansBold9pt7b, String(Time_Draw_Curve), BLACK, BLACK, CENTER, false);
Time_Draw_Curve++;
if (Time_Draw_Curve > Time_Draw_Curve_Max) {
Time_Draw_Curve = Time_Draw_Curve_Min;
}
labaleText(155, (height / 2) + 85, 77, 35, 5, 1, &FreeSansBold9pt7b, String(Time_Draw_Curve), PINK, BLACK, CENTER, false);
}
}
} Serial.println("Button Incres click.");
} // Click_Button_Incres
// This function will be called when the Button_Select_Ok was pressed 1 time (and no 2. button press followed).
void Click_Button_Select_Ok() {
if (Screen_Window_Current == Setting_Screen) {
if (Mode_Setting == ARS_Setting) {
Mode_Setting = ARS_Setting_Active;
Mode_Incres=Incres;
Mode_Dicreses=Dicreses;
labaleText(155, (height / 2) + 5, 77, 35, 5, 1, &FreeSansBold9pt7b, String(Avg_Readings_Sound_Current), PINK, BLACK, CENTER, false);
}
else if (Mode_Setting == ARS_Setting_Active) {
Mode_Setting = ARS_Setting;
Mode_Incres=No_Incres;
Mode_Dicreses=No_Dicreses;
labaleText(155, (height / 2) + 5, 77, 35, 5, 1, &FreeSansBold9pt7b, String(Avg_Readings_Sound_Current), BLUE, BLACK, CENTER, false);
}
else if (Mode_Setting == AHR_Setting) {
Mode_Setting = AHR_Setting_Active;
Mode_Incres=Incres;
Mode_Dicreses=Dicreses;
labaleText(155, (height / 2) + 45, 77, 35, 5, 1, &FreeSansBold9pt7b, String(Avg_Readings_HR), PINK, BLACK, CENTER, false);
}
else if (Mode_Setting == AHR_Setting_Active) {
Mode_Setting = AHR_Setting;
Mode_Incres=No_Incres;
Mode_Dicreses=No_Dicreses;
labaleText(155, (height / 2) + 45, 77, 35, 5, 1, &FreeSansBold9pt7b, String(Avg_Readings_HR), BLUE, BLACK, CENTER, false);
}
else if (Mode_Setting == TD_Setting) {
Mode_Setting = TD_Setting_Active;
Mode_Incres=Incres;
Mode_Dicreses=Dicreses;
labaleText(155, (height / 2) + 85, 77, 35, 5, 1, &FreeSansBold9pt7b, String(Time_Draw_Curve), PINK, BLACK, CENTER, false);
}
else if (Mode_Setting == TD_Setting_Active) {
Mode_Setting = TD_Setting;
Mode_Incres=No_Incres;
Mode_Dicreses=No_Dicreses;
labaleText(155, (height / 2) + 85, 77, 35, 5, 1, &FreeSansBold9pt7b, String(Time_Draw_Curve), BLUE, BLACK, CENTER, false);
}
}
Serial.println("Button Select Ok click.");
} // Click_Button_Select_Ok
// This function will be called when the Button_Incres was pressed 2 times in a short timeframe.
void Double_Click_Button_Incres() {
if (Screen_Window_Current == Main_Screen) {
if(Record_State==Stop){
Serial.println("Start Record");
Record_State = Record;
Record_Array_NO = 0;
Record_Array_NO_Show = 0;
}
else if(Record_State==Record){
Serial.println("Stop Record");
Record_State = Stop;
Record_Array_NO = 0;
}
}
Serial.println("Button_Incres doubleclick.");
} // Double_Click_Button_Incres
// This function will be called once, when the Button_Incres is double pressed for a Short time.
void Short_Double_Press_Start_Button_Incres() {
Serial.println("Button_Incres double shortPress start");
} // DoubleShort_Press_Start_Button_Incres
// This function will be called once, when the Button_Incres is double pressed for a long time.
void Long_Double_Press_Start_Button_Incres() {
Serial.println("Button_Incres double longPress start");
} // DoubleLong_Press_Start_Button_Incres
// This function will be called once, when the Button_Incres is released after beeing double pressed for a long time.
void Long_Double_Press_Stop_Button_Incres() {
Serial.println("Button_Incres double longPress stop");
} // DoubleLong_Press_Stop_Button_Incres
// This function will be called once, when the Button_Incres is pressed for a Short time.
void Short_Press_Start_Button_Incres() {
Serial.println("Button_Incres shortPress start");
} // Short_Press_Start_Button_Incres
// This function will be called once, when the Button_Incres is pressed for a long time.
void Long_Press_Start_Button_Incres() {
if (Screen_Window_Current == Setting_Screen) {
if(Mode_Incres == Incres&&Mode_Setting == ARS_Setting_Active){
Time_Incres_Dicreses_Set=100;
}
else{
Time_Incres_Dicreses_Set=500;
}
if(millis()>=Time_Incres_Dicreses+Time_Incres_Dicreses_Set){
if (Mode_Incres == No_Incres) {
if (Mode_Setting == ARS_Setting) {
Mode_Setting = TD_Setting;
labaleText(155, (height / 2) + 5, 77, 35, 5, 1, &FreeSansBold9pt7b, String(Avg_Readings_Sound_Current), WHITE, BLACK, CENTER, false);
labaleText(155, (height / 2) + 85, 77, 35, 5, 1, &FreeSansBold9pt7b, String(Time_Draw_Curve), BLUE, BLACK, CENTER, false);
}
else if (Mode_Setting == TD_Setting) {
Mode_Setting = AHR_Setting;
labaleText(155, (height / 2) + 45, 77, 35, 5, 1, &FreeSansBold9pt7b, String(Avg_Readings_HR), BLUE, BLACK, CENTER, false);
labaleText(155, (height / 2) + 85, 77, 35, 5, 1, &FreeSansBold9pt7b, String(Time_Draw_Curve), WHITE, BLACK, CENTER, false);
}
else if (Mode_Setting == AHR_Setting) {
Mode_Setting = ARS_Setting;
labaleText(155, (height / 2) + 45, 77, 35, 5, 1, &FreeSansBold9pt7b, String(Avg_Readings_HR), WHITE, BLACK, CENTER, false);
labaleText(155, (height / 2) + 5, 77, 35, 5, 1, &FreeSansBold9pt7b, String(Avg_Readings_Sound_Current), BLUE, BLACK, CENTER, false);
}
}
else if (Mode_Incres == Incres) {
if (Mode_Setting == ARS_Setting_Active) {
labaleText(155, (height / 2) + 5, 77, 35, 5, 1, &FreeSansBold9pt7b, String(Avg_Readings_Sound_Current), BLACK, BLACK, CENTER, false);
Avg_Readings_Sound_Current++;
if (Avg_Readings_Sound_Current > Avg_Readings_Sound_Max) {
Avg_Readings_Sound_Current = Avg_Readings_Sound_Min;
}
labaleText(155, (height / 2) + 5, 77, 35, 5, 1, &FreeSansBold9pt7b, String(Avg_Readings_Sound_Current), PINK, BLACK, CENTER, false);
}
else if (Mode_Setting == AHR_Setting_Active) {
labaleText(155, (height / 2) + 45, 77, 35, 5, 1, &FreeSansBold9pt7b, String(Avg_Readings_HR), BLACK, BLACK, CENTER, false);
Avg_Readings_HR++;
if (Avg_Readings_HR > Avg_Readings_HR_Max) {
Avg_Readings_HR = Avg_Readings_HR_Min;
}
labaleText(155, (height / 2) + 45, 77, 35, 5, 1, &FreeSansBold9pt7b, String(Avg_Readings_HR), PINK, BLACK, CENTER, false);
}
else if (Mode_Setting == TD_Setting_Active) {
labaleText(155, (height / 2) + 85, 77, 35, 5, 1, &FreeSansBold9pt7b, String(Time_Draw_Curve), BLACK, BLACK, CENTER, false);
Time_Draw_Curve++;
if (Time_Draw_Curve > Time_Draw_Curve_Max) {
Time_Draw_Curve = Time_Draw_Curve_Min;
}
labaleText(155, (height / 2) + 85, 77, 35, 5, 1, &FreeSansBold9pt7b, String(Time_Draw_Curve), PINK, BLACK, CENTER, false);
}
}
Time_Incres_Dicreses=millis();
} Serial.println("Long_Press_Start_Button_Incres.");
}} // Long_Press_Start_Button_Incres
// This function will be called once, when the Button_Incres is released after beeing pressed for a long time.
void Long_Press_Stop_Button_Incres() {
Serial.println("Button_Incres longPress stop");
} // Long_Press_Stop_Button_Incres
// ----- Button Dicreses callback functions
// This function will be called when the Button_Dicreses was pressed 1 time (and no 2. button press followed).
void Click_Button_Dicreses() {
if (Screen_Window_Current == Setting_Screen) {
if (Mode_Dicreses == No_Dicreses) {
if (Mode_Setting == ARS_Setting) {
Mode_Setting = AHR_Setting;
labaleText(155, (height / 2) + 5, 77, 35, 5, 1, &FreeSansBold9pt7b, String(Avg_Readings_Sound_Current), WHITE, BLACK, CENTER, false);
labaleText(155, (height / 2) + 45, 77, 35, 5, 1, &FreeSansBold9pt7b, String(Avg_Readings_HR), BLUE, BLACK, CENTER, false);
}
else if (Mode_Setting == AHR_Setting) {
Mode_Setting = TD_Setting;
labaleText(155, (height / 2) + 45, 77, 35, 5, 1, &FreeSansBold9pt7b, String(Avg_Readings_HR), WHITE, BLACK, CENTER, false);
labaleText(155, (height / 2) + 85, 77, 35, 5, 1, &FreeSansBold9pt7b, String(Time_Draw_Curve), BLUE, BLACK, CENTER, false);
}
else if (Mode_Setting == TD_Setting) {
Mode_Setting = ARS_Setting;
labaleText(155, (height / 2) + 85, 77, 35, 5, 1, &FreeSansBold9pt7b, String(Time_Draw_Curve), WHITE, BLACK, CENTER, false);
labaleText(155, (height / 2) + 5, 77, 35, 5, 1, &FreeSansBold9pt7b, String(Avg_Readings_Sound_Current), BLUE, BLACK, CENTER, false);
}
}
else if (Mode_Dicreses == Dicreses) {
if (Mode_Setting == ARS_Setting_Active) {
labaleText(155, (height / 2) + 5, 77, 35, 5, 1, &FreeSansBold9pt7b, String(Avg_Readings_Sound_Current), BLACK, BLACK, CENTER, false);
Avg_Readings_Sound_Current--;
if (Avg_Readings_Sound_Current < Avg_Readings_Sound_Min) {
Avg_Readings_Sound_Current = Avg_Readings_Sound_Max;
}
labaleText(155, (height / 2) + 5, 77, 35, 5, 1, &FreeSansBold9pt7b, String(Avg_Readings_Sound_Current), PINK, BLACK, CENTER, false);
}
else if (Mode_Setting == AHR_Setting_Active) {
labaleText(155, (height / 2) + 45, 77, 35, 5, 1, &FreeSansBold9pt7b, String(Avg_Readings_HR), BLACK, BLACK, CENTER, false);
Avg_Readings_HR--;
if (Avg_Readings_HR < Avg_Readings_HR_Min) {
Avg_Readings_HR = Avg_Readings_HR_Max;
}
labaleText(155, (height / 2) + 45, 77, 35, 5, 1, &FreeSansBold9pt7b, String(Avg_Readings_HR), PINK, BLACK, CENTER, false);
}
else if (Mode_Setting == TD_Setting_Active) {
labaleText(155, (height / 2) + 85, 77, 35, 5, 1, &FreeSansBold9pt7b, String(Time_Draw_Curve), BLACK, BLACK, CENTER, false);
Time_Draw_Curve--;
if (Time_Draw_Curve < Time_Draw_Curve_Min) {
Time_Draw_Curve = Time_Draw_Curve_Max;
}
labaleText(155, (height / 2) + 85, 77, 35, 5, 1, &FreeSansBold9pt7b, String(Time_Draw_Curve), PINK, BLACK, CENTER, false);
}
}
}
Serial.println("Button Dicreses click.");
} // Click_Button_Dicreses
// This function will be called when the Button_Dicreses was pressed 2 times in a short timeframe.
void Double_Click_Button_Dicreses() {
if (Screen_Window_Current == Main_Screen) {
if(Record_State==Stop){
if(Record_Array_NO_Show>=10){
Serial.println("Start Show");
Record_State = Show;
Record_Array_NO = 0;}
else{
}
}
else if(Record_State==Show){
Serial.println("Stop Show");
Record_State = Stop;
Record_Array_NO = 0;
}
}
Serial.println("Button Dicreses doubleclick.");
} // Double_Click_Button_Dicreses
// This function will be called once, when the Button_Dicreses is double pressed for a Short time.
void Short_Double_Press_Start_Button_Dicreses() {
Serial.println("Button Dicreses double shortPress start");
} // DoubleShort_Press_Start_Button_Dicreses
// This function will be called once, when the Button_Dicreses is double pressed for a long time.
void Long_Double_Press_Start_Button_Dicreses() {
Serial.println("Button Dicreses double longPress start");
} // DoubleLong_Press_Start_Button_Dicreses
// This function will be called once, when the Button_Dicreses is released after beeing double pressed for a long time.
void Long_Double_Press_Stop_Button_Dicreses() {
Serial.println("Button Dicreses double longPress stop");
} // DoubleLong_Press_Stop_Button_Dicreses
// This function will be called once, when the Button_Dicreses is pressed for a Short time.
void Short_Press_Start_Button_Dicreses() {
Serial.println("Button Dicreses shortPress start");
} // Short_Press_Start_Button_Dicreses
// This function will be called once, when the Button_Dicreses is pressed for a long time.
void Long_Press_Start_Button_Dicreses() {
if (Screen_Window_Current == Setting_Screen) {
if(Mode_Dicreses == Dicreses&&Mode_Setting == ARS_Setting_Active){
Time_Incres_Dicreses_Set=100;
}
else{
Time_Incres_Dicreses_Set=500;
}
if(millis()>=Time_Incres_Dicreses+Time_Incres_Dicreses_Set){
if (Mode_Dicreses == No_Dicreses) {
if (Mode_Setting == ARS_Setting) {
Mode_Setting = AHR_Setting;
labaleText(155, (height / 2) + 5, 77, 35, 5, 1, &FreeSansBold9pt7b, String(Avg_Readings_Sound_Current), WHITE, BLACK, CENTER, false);
labaleText(155, (height / 2) + 45, 77, 35, 5, 1, &FreeSansBold9pt7b, String(Avg_Readings_HR), BLUE, BLACK, CENTER, false);
}
else if (Mode_Setting == AHR_Setting) {
Mode_Setting = TD_Setting;
labaleText(155, (height / 2) + 45, 77, 35, 5, 1, &FreeSansBold9pt7b, String(Avg_Readings_HR), WHITE, BLACK, CENTER, false);
labaleText(155, (height / 2) + 85, 77, 35, 5, 1, &FreeSansBold9pt7b, String(Time_Draw_Curve), BLUE, BLACK, CENTER, false);
}
else if (Mode_Setting == TD_Setting) {
Mode_Setting = ARS_Setting;
labaleText(155, (height / 2) + 85, 77, 35, 5, 1, &FreeSansBold9pt7b, String(Time_Draw_Curve), WHITE, BLACK, CENTER, false);
labaleText(155, (height / 2) + 5, 77, 35, 5, 1, &FreeSansBold9pt7b, String(Avg_Readings_Sound_Current), BLUE, BLACK, CENTER, false);
}
}
else if (Mode_Dicreses == Dicreses) {
if (Mode_Setting == ARS_Setting_Active) {
labaleText(155, (height / 2) + 5, 77, 35, 5, 1, &FreeSansBold9pt7b, String(Avg_Readings_Sound_Current), BLACK, BLACK, CENTER, false);
Avg_Readings_Sound_Current--;
if (Avg_Readings_Sound_Current < Avg_Readings_Sound_Min) {
Avg_Readings_Sound_Current = Avg_Readings_Sound_Max;
}
labaleText(155, (height / 2) + 5, 77, 35, 5, 1, &FreeSansBold9pt7b, String(Avg_Readings_Sound_Current), PINK, BLACK, CENTER, false);
}
else if (Mode_Setting == AHR_Setting_Active) {
labaleText(155, (height / 2) + 45, 77, 35, 5, 1, &FreeSansBold9pt7b, String(Avg_Readings_HR), BLACK, BLACK, CENTER, false);
Avg_Readings_HR--;
if (Avg_Readings_HR < Avg_Readings_HR_Min) {
Avg_Readings_HR = Avg_Readings_HR_Max;
}
labaleText(155, (height / 2) + 45, 77, 35, 5, 1, &FreeSansBold9pt7b, String(Avg_Readings_HR), PINK, BLACK, CENTER, false);
}
else if (Mode_Setting == TD_Setting_Active) {
labaleText(155, (height / 2) + 85, 77, 35, 5, 1, &FreeSansBold9pt7b, String(Time_Draw_Curve), BLACK, BLACK, CENTER, false);
Time_Draw_Curve--;
if (Time_Draw_Curve < Time_Draw_Curve_Min) {
Time_Draw_Curve = Time_Draw_Curve_Max;
}
labaleText(155, (height / 2) + 85, 77, 35, 5, 1, &FreeSansBold9pt7b, String(Time_Draw_Curve), PINK, BLACK, CENTER, false);
}
}
Time_Incres_Dicreses=millis();
}
}
Serial.println("Button Dicreses longPress start");
} // Long_Press_Start_Button_Dicreses
// This function will be called once, when the Button_Dicreses is released after beeing pressed for a long time.
void Long_Press_Stop_Button_Dicreses() {
Serial.println("Button Dicreses longPress stop");
} // Long_Press_Stop_Button_Dicreses
// ----- Button Select Ok callback functions
// This function will be called when the Button_Select_Ok was pressed 2 times in a short timeframe.
void Double_Click_Button_Select_Ok() {
Serial.println("Button Select Ok doubleclick.");
} // Double_Click_Button_Select_Ok
// This function will be called once, when the Button_Select_Ok is double pressed for a Short time.
void Short_Double_Press_Start_Button_Select_Ok() {
Serial.println("Button Select Ok double shortPress start");
} // DoubleShort_Press_Start_Button_Select_Ok
// This function will be called once, when the Button_Select_Ok is double pressed for a long time.
void Long_Double_Press_Start_Button_Select_Ok() {
Serial.println("Button Select Ok double longPress start");
} // DoubleLong_Press_Start_Button_Select_Ok
// This function will be called once, when the Button_Select_Ok is released after beeing double pressed for a long time.
void Long_Double_Press_Stop_Button_Select_Ok() {
Serial.println("Button Select Ok double longPress stop");
} // DoubleLong_Press_Stop_Button_Select_Ok
// This function will be called once, when the Button_Select_Ok is pressed for a Short time.
void Short_Press_Start_Button_Select_Ok() {
Serial.println("Button Select Ok shortPress start");
} // Short_Press_Start_Button_Select_Ok
// This function will be called once, when the Button_Select_Ok is pressed for a long time.
void Long_Press_Start_Button_Select_Ok() {
if (Screen_Window_Current == Main_Screen && State_Button_Select_Ok == Long_Press_Button_Select_Ok_Main_Screen) {
State_Button_Select_Ok = Long_Press_Button_Select_Ok_Setting_Screen;
Serial.println("Button Select Ok longPress start in Main_Screen ");
}
else if (Screen_Window_Current == Setting_Screen && State_Button_Select_Ok == Long_Press_Button_Select_Ok_Setting_Screen) {
State_Button_Select_Ok = Long_Press_Button_Select_Ok_Main_Screen;
Serial.println("Button Select Ok longPress start in Setting_Screen ");
}
} // Long_Press_Start_Button_Select_Ok
// This function will be called once, when the Button_Select_Ok is released after beeing pressed for a long time.
void Long_Press_Stop_Button_Select_Ok() {
if (Screen_Window_Current == Main_Screen && State_Button_Select_Ok == Long_Press_Button_Select_Ok_Setting_Screen) {
Screen_Window_Current = Setting_Screen;
Serial.println("Draw Setting_Screen ");
Draw_Seting_Screen();
}
else if (Screen_Window_Current == Setting_Screen && State_Button_Select_Ok == Long_Press_Button_Select_Ok_Main_Screen) {
Screen_Window_Current = Main_Screen;
Serial.println("Draw Main_Screen ");
Draw_Main_Screen();
}
} // Long_Press_Stop_Button_Select_Ok
void Long_Double_Press_Start_Button_Power_On_Off() {
Serial.println("Long_Double_Press_Start_Button_Power_On_Off");
tft.fillScreen(BLACK);
Draw_End_Screen();
} // DoubleLong_Press_Start_Button_Select_Ok
void Long_Double_Press_Stop_Button_Power_On_Off() {
Serial.println("Long_Double_Press_Stop_Button_Power_On_Off");
Draw_End_Screen();
} // DoubleLong_Press_Start_Button_Select_Ok
// End
void Read_HR() {
if (average_Sound > lADC0MaxValue2) {
lADC0MaxValue2 = average_Sound;
}
if (average_Sound < lADC0MaxValue2) {
lADC0MinValue2 = average_Sound;
}
if (millis() >= Time_calabration + 10000) {
Time_calabration = millis();
lADC0MaxValue = lADC0MaxValue2;
lADC0MinValue = lADC0MinValue2;
lADC0MaxValue2 = 0;
lADC0MinValue2 = ADC_MAX_VAL;
// ucScreenIndex = 1;
iADC0SpanValue = (lADC0MaxValue - lADC0MinValue);
// fScalingFactor = (float)((float)(PULSE_WINDOW_HEIGHT) / (float)ADC_MAX_VAL);
iPulseTriggerLevelHigh = ((int)(((float)(lADC0MaxValue - lADC0MinValue)) * 0.10)) + lADC0MinValue;
iPulseTriggerLevelLow = ((int)(((float)(lADC0MaxValue - lADC0MinValue)) * 0.30)) + lADC0MinValue;
Serial.println("-------------------------------------");
Serial.println((String)"lADC0MaxValue : " + lADC0MaxValue);
Serial.println((String)"lADC0MinValue : " + lADC0MinValue);
Serial.println("-------------------------------------");
Serial.println((String)"iADC0SpanValue : " + iADC0SpanValue);
// Serial.println((String)"fScalingFactor : " + fScalingFactor);
Serial.println("--------------------------------------");
Serial.println((String)"iPulseTriggerLevelHigh : " + iPulseTriggerLevelHigh);
Serial.println((String)"iPulseTriggerLevelLow : " + iPulseTriggerLevelLow);
}
iPhototransistorSignalIR = average_Sound; // Read raw level from analogue input
if (iADC0SpanValue >= volume_New*3) {
if (iPhototransistorSignalIR <= iPulseTriggerLevelHigh)
{
if ((!bRisingEdgeDetected) && (!bFallingEdgeDetected))
{
// Serial.println(" RISING" );
bRisingEdgeDetected = true;
ulPulseCurrentTime = millis();
}
if ((bRisingEdgeDetected) && (bFallingEdgeDetected))
{
// Serial.println(" RISING 2" );
bFallingEdgeDetected = false;
bRisingEdgeDetected = false;
ulPulseOldTime = ulPulseCurrentTime;
ulPulseCurrentTime = millis();
if (ulPulseCurrentTime >= ulPulseOldTime)
lBPMArray[iBPMArrayIndex++] = (long) ((float)60000.0 / (float)(ulPulseCurrentTime - ulPulseOldTime));
else
lBPMArray[iBPMArrayIndex++] = (long) ((float)60000.0 / (float)((MAX_UL - ulPulseOldTime) + ulPulseCurrentTime));
//lBPMArray[iBPMArrayIndex++] = ((MAX_UL - ulPulseOldTime) + ulPulseCurrentTime);
if (iBPMArrayIndex > (Avg_Readings_HR - 1)) // Handle wrapping of BPM arrary
iBPMArrayIndex = 0;
lBPMTemp = 0;
for (iBPMAverageIndex = 0; iBPMAverageIndex < Avg_Readings_HR; iBPMAverageIndex++)
lBPMTemp += lBPMArray[iBPMAverageIndex];
iBPM = (int)(((float)lBPMTemp) / ((float)Avg_Readings_HR)); // Recalculate the rolling average over 3 samples
if ((iBPM != iBPMOld) && (iBPM < UPPER_PULSE_RATE))
{
if (millis() >= Time_Print_HR + 2000) {
if (Screen_Window_Current == Main_Screen) {
labaleText(85, (height / 2) + 5, 70, 35, 5, 1, &FreeSansBold9pt7b, String(iBPMOld), BLACK, BLACK, CENTER, false);
labaleText(85, (height / 2) + 5, 70, 35, 5, 1, &FreeSansBold9pt7b, String(iBPM), CYAN, BLACK, CENTER, false);
// printMsg(47, (height / 2) + 23, 1, &FreeSans12pt7b, String(iBPMOld), BLACK, BLACK);
// printMsg(47, (height / 2) + 23, 1, &FreeSans12pt7b, String(iBPM), BLACK, CYAN);
}
Time_Print_HR = millis();
iBPMOld = iBPM;
}
}
}
}
if (iPhototransistorSignalIR >= iPulseTriggerLevelLow)
{
if ((bRisingEdgeDetected) && (!bFallingEdgeDetected))
bFallingEdgeDetected = true;
// Serial.println(" RISING 3" );
}
}
else {
if (millis() >= Time_Print_HR + 3000) {
iBPMArrayIndex = 0;
iBPM = 0;
if (Screen_Window_Current == Main_Screen) {
labaleText(85, (height / 2) + 5, 70, 35, 5, 1, &FreeSansBold9pt7b, String(iBPMOld), BLACK, BLACK, CENTER, false);
labaleText(85, (height / 2) + 5, 70, 35, 5, 1, &FreeSansBold9pt7b, String(iBPM), CYAN, BLACK, CENTER, false);
// printMsg(47, (height / 2) + 23, 1, &FreeSans12pt7b, String(iBPMOld), BLACK, BLACK);
// printMsg(47, (height / 2) + 23, 1, &FreeSans12pt7b, String(iBPM), BLACK, CYAN);
}
Time_Print_HR = millis();
iBPMOld = iBPM;
}
}
}
void Read_Sig() {
if (millis() >= Time_Read_Sig + 20)
{
Time_Read_Sig = millis();
total_Sig = total_Sig - readings_Sig[readIndex_Sig];
readings_Sig[readIndex_Sig] = analogRead(Pin_Sig);
total_Sig = total_Sig + readings_Sig[readIndex_Sig];
readIndex_Sig = readIndex_Sig + 1;
if (readIndex_Sig >= numreadings_Sig) {
readIndex_Sig = 0;
}
}
average_Sig = int(total_Sig / numreadings_Sig);
average_Sig = constrain(average_Sig, 0, 4095);
Signal_New = map(average_Sig, 0, 4095, 0, 100);
if (millis() >= Time_Print_Sig + 10) {
if (Signal_New != Signal_Old) {
if (Screen_Window_Current == Main_Screen) {
labaleText(85, (height / 2) + 85, 70, 35, 5, 1, &FreeSansBold9pt7b, String(Signal_Old), BLACK, BLACK, CENTER, false);
labaleText(85, (height / 2) + 85, 70, 35, 5, 1, &FreeSansBold9pt7b, String(Signal_New), CYAN, BLACK, CENTER, false);
// printMsg(47, (height / 2) + 80, 1, &FreeSans12pt7b, String(Signal_Old), BLACK, BLACK);
// printMsg(47, (height / 2) + 80, 1, &FreeSans12pt7b, String(Signal_New), BLACK, GREEN);
}
Signal_Old = Signal_New;
Time_Print_Sig = millis();
}
}
}
void Read_Sound() {
if (millis() >= time_read_mic + 1)
{
time_read_mic = millis();
total_Sound = total_Sound - readings_Sound[readIndex_Sound];
readings_Sound[readIndex_Sound] = analogRead(Mic_Pin1);
total_Sound = total_Sound + readings_Sound[readIndex_Sound];
readIndex_Sound = readIndex_Sound + 1;
if (readIndex_Sound >= Avg_Readings_Sound_Current) {
readIndex_Sound = 0;
}
}
average_Sound = total_Sound / Avg_Readings_Sound_Current;
// amplifiedSignal = average_Sound * 2;
// filteredSignal = filterOneHighpass.input(filterOneLowpass.input(amplifiedSignal));
// average_Sound = int(filteredSignal);
// average_Sound += 500;
// average_Sound *= 2;
if (millis() >= Time_Draw_Sound + Time_Draw_Curve) {
Time_Draw_Sound = millis();
if (Record_State == Stop) {
Draw_Curve_signal_Sound(5, 30, width - 10, (height / 2) , BLACK, "Play PCG Current", average_Sound, GREEN, Sound_VALUE_MAX, 0);
}
if (Record_State == Record) {
Draw_Curve_signal_Sound(5, 30, width - 10, (height / 2) , BLACK, "Start PCG Record", average_Sound, ORANGE, Sound_VALUE_MAX, 0);
Record_Array[Record_Array_NO] = average_Sound;
Serial.println(" write Record_Array[ " + String(Record_Array_NO) + " ] = " + String(average_Sound));
Serial.println(" Read Record_Array[ " + String(Record_Array_NO) + " ] = " + String(Record_Array[Record_Array_NO]));
Record_Array_NO++;
Record_Array_NO_Show++;
if (Record_Array_NO > 10000) {
Record_State = Stop;
Record_Array_NO_Show = 0;
Record_Array_NO = 0;
}
}
else if (Record_State == Show) {
Draw_Curve_signal_Sound(5, 30, width - 10, (height / 2) , BLACK, "Play PCG Record ", Record_Array[Record_Array_NO], CYAN, Sound_VALUE_MAX, 0);
Serial.println(" Read Record_Array[ " + String(Record_Array_NO) + " ] = " + String(Record_Array[Record_Array_NO]));
Record_Array_NO++;
if (Record_Array_NO >= Record_Array_NO_Show) {
Record_Array_NO = 0;
}
}
//---------Colection BPM-------------//
}
}
void Read_Vol() {
if (millis() >= Time_Read_Vol + 20)
{
Time_Read_Vol = millis();
total_Vol = total_Vol - readings_Vol[readIndex_Vol];
readings_Vol[readIndex_Vol] = analogRead(Pin_Vol);
total_Vol = total_Vol + readings_Vol[readIndex_Vol];
readIndex_Vol = readIndex_Vol + 1;
if (readIndex_Vol >= numreadings_Vol) {
readIndex_Vol = 0;
}
}
average_Vol = int(total_Vol / numreadings_Vol);
average_Vol = constrain(average_Vol, 0, 4095);
volume_New = map(average_Vol, 0, 4095, 0, 100);
if (millis() >= Time_Print_Vol + 10) {
if (volume_New != volume_Old) {
if (Screen_Window_Current == Main_Screen) {
labaleText(85, (height / 2) + 45, 70, 35, 5, 1, &FreeSansBold9pt7b, String(volume_Old), BLACK, BLACK, CENTER, false);
labaleText(85, (height / 2) + 45, 70, 35, 5, 1, &FreeSansBold9pt7b, String(volume_New), CYAN, BLACK, CENTER, false);
}
// printMsg(47, (height / 2) + 50, 1, &FreeSans12pt7b, String(volume_Old), BLACK, BLACK);
// printMsg(47, (height / 2) + 50, 1, &FreeSans12pt7b, String(volume_New), BLACK, LIGHTGREY);
volume_Old = volume_New;
Time_Print_Vol = millis();
}
}
}
int X_Begin_signal_Sound = 1;
int Y_Begin_signal_Sound;
int Begin_draw_gread_signal_Sound;
uint32_t Sound_MAX_DRAW_CEL = 0, Sound_MIN_DRAW_CEL = 4200;
int16_t Text_signal_color_Befor = 0;
void Begin_signal_Sound() {
Begin_draw_gread_signal_Sound = 0;
}
void Draw_Curve_signal_Sound(int x1, int y1, int x2, int y2, int16_t BackColor,
String name, long Sound_VALUE, int16_t Text_signal_color
, uint32_t VALUE_MAX, uint32_t VALUE_MIN) {
Sound_VALUE = constrain(Sound_VALUE, VALUE_MIN, VALUE_MAX);
if (Sound_VALUE > Sound_MAX_DRAW_CEL) {
Sound_MAX_DRAW_CEL = Sound_VALUE;
}
if (Sound_VALUE < Sound_MIN_DRAW_CEL) {
Sound_MIN_DRAW_CEL = Sound_VALUE;
}
if (Text_signal_color != Text_signal_color_Befor) {
tft.fillRoundRect(5, y1 - 25, width - 10, 25, 10, BackColor) ;
labaleText(5, y1 - 25, width - 10, 25, 10, 1, &FreeSansBold9pt7b, name, Text_signal_color, BackColor, CENTER,false);
// printMsg(x1, y1-5, 1, &FreeSansBold9pt7b, name, BackColor, Text_signal_color);
Text_signal_color_Befor = Text_signal_color;
}
if ( Begin_draw_gread_signal_Sound == 0) {
fillRoundRect(x1, y1, x2, y2, 2, BackColor);
drawRoundRect(x1, y1, x1, y2,4, LIGHTGREY);
drawRoundRect(x2+3, y1, x2+3, y2,4, LIGHTGREY);
tft.drawLine(x1+10, y2, x2-10, y2, LIGHTGREY);
Begin_draw_gread_signal_Sound++;
X_Begin_signal_Sound = x1 + 2;
Y_Begin_signal_Sound = (((y2 - y1) / 2) + y1) - 2;
}
Sound_VALUE = constrain(Sound_VALUE, Sound_VALUE_MIN, Sound_VALUE_MAX);
int Signal_Convert_Peak = map(Sound_VALUE, Sound_VALUE_MIN, Sound_VALUE_MAX, -3, 3);
int yN2 = map(Sound_VALUE, Sound_VALUE_MIN , Sound_VALUE_MAX, y2 -10, y1 + 10);//5,3
if (yN2 < 0) {
yN2 = y2 - 10;//5
}
if ( X_Begin_signal_Sound < x2)
{
tft.drawLine( X_Begin_signal_Sound + 1, y1, X_Begin_signal_Sound + 1, y2 - 1, BackColor);
tft.drawLine( X_Begin_signal_Sound + 2, y1, X_Begin_signal_Sound + 2, y2 - 1, BackColor);
tft.drawLine( X_Begin_signal_Sound, Y_Begin_signal_Sound, X_Begin_signal_Sound + 2, yN2, Text_signal_color);
Y_Begin_signal_Sound = yN2;
X_Begin_signal_Sound += 2;
}
else if ( X_Begin_signal_Sound >= x2) {
X_Begin_signal_Sound = x1 + 2;
tft.drawLine( X_Begin_signal_Sound + 2, y1, X_Begin_signal_Sound + 2, y2 - 1, BackColor);
tft.drawLine( X_Begin_signal_Sound + 1, y1, X_Begin_signal_Sound + 1, y2 - 1, BackColor);
tft.drawLine( X_Begin_signal_Sound, y1, X_Begin_signal_Sound, y2 - 1, BackColor);
tft.drawLine( X_Begin_signal_Sound, Y_Begin_signal_Sound, X_Begin_signal_Sound + 2, yN2, Text_signal_color);
Y_Begin_signal_Sound = yN2;
X_Begin_signal_Sound += 2;
if (Sound_MAX_DRAW_CEL - Sound_MIN_DRAW_CEL > 100) {
Serial.println((String)"Sound_MAX_DRAW_CEL : "+Sound_MAX_DRAW_CEL);
Serial.println((String)"Sound_MIN_DRAW_CEL : "+Sound_MIN_DRAW_CEL);
Sound_VALUE_MAX = Sound_MAX_DRAW_CEL + 300;
Sound_VALUE_MIN = Sound_MIN_DRAW_CEL - 300;
Sound_MAX_DRAW_CEL = 0;
Sound_MIN_DRAW_CEL = 10000;
}
else {
Serial.println((String)"Sound_MIN_DRAW_CEL : "+Sound_MIN_DRAW_CEL);
Serial.println((String)"Sound_MAX_DRAW_CEL: "+Sound_MAX_DRAW_CEL);
Sound_MAX_DRAW_CEL = 0;
Sound_MIN_DRAW_CEL = 10000;
Sound_VALUE_MAX = Sound_MIN_DRAW_CEL;
Sound_VALUE_MIN = Sound_MAX_DRAW_CEL;
// Serial.println((String)"Sound_VALUE_MAX off : "+Sound_VALUE_MAX);
// Serial.println((String)"Sound_VALUE_MIN off: "+Sound_VALUE_MIN);
}
}
}
void Test_Serial() {
if (Serial.available()) {
char read = Serial.read();
if (read == 'R' || read == 'r') {
Serial.println("Start Read Record");
Record_State = Record;
Record_Array_NO = 0;
Record_Array_NO_Show = 0;
}
else if (read == 'W' || read == 'w') {
Serial.println("Start Show Record");
Record_State = Show;
Record_Array_NO = 0;
}
else if (read == 'S' || read == 's') {
Serial.println("Stop Record");
Record_State = Stop;
Record_Array_NO = 0;
// Record_Array_NO_Show=0;
}
else if (read == 'M') {
Sound_VALUE_MAX = Serial.parseInt();
Serial.println("Sound_VALUE_MAX : " + String(Sound_VALUE_MAX));
}
else if (read == 'm') {
Sound_VALUE_MIN = Serial.parseInt();
Serial.println("Sound_VALUE_MIN : " + String(Sound_VALUE_MIN));
}
else if (read == 'c') {
Serial.println("Reset button : ");
// tft.readGRAM(0, (height / 2), RGB_Array, width , 100);
// tft.fillRect(0, (height / 2), width , 100, GREEN);
// delay(4000);
// tft.setAddrWindow(0, (height / 2), width - 1, 100);
// tft.pushColors(RGB_Array, 32000, 1);
}
else if (read == 'o') {
Serial.println("Reset millis : "+String(millis()));
// tft.readGRAM(0, (height / 2), RGB_Array, width , 100);
// tft.fillRect(0, (height / 2), width , 100, GREEN);
// delay(4000);
// tft.setAddrWindow(0, (height / 2), width - 1, 100);
// tft.pushColors(RGB_Array, 32000, 1);
}
}
}
void fillRoundRect(int16_t x, int16_t y, int16_t w,
int16_t h, int16_t r,int16_t color) {
tft.fillRoundRect( x, y, (w - x) + 1, (h - y) + 1, r, color) ;
}
void drawRoundRect(int16_t x, int16_t y, int16_t w,
int16_t h, int16_t r,int16_t color) {
tft.drawRoundRect( x, y, (w - x) + 1, (h - y) + 1, r, color) ;
}
void printMsg(int x, int y, int sz, const GFXfont *f,
String msg, int16_t colorBack, int16_t colorText) {
tft.setFont(f);
tft.setCursor(x, y);
tft.setTextColor(colorText, colorBack);
tft.setTextSize(sz);
tft.print(msg);
}
void labaleText(int16_t x, int16_t y, int16_t w, int16_t h, int16_t cr , int16_t sz, const GFXfont *f,
String msg, int16_t colorLabel, int16_t colorText, byte c,bool State)
{
uint16_t Width_Text, Height_Text;
int16_t x1, y1;
tft.setFont(f);
if(State){
tft.setTextColor(colorText);
}
else{
tft.setTextColor(colorLabel);
}
tft.setTextSize(sz);
tft.getTextBounds(msg, x, y, &x1, &y1, &Width_Text, &Height_Text);// error 1
int xtext =x+int((w- Width_Text)/2) ;
int ytext2 = y+Height_Text+((h- Height_Text)/2);
int ytext = int((y+h)-(float((h- Height_Text)/2))-(Height_Text/8));
// int xtext = (((w - (10 + Width_Text)) / 2) + 5 + x) ;
// int ytext = (y + h) - (((h - (10 + Height_Text)) / 2) + 5);
if(State){
tft.fillRoundRect(x, y, w, h, cr, colorLabel) ;
tft.drawRoundRect(x, y, w, h, cr, colorText) ;
}
else {
if(colorText!=colorLabel){
tft.drawRoundRect(x, y, w, h, cr, colorLabel) ;}
}
Serial.println((String)"x1:"+x1+"\ty1:"+y1+"\tWidth_Text:"+Width_Text+"\tHeight_Text:"+Height_Text);
Serial.println((String)"xtext : "+xtext+"\tytext : "+ytext);
Serial.println((String)"xtext 2 : "+xtext+"\tytext 2 : "+ytext2);
if (c == RIGHT) {
tft.setCursor(x + 2, ytext);
}
else if (c == LIFT) {
tft.setCursor((x + w) - (Width_Text +4 ), ytext);
}
else if (c == CENTER)
{
tft.setCursor(xtext, ytext);
}
tft.print(msg);
}
uint16_t updateTextColor() {
// تحديد قيم RGB للون
int red = map(colorIndex, 0, 255, 0, 255);
int green = map(colorIndex, 0, 255, 255, 0);
int blue = map(colorIndex, 0, 255, 0, 255);
// تعيين لون النص
// tft.setTextColor(tft.color565(red, green, blue));
// printMsg(50, 160,1,&FreeSans12pt7b,"Smart Medical",BLACK,tft.color565(red, green, blue));
// زيادة فهرس الألوان باتجاه المزيد من التدرجات
colorIndex++;
if (colorIndex > 255) {
colorIndex = 0;
}
return tft.color565(red, green, blue);
}