diff --git a/examples/Arduino/TFTArc/TFTArc.ino b/examples/Arduino/TFTArc/TFTArc.ino new file mode 100644 index 0000000..372a84f --- /dev/null +++ b/examples/Arduino/TFTArc/TFTArc.ino @@ -0,0 +1,73 @@ +/* TFTArc + by K.Abhijeet + This example code takes input x, y(center coordinates of arc ), r(radius), start angle and end angle from serial and prints the arc to the screen. +*/ + +#include // Arduino LCD library +#include + +// pin definition for the Uno +#define cs 10 +#define dc 9 +#define rst 8 + + +// pin definition for the Leonardo +// #define cs 7 +// #define dc 0 +// #define rst 1 + +// create an instance of the library +TFT TFTscreen = TFT(cs, dc, rst); +int16_t x, y, r, st_angle, end_angle; + +void setup() { + + Serial.begin(9600); + + // Put this line at the beginning of every sketch that uses the GLCD: + TFTscreen.begin(); + + // clear the screen with a black background + TFTscreen.background(0, 0, 0); + + // set the font color to white + TFTscreen.stroke(255, 255, 255); + + // print text on the serial monitor to instruct user for the input + Serial.println("Enter x :"); + //waiting for input + while (Serial.available() == 0) {} + x = Serial.parseInt(); + + // print text on the serial monitor to instruct user for the input + Serial.println("Enter y :"); + //waiting for input + while (Serial.available() == 0) {} + y = Serial.parseInt(); + + // print text on the serial monitor to instruct user for the input + Serial.println("Enter r :"); + //waiting for input + while (Serial.available() == 0) {} + r = Serial.parseInt(); + + // print text on the serial monitor to instruct user for the input + Serial.println("Enter start angle :"); + //waiting for input + while (Serial.available() == 0) {} + st_angle = Serial.parseInt(); + + // print text on the serial monitor to instruct user for the input + Serial.println("Enter end angle :"); + //waiting for input + while (Serial.available() == 0) {} + end_angle = Serial.parseInt(); + + TFTscreen.setTextSize(1); + TFTscreen.text("Your Arc: ", 2, 0); + + TFTscreen.arc(x, y, r, st_angle, end_angle); //draw arc +} + +void loop() {} diff --git a/examples/Arduino/TFTCircle/TFTCircle.ino b/examples/Arduino/TFTCircle/TFTCircle.ino new file mode 100644 index 0000000..db40619 --- /dev/null +++ b/examples/Arduino/TFTCircle/TFTCircle.ino @@ -0,0 +1,51 @@ +/* TFTCircle + + by K.Abhijeet + This example code takes input radius from serial and prints the circle to the screen. +*/ + +#include // Arduino LCD library +#include + +// pin definition for the Uno +#define cs 10 +#define dc 9 +#define rst 8 + +// pin definition for the Leonardo +// #define cs 7 +// #define dc 0 +// #define rst 1 + +// create an instance of the library +TFT TFTscreen = TFT(cs, dc, rst); + +int red; + +void setup() { + + Serial.begin(9600); + + // Put this line at the beginning of every sketch that uses the GLCD: + TFTscreen.begin(); + + // clear the screen with a black background + TFTscreen.background(0, 0, 0); + + // set the font color to white + TFTscreen.stroke(255, 255, 255); + + // print text on the serial monitor to instruct user for the input + Serial.println("Enter radius :"); + //waiting for input + while (Serial.available() == 0) {} + red = Serial.parseInt(); + + TFTscreen.setTextSize(1); + TFTscreen.text("Your circle : ", 2, 0); + + TFTscreen.fill(255, 255, 0); + TFTscreen.circle( TFTscreen.width() / 2, TFTscreen.height() / 2, red); // draw circle +} + +void loop() {} diff --git a/examples/Arduino/TFTLine/TFTLine.ino b/examples/Arduino/TFTLine/TFTLine.ino new file mode 100644 index 0000000..788c1a9 --- /dev/null +++ b/examples/Arduino/TFTLine/TFTLine.ino @@ -0,0 +1,65 @@ +/* TFTLine + + by K.Abhijeet + This example code takes input x1,y1(Initial coordinates to draw the line) and x2,y2(end co-ordinates to draw the line) from serial monitor and prints the line to the screen. +*/ + +#include // Arduino LCD library +#include + +// pin definition for the Uno +#define cs 10 +#define dc 9 +#define rst 8 + +// pin definition for the Leonardo +// #define cs 7 +// #define dc 0 +// #define rst 1 + +// create an instance of the library +TFT TFTscreen = TFT(cs, dc, rst); +int x1, y1, x2, y2; + +void setup() { + + Serial.begin(9600); + + // Put this line at the beginning of every sketch that uses the GLCD: + TFTscreen.begin(); + + // clear the screen with a black background + TFTscreen.background(0, 0, 0); + + // set the font color to white + TFTscreen.stroke(255, 255, 255); + + // print text on the serial monitor to instruct user for the input + Serial.println("Enter x1 :"); + //waiting for input + while (Serial.available() == 0) {} + x1 = Serial.parseInt(); + + // print text on the serial monitor to instruct user for the input + Serial.println("Enter y1 :"); + //waiting for input + while (Serial.available() == 0) {} + y1 = Serial.parseInt(); + + // print text on the serial monitor to instruct user for the input + Serial.println("Enter x2 :"); + //waiting for input + while (Serial.available() == 0) {} + x2 = Serial.parseInt(); + + // print text on the serial monitor to instruct user for the input + Serial.println("Enter y2 :"); + //waiting for input + while (Serial.available() == 0) {} + y2 = Serial.parseInt(); + + TFTscreen.setTextSize(1); + TFTscreen.text("Your Line : ", 2, 0); + TFTscreen.line(x1, y1, x2, y2); +} +void loop() {} diff --git a/examples/Arduino/TFTPointLine/TFTPointLine.ino b/examples/Arduino/TFTPointLine/TFTPointLine.ino new file mode 100644 index 0000000..b311ebc --- /dev/null +++ b/examples/Arduino/TFTPointLine/TFTPointLine.ino @@ -0,0 +1,97 @@ +/* TFTPointLine + + by K.Abhijeet + This example code takes input x1,y1(Initial coordinates to draw the line) and x2,y2(end coordinates to draw the line) from serial monitor and prints the line to the screen + by using bresenham’s line drawing algorithm. +*/ + +#include // Arduino LCD library +#include + +// pin definition for the Uno +#define cs 10 +#define dc 9 +#define rst 8 + +// pin definition for the Leonardo +// #define cs 7 +// #define dc 0 +// #define rst 1 + +// create an instance of the library +TFT TFTscreen = TFT(cs, dc, rst); +int x1, y1, x2, y2; +float m, b; +void setup() { + + Serial.begin(9600); + + // Put this line at the beginning of every sketch that uses the GLCD: + TFTscreen.begin(); + + // clear the screen with a black background + TFTscreen.background(0, 0, 0); + + // set the font color to white + TFTscreen.stroke(255, 255, 255); + + // print text on the serial monitor to instruct user for the input + Serial.println("Enter x1 :"); + //waiting for input + while (Serial.available() == 0) {} + x1 = Serial.parseInt(); + + // print text on the serial monitor to instruct user for the input + Serial.println("Enter y1 :"); + //waiting for input + while (Serial.available() == 0) {} + y1 = Serial.parseInt(); + + // print text on the serial monitor to instruct user for the input + Serial.println("Enter x2 :"); + //waiting for input + while (Serial.available() == 0) {} + x2 = Serial.parseInt(); + + // print text on the serial monitor to instruct user for the input + Serial.println("Enter y2 :"); + //waiting for input + while (Serial.available() == 0) {} + y2 = Serial.parseInt(); + + TFTscreen.setTextSize(1); + TFTscreen.text("Your Line : ", 2, 0); + drawline(x1, y1, x2, y2); +} +void loop() {} + +void drawline(int x0, int y0, int x1, int y1) +{ + int dx, dy, p, x, y; + + dx = x1 - x0; + dy = y1 - y0; + + x = x0; + y = y0; + + p = 2 * dy - dx; + + while (x < x1) + { + if (p >= 0) + { + delay(100); + TFTscreen.point(x, y); + y = y + 1; + p = p + 2 * dy - 2 * dx; + } + else + { + delay(100); + TFTscreen.point(x, y); + p = p + 2 * dy; + } + x = x + 1; + } +} diff --git a/examples/Arduino/TFTRainbow/TFTRainbow.ino b/examples/Arduino/TFTRainbow/TFTRainbow.ino new file mode 100644 index 0000000..84d9d7e --- /dev/null +++ b/examples/Arduino/TFTRainbow/TFTRainbow.ino @@ -0,0 +1,51 @@ +/* TFTRainbow + by K.Abhijeet + This example code prints the Rainbow to the screen. +*/ + +#include // Arduino LCD library +#include + +// pin definition for the Uno +#define cs 10 +#define dc 9 +#define rst 8 + + +// pin definition for the Leonardo +// #define cs 7 +// #define dc 0 +// #define rst 1 + +// create an instance of the library +TFT TFTscreen = TFT(cs, dc, rst); +int16_t x=80, y=100, r, st_angle=0, end_angle=180; + +void setup() { + + Serial.begin(9600); + + // Put this line at the beginning of every sketch that uses the GLCD: + TFTscreen.begin(); + + // clear the screen with a black background + TFTscreen.background(0, 0, 0); + + // set the font color to white + TFTscreen.stroke(255, 255, 255); + + + TFTscreen.setTextSize(1); + TFTscreen.text("Rainbow: ", 2, 0); + + for(r=0;r<=50;r++) + { + TFTscreen.stroke(random(255),random(255),random(255)); + delay(5); + TFTscreen.arc(x, y, r, st_angle, end_angle); + } + + Serial.println("Rainbow completed!"); +} + +void loop() {} diff --git a/examples/Arduino/TFTRect/TFTRect.ino b/examples/Arduino/TFTRect/TFTRect.ino new file mode 100644 index 0000000..33ee0a7 --- /dev/null +++ b/examples/Arduino/TFTRect/TFTRect.ino @@ -0,0 +1,57 @@ +/* TFTRect + + by K.Abhijeet + This example code takes input length and width from serial and prints the rectangle to the screen. +*/ +#include // Arduino LCD library +#include + +// pin definition for the Uno +#define cs 10 +#define dc 9 +#define rst 8 + +// pin definition for the Leonardo +// #define cs 7 +// #define dc 0 +// #define rst 1 + +// create an instance of the library +TFT TFTscreen = TFT(cs, dc, rst); + +// char array to print to the screen + +int len, wid; + +void setup() { + + Serial.begin(9600); + + // Put this line at the beginning of every sketch that uses the GLCD: + TFTscreen.begin(); + + // clear the screen with a black background + TFTscreen.background(0, 0, 0); + + // set the font color to white + TFTscreen.stroke(255, 255, 255); + + // print text on the serial monitor to instruct user for the input + Serial.println("Enter length :"); + //waiting for input + while (Serial.available() == 0) {} + len = Serial.parseInt(); + + // print text on the serial monitor to instruct user for the input + Serial.println("Enter width :"); + //waiting for input + while (Serial.available() == 0) {} + wid = Serial.parseInt(); + + TFTscreen.setTextSize(1); + TFTscreen.text("Your Rectangle : ", 2, 0); + + TFTscreen.fill(255, 255, 0); + TFTscreen.rect(10, 20, len, wid); // draw rectangle +} +void loop() {} diff --git a/examples/Arduino/TFTStorageRead/TFTStorageRead.ino b/examples/Arduino/TFTStorageRead/TFTStorageRead.ino new file mode 100644 index 0000000..de1bff8 --- /dev/null +++ b/examples/Arduino/TFTStorageRead/TFTStorageRead.ino @@ -0,0 +1,125 @@ +/* + TFTStorageRead + + This example shows how to read data from an SD card file and prints it to the screen. + + The circuit: + SD card attached to SPI bus as follows: + ** MOSI - pin 11 + ** MISO - pin 12 + ** CLK - pin 13 + ** CS - pin 4 (for MKRZero SD: SDCARD_SS_PIN) + + TFT Screen attached to SPI bus as follows: + cs - pin 10 + dc - pin 9 + rst - pin 8 + + created by Abhijeet Kadam + This example code is in the public domain. +*/ + +// pin definition for the Uno +#define cs 10 +#define dc 9 +#define rst 8 + +#include // Arduino LCD library +#include +#include + +String fname; +int n = 0; +File root, myFile; +char str[100];// char array to print to the screen + +// create an instance of the library +TFT TFTscreen = TFT(cs, dc, rst); + +void setup() { + // Open serial communications and wait for port to open: + Serial.begin(9600); + while (!Serial) { + ; // wait for serial port to connect. Needed for native USB port only + } + + Serial.print("Initializing SD card..."); + + if (!SD.begin(4)) { + Serial.println("initialization failed!"); + while (1); + } + Serial.println("initialization done."); + + // Put this line at the beginning of every sketch that uses the GLCD: + TFTscreen.begin(); + + // clear the screen with a black background + TFTscreen.background(0, 0, 0); + + // set the font color to white + TFTscreen.stroke(255, 255, 255); + // set the font size + + root = SD.open("/"); // create root reference. + printDirectory(root, 0); // print directory and file list + + Serial.println("Enter file name to read file data:");//ask user for file name eg. test.txt + waitip(); + fname = Serial.readString(); + fname.trim(); + + if (SD.exists(fname)) { //if file already exists + + myFile = SD.open(fname); + if (myFile) { + Serial.println(" " + fname); + // read from the file until there's nothing else in it: + n = 0; + while (myFile.available()) { + str[n] = myFile.read(); + n++; + } + // close the file: + myFile.close(); + TFTscreen.text(str, 0, 0); + } + } + else { //if file not exists show the message + Serial.println(fname + " doesn't exist."); + } +} + +void loop() { +} + +void waitip()// wait for user input +{ + while (Serial.available() == 0) + {} +} + +void printDirectory(File dir, int numTabs) // prints directory list +{ + while (true) { + + File entry = dir.openNextFile(); + if (! entry) { + // no more files + break; + } + for (uint8_t i = 0; i < numTabs; i++) { + Serial.print('\t'); + } + Serial.print(entry.name()); + if (entry.isDirectory()) { + Serial.println("/"); + printDirectory(entry, numTabs + 1); + } else { + // files have sizes, directories do not + Serial.print("\t\t"); + Serial.println(entry.size(), DEC); + } + entry.close(); + } +} diff --git a/examples/Arduino/TFTTriangle/TFTTriangle.ino b/examples/Arduino/TFTTriangle/TFTTriangle.ino new file mode 100644 index 0000000..621dc15 --- /dev/null +++ b/examples/Arduino/TFTTriangle/TFTTriangle.ino @@ -0,0 +1,76 @@ +/* TFTPointLine + + by K.Abhijeet + This example code takes input x1,y1 x2,y2 and x3,y3 from serial and prints the triangle to the screen +*/ + +#include // Arduino LCD library +#include + +// pin definition for the Uno +#define cs 10 +#define dc 9 +#define rst 8 + +// pin definition for the Leonardo +// #define cs 7 +// #define dc 0 +// #define rst 1 + +// create an instance of the library +TFT TFTscreen = TFT(cs, dc, rst); +int x1, y1, x2, y2, x3, y3; + +void setup() { + + Serial.begin(9600); + + // Put this line at the beginning of every sketch that uses the GLCD: + TFTscreen.begin(); + + // clear the screen with a black background + TFTscreen.background(0, 0, 0); + + // set the font color to white + TFTscreen.stroke(255, 255, 255); + + // print text on the serial monitor to instruct user for the input + Serial.println("Enter x1 :"); + //waiting for input + while (Serial.available() == 0) {} + x1 = Serial.parseInt(); + + // print text on the serial monitor to instruct user for the input + Serial.println("Enter y1 :"); + //waiting for input + while (Serial.available() == 0) {} + y1 = Serial.parseInt(); + + // print text on the serial monitor to instruct user for the input + Serial.println("Enter x2 :"); + //waiting for input + while (Serial.available() == 0) {} + x2 = Serial.parseInt(); + + // print text on the serial monitor to instruct user for the input + Serial.println("Enter y2 :"); + //waiting for input + while (Serial.available() == 0) {} + y2 = Serial.parseInt(); + + // print text on the serial monitor to instruct user for the input + Serial.println("Enter x3 :"); + //waiting for input + while (Serial.available() == 0) {} + x3 = Serial.parseInt(); + + // print text on the serial monitor to instruct user for the input + Serial.println("Enter y3 :"); + //waiting for input + while (Serial.available() == 0) {} + y3 = Serial.parseInt(); + + //draw triangle to the screen + TFTscreen.triangle(x1, y1, x2, y2, x3, y3); +} +void loop() {} diff --git a/examples/Arduino/TFTUserText/TFTUserText.ino b/examples/Arduino/TFTUserText/TFTUserText.ino new file mode 100644 index 0000000..a45b321 --- /dev/null +++ b/examples/Arduino/TFTUserText/TFTUserText.ino @@ -0,0 +1,55 @@ +/* UserText + + by K.Abhijeet + This example code takes input username from serial and prints to the screen. +*/ +#include // Arduino LCD library +#include + +// pin definition for the Uno +#define cs 10 +#define dc 9 +#define rst 8 + +// pin definition for the Leonardo +// #define cs 7 +// #define dc 0 +// #define rst 1 + +// create an instance of the library +TFT TFTscreen = TFT(cs, dc, rst); + +// char array to print to the screen +String nm; +char str[100]; + +void setup() { + + Serial.begin(9600); + + // Put this line at the beginning of every sketch that uses the GLCD: + TFTscreen.begin(); + + // clear the screen with a black background + TFTscreen.background(0, 0, 0); + + // set the font color to white + TFTscreen.stroke(255, 255, 255); + // set the font size + + // print text on the serial monitor to instruct user for the input + Serial.println("Enter your name :"); + TFTscreen.setTextSize(1); + TFTscreen.text("Enter your name :\n ", 2, 2); + //waiting for input + while (Serial.available() == 0) {} + nm = Serial.readString(); // take input from serial + nm = "Hello " + nm; + + // convert the string to a char array + nm.toCharArray(str, 100); + + //print the name + TFTscreen.text(str, 0, 20); +} +void loop() {} diff --git a/src/utility/Adafruit_GFX.cpp b/src/utility/Adafruit_GFX.cpp index 4176f11..b5addf2 100644 --- a/src/utility/Adafruit_GFX.cpp +++ b/src/utility/Adafruit_GFX.cpp @@ -160,7 +160,7 @@ void Adafruit_GFX::fillCircleHelper(int16_t x0, int16_t y0, int16_t r, } } -// Bresenham's algorithm - thx wikipedia +// Bresenham's algorithm - Thanks J.E. Bresenham(1962), Bresenham's line algorithm Wikipedia [URL: https://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm ] void Adafruit_GFX::drawLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1, uint16_t color) @@ -655,6 +655,40 @@ void Adafruit_GFX::triangle(int16_t x1, int16_t y1, int16_t x2, int16_t y2, int1 } } +// draw a arc outline +void Adafruit_GFX::drawArc(int16_t x, int16_t y, int16_t r, int16_t st_angle, int16_t end_angle,uint16_t color) +{ + + float angle; +float range; +float h; +float k; + angle=(((st_angle<=end_angle)?st_angle:end_angle)*(PI/180)); + range=(((end_angle>st_angle)?end_angle:st_angle)*(PI/180)); + + h=(r*cos(angle)); + k=(r*sin(angle)); + + do { + drawPixel((int16_t)(x+h+0.5),(int16_t)(y-k+0.5) ,strokeColor); + + angle = angle + 0.001; + h=(r*cos(angle)); + k=(r*sin(angle)); + + } while (angle<=range); + + +} +// draw a arc outline +void Adafruit_GFX::arc(int16_t x, int16_t y, int16_t r, int16_t st_angle, int16_t end_angle) +{ + + drawArc(x,y,r,st_angle,end_angle,strokeColor); + +} + + #if defined(__SD_H__) // Arduino SD library #define BUFFPIXEL 20 diff --git a/src/utility/Adafruit_GFX.h b/src/utility/Adafruit_GFX.h index ab02b29..c5030fc 100644 --- a/src/utility/Adafruit_GFX.h +++ b/src/utility/Adafruit_GFX.h @@ -60,7 +60,7 @@ #warning "The SD library was not found. loadImage() and image() won't be supported." #endif -inline void swap(int16_t &a, int16_t &b) { int16_t t = a; a = b; b = t; } +#define swap(a, b) { int16_t t = a; a = b; b = t; } /* TODO enum RectMode { @@ -125,7 +125,8 @@ class Adafruit_GFX : public Print { setTextColor(uint16_t c, uint16_t bg), setTextSize(uint8_t s), setTextWrap(boolean w), - setRotation(uint8_t r); + setRotation(uint8_t r), + drawArc(int16_t x, int16_t y, int16_t r, int16_t st_angle, int16_t end_angle,uint16_t color); #if ARDUINO >= 100 virtual size_t write(uint8_t); @@ -185,8 +186,8 @@ class Adafruit_GFX : public Print { quad(int16_t x1, int16_t y1, int16_t x2, int16_t y2, int16_t x3, int16_t y3, int16_t x4, int16_t y4), rect(int16_t x, int16_t y, int16_t width, int16_t height), rect(int16_t x, int16_t y, int16_t width, int16_t height, int16_t radius), - triangle(int16_t x1, int16_t y1, int16_t x2, int16_t y2, int16_t x3, int16_t y3); - + triangle(int16_t x1, int16_t y1, int16_t x2, int16_t y2, int16_t x3, int16_t y3), + arc(int16_t x, int16_t y, int16_t r, int16_t st_angle, int16_t end_angle); /* TODO void rectMode(RectMode mode); diff --git a/src/utility/Adafruit_ST7735.h b/src/utility/Adafruit_ST7735.h index fbbd6ab..fa3b7c0 100644 --- a/src/utility/Adafruit_ST7735.h +++ b/src/utility/Adafruit_ST7735.h @@ -109,6 +109,7 @@ class Adafruit_ST7735 : public Adafruit_GFX { pushColor(uint16_t color), fillScreen(uint16_t color), drawPixel(int16_t x, int16_t y, uint16_t color), + drawArc(int16_t x, int16_t y, int16_t r, int16_t st_angle, int16_t end_angle,uint16_t color), drawFastVLine(int16_t x, int16_t y, int16_t h, uint16_t color), drawFastHLine(int16_t x, int16_t y, int16_t w, uint16_t color), fillRect(int16_t x, int16_t y, int16_t w, int16_t h, diff --git a/src/utility/keywords.txt b/src/utility/keywords.txt index 9614847..35fa4ad 100644 --- a/src/utility/keywords.txt +++ b/src/utility/keywords.txt @@ -41,6 +41,8 @@ height KEYWORD2 width KEYWORD2 setRotation KEYWORD2 getRotation KEYWORD2 +drawArc KEYWORD2 +arc KEYWORD2