A Smartwatch
Using Teensy, Adafruit 2.8 TFT LCD 
Code Explanation:

 Posted by  Manpreet Singh on June 14, 2015
Smartwatch Touchscreen

As promised, here is the explanation of code of SmartWatch v1.0-

Code explanation:                     

1. Drawing on lcd

All graphics done on LCD is using UTFT library. Here is code for graphics part- 

 
#include<UTFT.h>//to include the library
extern uint8_t SmallFont[];
extern uint8_t BigFont[];
extern uint8_t SevenSegNumFont[]; //for different fonts
//analog clock variables
int centerX = 0;
int centerY = 0;
int radius = 0;
UTFT myGLCD(ILI9341_S5P,7,14,6,16,17);  //Format (model,MOSI,SCK,CS/SS,RST,RS/(D/C))
                        

Of course you can use alternative CS pins. Here is an alternative pinout of Teensy 3.1 

 
void setup()
{
  myGLCD.InitLCD();  //initializing LCD
  centerX = 160;		//center x co-ordinate	
  centerY = 120;		//center y co-ordinate
  radius = centerY-10;		//radius of analog clock
  myGLCD.clrScr(); //for clearing of screen on start
}
void draw()				//digital clock
{
  myGLCD.setColor(0, 255, 0);  //setting color of text
  myGLCD.setBackColor(0, 0, 0);	     //setting background color

  myGLCD.setFont(BigFont);	//choosing font
  myGLCD.print("TIME",CENTER,50);
  myGLCD.setFont(SevenSegNumFont);
 if(hour<10)			//if hour<10,print 0 before hour
 {
  myGLCD.printNumI(0, 60, 90);
  myGLCD.printNumI(hour, 100, 90);
 }
  else
  myGLCD.printNumI(hour, 60, 90);
  myGLCD.setFont(BigFont);
  myGLCD.print(",", 140, 95);
  myGLCD.print("'", 140, 125);
  myGLCD.setFont(SevenSegNumFont);
 if(minutes<10)		//same with minutes
 {
  myGLCD.printNumI(0, 160, 90);
  myGLCD.printNumI(minutes, 195, 90);
 }
  else
  myGLCD.printNumI(minutes, 160, 90);
  myGLCD.setFont(BigFont);
 if(ampm==0)
 myGLCD.print("AM",230,120);
 else
 myGLCD.print("PM",230,120);
 myGLCD.setFont(BigFont);
 switch(week)
  {
	  case 1:myGLCD.print("SUN,",40,170);
	  break;
	  case 2:myGLCD.print("MON,",40,170);
	  break;
	  case 3:myGLCD.print("TUE,",40,170);
	  break;
	  case 4:myGLCD.print("WED,",40,170);
	  break;
	  case 5:myGLCD.print("THU,",40,170);
	  break;
	  case 6:myGLCD.print("FRI,",40,170);
	  break;
	  default:myGLCD.print("SAT,",40,170);
	  break;
	};
	myGLCD.printNumI(day,110,170);
	myGLCD.print("/",150,170);
	myGLCD.printNumI(month,170,170);
	myGLCD.print("/",210,170);
	myGLCD.printNumI(year,230,170);
	}
void drawclock()				//analog clock
{
  myGLCD.drawCircle(centerX, centerY, radius);
  showTimePin(centerX, centerY, 0.1, 0.5, hour*5 + (int)(minutes*5/60));//hour hand
  showTimePin(centerX, centerY, 0.1, 0.78, minutes);	//minutes hand
}

double RAD=3.141592/180;
double LR = 89.99;

void showTimePin(int center_x, int center_y, double pl1, double pl2, double pl3) 
{
  double x1, x2, y1, y2;
  x1 = center_x + (radius * pl1) * cos((6 * pl3 + LR) * RAD);
  y1 = center_y + (radius * pl1) * sin((6 * pl3 + LR) * RAD);
  x2 = center_x + (radius * pl2) * cos((6 * pl3 - LR) * RAD);
  y2 = center_y + (radius * pl2) * sin((6 * pl3 - LR) * RAD);
  //polar mathematics for circle
  myGLCD.drawLine((int)x1, (int)y1, (int)x2, (int)y2);
}

For details,refer to UTFT manual for different functions

2. Time

Different variables for time-

						 
unsigned long prev_time = 0;
unsigned long current_time = 0;
#define update_interval 1000		//time after which clock updates
byte month = 1;
byte day = 1;
byte week = 1;
byte ampm = 0;
byte hour = 0;
byte minutes = 0;
byte seconds = 0;
int year = 2014;

void loop()
 {
  current_time = millis();	//time since the program started executing
  updateTime(current_time);	//updating time
 }
void updateTime(unsigned long current_time) 
 {
  if(minutes >= 0) 
  {
    if(current_time - prev_time > update_interval)            //update interval=1sec
	{		
     // Increase time
     seconds++;
     if(seconds>=60){
      seconds=0;
      minutes++;
       if(minutes >= 60) {
        minutes = 0;
        hour++;
        if(hour > 12) {
           hour = 1;
          (ampm == 0) ? ampm=1 : ampm=0;
           if(ampm == 0) {
            week++;
             if(week > 7)
             week = 1;
             day++;
              if(month==4||month==6||month==9||month==11)   //April,June,September,November
               {
                if(day > 30)  
                { 
			      day = 1;
			      month++;
			   }
               }
	           else if(month==2 && (year%4)==0)        //leap year
                       {
		         if(day>29)                            //February
                  {
		           day=1;month++;
				  }                          
                 }
               else if(month==2)
                {
				if(day>28)                
                 {
		          day=1;month++;
				 }
                }
               else
                {
				day=1;month++;
				}                      //Rest
          }
        }
      }
	  myGLCD.clrScr();draw();	//draw after every minute
    }
      prev_time = current_time;		//update time every second
    }
  }
}

						 

3. Bluetooth

I am using 2nd serial port (i.e. pins 9 & 10) for Bluetooth module.

						 
#define BTSerial Serial2	  //2nd serial port(this method applicable for Teensy only)
const int fields = 7;		  //no. of field will be received from app
int timeBuffer[fields];	          //buffer
void setup()
{
 BTSerial.begin(9600);
}
void loop()
{
 BTbegin();
}
void BTbegin()
{
 if( BTSerial.available() > 0 ) // if data is available to read
{
for(int i = 0;i12)
  {
  hour = timeBuffer[4]-12;
  ampm=1;                        //0=AM.1=PM
  }
  else
  hour = timeBuffer[4];
}
						 

The above functions depends entirely on your app that how it sends value through Bluetooth. As you can see above, my app has problem in month(less by 1).
So, I have to correct it manually.

In case you need, here are the connections of Teensy, LCD and HC05-

LCD                      Teensy 3.1
CLK ------>             14
MOSI----->               7
CS--------->              6
RST------->             16
D/C-------->            17

No need for MISO pin for now.

HC-05                     Teensy 3.1
RX ------>             10
TX----->               9

Comments:


If You Like this Post, Please Like our Page


Top
www.000webhost.com