#include <SPI.h>
#include "Adafruit_MAX31855.h"
#include "HX8357_t3n.h"
//#include <FlickerFreePrint.h>// library to draw w/o flicker
#include <math.h>
#include <stdio.h>
#include <elapsedMillis.h>
#include <ButtonEvents.h>
#include <ADC.h>
#include <Encoder.h>
#include <PID_v1.h>
// Default connection is using software SPI, but comment and uncomment one of
// the two examples below to switch between software SPI and hardware SPI:
// Example creating a thermocouple instance with software SPI on any three
// digital IO pins.
#define C_BLACK 0x0000
#define C_BLUE 0x001F
#define C_RED 0xF800
#define C_GREEN 0x07E0
#define C_CYAN 0x07FF
#define C_MAGENTA 0xF81F
#define C_YELLOW 0xFFE0
#define C_WHITE 0xFFFF
#define TFT_CS 10
#define TFT_RST -1
#define TFT_DC 9
#define MAXMISO 12
#define MAXCLK 13 //max31855 nothing else.
#define MAX31855_1 14
SPISettings settingsTFT(25000000, MSBFIRST, SPI_MODE3);
SPISettings SettingsA(1000000, MSBFIRST, SPI_MODE0);
// initialize the Thermocouple
Adafruit_MAX31855 TH1(MAXCLK, MAX31855_1, MAXMISO);
//constructors for TFT without flickering
HX8357_t3n tft = HX8357_t3n(TFT_CS, TFT_DC, TFT_RST);
//arrays for the temperature
double TC1[12];
double *pArray_From_TC;
double pArray_Screen;
static int a = 0; // loop function count
float internal;
double centigrade;
double *Thermocouples1()
{
//use this to test the code and the pointers, you dont need to have MAX31855. good idea for debugging
for (int i = 0; i < 12; i++) TC1[i] += 0.25;
return TC1;
// if (TH1.readCelsius() < 0) //if temperature is lower than 0 ////Thermocouple 1
// {
//
//
// TC1[8] = TH1.readCelsius();
// delay(10);
// TC1[4] = TH1.readInternal();
//
// if (isnan( TC1[8]))
// {
// Serial.print("Something wrong with Thermocouple ");
// }
// else {
// Serial.print("C = ");
// Serial.println(TC1[8]);
// }
// } else //this is the 0...500°
// {
//
// TC1[4] = TH1.readInternal();
//
// TC1[8] = TH1.readCelsius();
// if (isnan( TC1[8]))
// {
// Serial.print("Something wrong with Thermocouple ");
// }
// else {
// Serial.print("C = ");
// Serial.println(TC1[8]);
// }
// Serial.print("Thermocouples1 Print cArray_TC[8] = ");
// Serial.println(TC1[8]);
//
//
//
// }
//
// return TC1;
}
void setup()
{
Serial.begin(115200);
//SPI CS pins for the MAX31855 need to be set high!
pinMode(MAX31855_1, OUTPUT); digitalWrite(MAX31855_1, HIGH);
pinMode(TFT_CS, OUTPUT); digitalWrite(TFT_CS, HIGH);
SPI.begin();
tft.begin();
uint8_t x = tft.readcommand8(HX8357_RDMODE);
Serial.print("Display Power Mode: 0x"); Serial.println(x, HEX);
x = tft.readcommand8(HX8357_RDMADCTL);
Serial.print("MADCTL Mode: 0x"); Serial.println(x, HEX);
x = tft.readcommand8(HX8357_RDPIXFMT);
Serial.print("Pixel Format: 0x"); Serial.println(x, HEX);
x = tft.readcommand8(HX8357_RDIMGFMT);
Serial.print("Image Format: 0x"); Serial.println(x, HEX);
x = tft.readcommand8(HX8357_RDSELFDIAG);
Serial.print("Self Diagnostic: 0x"); Serial.println(x, HEX);
tft.setRotation(1);
tft.fillScreen(C_BLACK);
tft.fillRect(0, 30, 480, 10, C_RED);
tft.fillRect(0, 140, 480, 10, C_BLUE);
// if (!TH1.begin())
// {
// //dont do anything
// Serial.println("Error");
// }
}
void loop()
{
//
a++;
if ((a >= 0) && (a <= 5))
{
uint32_t d = 0;
uint8_t buf[4];
TH1.spi_dev.read(buf, 4);
d = buf[0];
d <<= 8;
d |= buf[1];
d <<= 8;
d |= buf[2];
d <<= 8;
d |= buf[3];
// Serial.println(d, HEX);
//return d;
Serial.println("test = ");
Serial.println(d);
}
else if ((a > 5) && (a < 10)) //during the first 50ms read the sensor.
{
uint32_t v;
v = TH1.spiread32();
// ignore bottom 4 bits - they're just thermocouple data
v >>= 4;
// pull the bottom 11 bits off
float internal = v & 0x7FF;
// check sign bit!
if (v & 0x800) {
// Convert to negative value by extending sign and casting to signed type.
int16_t tmp = 0xF800 | (v & 0x7FF);
internal = tmp;
}
internal *= 0.0625; // LSB = 0.0625 degrees
// Serial.print("\tInternal Temp: "); Serial.println(internal);
Serial.print("Internal Temp ==");
Serial.println(internal);
}
else if ((a >= 10) && (a <= 15))
{
uint32_t v;
v = TH1.spiread32();
if (v & 0x7) {
// uh oh, a serious problem!
return NAN;
}
if (v & 0x80000000) {
// Negative value, drop the lower 18 bits and explicitly extend sign bits.
v = 0xFFFFC000 | ((v >> 18) & 0x00003FFF);
} else {
// Positive value, just drop the lower 18 bits.
v >>= 18;
}
// Serial.println(v, HEX);
double centigrade = v;
// LSB = 0.25 degrees C
centigrade *= 0.25;
Serial.print("centigrade");
Serial.println(centigrade);
}
else if ((a >= 15) && (a <= 20) )
{
//other TFT function
//TFTscreen(Thermocouples1());
TFTfunction();
tft.fillScreen(C_RED);
//TFTmillis = TFTmillis - 200; //it calls the screen 4x in a second
} else
{
a = 0;
b = 0;
}
}
double TFTfunction()
{
Serial.println("inside the TFT function");
tft.setTextColor(C_WHITE, C_BLACK);
tft.setCursor(0, 20);
tft.setTextSize(2);
tft.println(" This is just a test");
tft.println(" this is just a test ");
tft.setTextSize(2);
tft.setTextColor(C_WHITE, C_BLACK);
tft.setCursor(20, 200);
tft.print("TempEmag: ");
// Serial.println("pArray_Screen[8]");
// Serial.println(pArray_Screen[8]);
// tft.print(pArray_Screen[8]);
//
// // tft.print(j, 2);
// tft.print(" "); // blank out some if new string not as long as previous..
// sprintf(str, "TempEmag: %.2lf\n", pArray_Screen[8]);
// tft.print(str);
return 0;
}