#define DECODE_NEC
#define USE_NO_SEND_PWM
#define xres 480
#define yres 320
#define xc 20
#define padding 28
#define phi 200
#include <UTFTGLUE.h> //use GLUE class and constructor
#include <IRremote.hpp>
#include <ATmega_TimerInterrupt.h>
#include <ISR_Timer.h>
UTFTGLUE myGLCD(0,A2,A1,A3,A4,A0); //all dummy args
float y=0, xscale=1, yscale=0.75 /* ,x=0 */ ;
int dt=0,x=1,period=10; //ATTENTION: The screen is 0-indexed!
const int reset= (floor(xres*xscale)),offset=yres/2;
//float z=x-phi ;
volatile int state = HIGH;
ATmega_TimerInterrupt timer;
GFXcanvas1 canvas(480, yres-padding);
short Curve[480];
/*float rndFloat(float min, float max) {
return ((float)rand() / RAND_MAX) * (max - min) + min;
} */
float sinrhythm(float x){
if(x<xres*2/3){
return offset-0.25/yscale*(20*exp(-pow(((x-xc)/16),2))-30*exp(-pow(((x-xc-75)/5),2))+200*exp(-pow(((x-xc-93)/11.2),2))+60*exp(-pow(((x-xc-193)/29),2))+10*exp(-pow(((x-xc-260)/10),2))+-50*exp(-pow(((x-xc-111)/5),2)));
}
else{
return offset-0.25/yscale*(20*exp(-pow(((x-xc-xres*3/4)/16),2))-30*exp(-pow(((x-xc-xres*3/4-75)/5),2))+200*exp(-pow(((x-xc-xres*3/4-93)/11.2),2))+60*exp(-pow(((x-xc-xres*3/4-193)/29),2))+10*exp(-pow(((x-xc-xres*3/4-260)/10),2))+-50*exp(-pow(((x-xc-xres*3/4-111)/5),2)));
}
}
float vtachycard(float x){
float y=int(floor(x))%97+x-floor(x)+5;
return offset-50/yscale*(0.75*exp(-pow(((y-38)/14),2))+1/6*exp(-pow(((y-25)/5),2))-1.2*exp(-pow(((y-80)/10),2))+1/2*exp(-pow(((y-79)/2.5),2))+0.5);
}
float vfib(float x, float d){
return offset-50/yscale*(0.5*sin(x/5)+0.1*sin(d*x/10)+0.01*d*sin(x));
}
float asys(float x, float d){
return offset-50/yscale*(0.05*sin(d + x/100)+ d/100*sin(x/d/10));
}
float(*fnptr)(float,float){sinrhythm};
//void(*ptr)(){drawCurve};
void listen_IR(){
if (IrReceiver.decode()) {
//Serial.println(IrReceiver.decodedIRData.command);
if (IrReceiver.decodedIRData.command == 0x45) { //button1
// do something
fnptr=sinrhythm;
for(int i=1;i<481;i++){
Curve[i]=fnptr(float(i),3);
}
Serial.println("PRESSED 1");
} else if (IrReceiver.decodedIRData.command == 0x46) { //button2
// do something else
fnptr=vtachycard;
for(int i=1;i<481;i++){
Curve[i]=fnptr(float(i),3);
}
//Serial.println("PRESSED 2");
}else if (IrReceiver.decodedIRData.command == 0x47) { //button3
// do something else
fnptr=vfib;
for(int i=1;i<481;i++){
Curve[i]=fnptr(float(i),3);
}
//Serial.println("PRESSED 3");
}else if (IrReceiver.decodedIRData.command == 0x44) { //button4
// do something else
fnptr=asys;
for(int i=1;i<481;i++){
Curve[i]=fnptr(float(i),3);
}
//Serial.println("PRESSED 4");
}/*else if (IrReceiver.decodedIRData.command == 64) { //button5
Serial.print("SUCCESS");
}*/
IrReceiver.resume();
}
}
void charge(int i){
myGLCD.setColor(255,255,0);
myGLCD.fillRect((25-i)*15+55, yres-50, (25-i)*15+65, yres-25);
}
void discharge(float x, int span){
int i;
myGLCD.setColor(0,0,0);
myGLCD.fillRect(50, yres-55, xres-50, yres-20);
myGLCD.setColor(255,255,255);
for(i=0;i<span;i++){
myGLCD.drawPixel(((x+i)/xscale),-50*(sinrhythm(i))/yscale+offset);
}
}
void fillCanvas(){
for(int i=0;i<canvas.width();i++)
{canvas.drawPixel(i, Curve[i], 1);}
}
void drawCurve(){
//if((x==reset) || x ==0){y=rndFloat(1,5);};
myGLCD.setColor(0,0,0);
myGLCD.drawBitmap(xres-x%xres, 14, canvas.getBuffer(),canvas.width(), canvas.height()+14, 1);
myGLCD.setColor(255,255,255);
myGLCD.drawBitmap(xres-(x+1)%xres, 14, canvas.getBuffer(),canvas.width(), canvas.height()+14, 1);
x=(x+1);
if(x>=reset){x=1;}
}
void setup()
{
pinMode(12,INPUT_PULLUP); //input for charge button
pinMode(10,INPUT_PULLUP); //input for discharge button
Serial.begin(9600);
IrReceiver.begin(A5, ENABLE_LED_FEEDBACK);
randomSeed(analogRead(0));
// Setup the LCD
myGLCD.InitLCD();
myGLCD.setFont(SmallFont);
// Clear the screen and draw the frame
myGLCD.clrScr();
myGLCD.setColor(255, 0, 0);
myGLCD.fillRect(0, 0, 479, 13);
myGLCD.setColor(64, 64, 64);
myGLCD.fillRect(0, 306, 479, 319);
myGLCD.setColor(255, 255, 255);
myGLCD.setBackColor(255, 0, 0);
myGLCD.print("Electrocardiogram simulator", CENTER, 1);
myGLCD.setBackColor(64, 64, 64);
myGLCD.setColor(255,255,0);
myGLCD.print("Pura Solutions", CENTER, 307);
for(int i=1;i<481;i++){
Curve[i]=fnptr(float(i-1),3);
}
timer.attachInterruptInterval(period, drawCurve);
}
int charged=0,falling=0,i=0;
void loop(){
//Hakim Code
listen_IR();
if(!digitalRead(12) && i==0){
i=25;
myGLCD.setColor(255,255,0);
myGLCD.drawRect(50, yres-55, xres-50, yres-20);
charged=1;
}
/*if (IrReceiver.decode()){
if (IrReceiver.decodedIRData.command == 64) {
i=25;
Serial.print("SUCCESS");
}
IrReceiver.resume();
}*/
if(i>0 && millis()-dt>100){
//Serial.print("SUCCESS");
charge(i);
i=i-1;
dt=millis();
}
if(!digitalRead(10)){falling=1;}
if(falling && i==0 && charged){
discharge(x,50);
charged=0;
falling=0;
x=x+50;
}
}