added arduino, modified build

This commit is contained in:
2020-02-02 15:28:36 -08:00
parent 0189d519c6
commit 6480bc593f
3583 changed files with 1305025 additions and 247 deletions

View File

@@ -0,0 +1,26 @@
= TFT Library for Arduino =
This library enables an Arduino board to communicate with the Arduino TFT LCD screen. It simplifies the process for drawing shapes, lines, images, and text to the screen.
For more information about this library please visit us at
http://www.arduino.cc/en/Reference/TFTLibrary
== License ==
Copyright (c) Arduino LLC. All right reserved.
Copyright (c) Enrico Gueli. All right reserved.
Copyright (c) 2012, Adafruit Industries. All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

View File

@@ -0,0 +1,108 @@
/*
Arduino TFT Bitmap Logo example
This example reads an image file from a micro-SD card
and draws it on the screen, at random locations.
In this sketch, the Arduino logo is read from a micro-SD card.
There is a .bmp file included with this sketch.
- open the sketch folder (Ctrl-K or Cmd-K)
- copy the "arduino.bmp" file to a micro-SD
- put the SD into the SD slot of the Arduino TFT module.
This example code is in the public domain.
Created 19 April 2013 by Enrico Gueli
http://www.arduino.cc/en/Tutorial/TFTBitmapLogo
*/
// include the necessary libraries
#include <SPI.h>
#include <SD.h>
#include <TFT.h> // Arduino LCD library
// pin definition for the Uno
#define sd_cs 4
#define lcd_cs 10
#define dc 9
#define rst 8
// pin definition for the Leonardo
//#define sd_cs 8
//#define lcd_cs 7
//#define dc 0
//#define rst 1
TFT TFTscreen = TFT(lcd_cs, dc, rst);
// this variable represents the image to be drawn on screen
PImage logo;
void setup() {
// initialize the GLCD and show a message
// asking the user to open the serial line
TFTscreen.begin();
TFTscreen.background(255, 255, 255);
TFTscreen.stroke(0, 0, 255);
TFTscreen.println();
TFTscreen.println(F("Arduino TFT Bitmap Example"));
TFTscreen.stroke(0, 0, 0);
TFTscreen.println(F("Open serial monitor"));
TFTscreen.println(F("to run the sketch"));
// initialize the serial port: it will be used to
// print some diagnostic info
Serial.begin(9600);
while (!Serial) {
// wait for serial port to connect. Needed for native USB port only
}
// clear the GLCD screen before starting
TFTscreen.background(255, 255, 255);
// try to access the SD card. If that fails (e.g.
// no card present), the setup process will stop.
Serial.print(F("Initializing SD card..."));
if (!SD.begin(sd_cs)) {
Serial.println(F("failed!"));
return;
}
Serial.println(F("OK!"));
// initialize and clear the GLCD screen
TFTscreen.begin();
TFTscreen.background(255, 255, 255);
// now that the SD card can be access, try to load the
// image file.
logo = TFTscreen.loadImage("arduino.bmp");
if (!logo.isValid()) {
Serial.println(F("error while loading arduino.bmp"));
}
}
void loop() {
// don't do anything if the image wasn't loaded correctly.
if (logo.isValid() == false) {
return;
}
Serial.println(F("drawing image"));
// get a random location where to draw the image.
// To avoid the image to be draw outside the screen,
// take into account the image size.
int x = random(TFTscreen.width() - logo.width());
int y = random(TFTscreen.height() - logo.height());
// draw the image to the screen
TFTscreen.image(logo, x, y);
// wait a little bit before drawing again
delay(1500);
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.8 KiB

View File

@@ -0,0 +1,67 @@
/*
TFT Color Picker
This example for the Arduino screen reads the input of
potentiometers or analog sensors attached to A0, A1,
and A2 and uses the values to change the screen's color.
This example code is in the public domain.
Created 15 April 2013 by Scott Fitzgerald
http://www.arduino.cc/en/Tutorial/TFTColorPicker
*/
// 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
#include <TFT.h> // Arduino LCD library
#include <SPI.h>
TFT TFTscreen = TFT(cs, dc, rst);
void setup() {
// begin serial communication
Serial.begin(9600);
// initialize the display
TFTscreen.begin();
// set the background to white
TFTscreen.background(255, 255, 255);
}
void loop() {
// read the values from your sensors and scale them to 0-255
int redVal = map(analogRead(A0), 0, 1023, 0, 255);
int greenVal = map(analogRead(A1), 0, 1023, 0, 255);
int blueVal = map(analogRead(A2), 0, 1023, 0, 255);
// draw the background based on the mapped values
TFTscreen.background(redVal, greenVal, blueVal);
// send the values to the serial monitor
Serial.print("background(");
Serial.print(redVal);
Serial.print(" , ");
Serial.print(greenVal);
Serial.print(" , ");
Serial.print(blueVal);
Serial.println(")");
// wait for a moment
delay(33);
}

View File

@@ -0,0 +1,74 @@
/*
Arduino TFT text example
This example demonstrates how to draw text on the
TFT with an Arduino. The Arduino reads the value
of an analog sensor attached to pin A0, and writes
the value to the LCD screen, updating every
quarter second.
This example code is in the public domain
Created 15 April 2013 by Scott Fitzgerald
http://www.arduino.cc/en/Tutorial/TFTDisplayText
*/
#include <TFT.h> // Arduino LCD library
#include <SPI.h>
// 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
char sensorPrintout[4];
void setup() {
// 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);
// write the static text to the screen
// set the font color to white
TFTscreen.stroke(255, 255, 255);
// set the font size
TFTscreen.setTextSize(2);
// write the text to the top left corner of the screen
TFTscreen.text("Sensor Value :\n ", 0, 0);
// ste the font size very large for the loop
TFTscreen.setTextSize(5);
}
void loop() {
// Read the value of the sensor on A0
String sensorVal = String(analogRead(A0));
// convert the reading to a char array
sensorVal.toCharArray(sensorPrintout, 4);
// set the font color
TFTscreen.stroke(255, 255, 255);
// print the sensor value
TFTscreen.text(sensorPrintout, 0, 20);
// wait for a moment
delay(250);
// erase the text you just wrote
TFTscreen.stroke(0, 0, 0);
TFTscreen.text(sensorPrintout, 0, 20);
}

View File

@@ -0,0 +1,83 @@
/*
TFT EtchASketch
This example for the Arduino screen draws a white point
on the GLCD based on the values of 2 potentiometers.
To clear the screen, press a button attached to pin 2.
This example code is in the public domain.
Created 15 April 2013 by Scott Fitzgerald
http://www.arduino.cc/en/Tutorial/TFTEtchASketch
*/
#include <TFT.h> // Arduino LCD library
#include <SPI.h>
// 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
TFT TFTscreen = TFT(cs, dc, rst);
// initial position of the cursor
int xPos = TFTscreen.width() / 2;
int yPos = TFTscreen.height() / 2;
// pin the erase switch is connected to
int erasePin = 2;
void setup() {
// declare inputs
pinMode(erasePin, INPUT);
// initialize the screen
TFTscreen.begin();
// make the background black
TFTscreen.background(0, 0, 0);
}
void loop() {
// read the potentiometers on A0 and A1
int xValue = analogRead(A0);
int yValue = analogRead(A1);
// map the values and update the position
xPos = xPos + (map(xValue, 0, 1023, 2, -2));
yPos = yPos + (map(yValue, 0, 1023, -2, 2));
// don't let the point go past the screen edges
if (xPos > 159) {
(xPos = 159);
}
if (xPos < 0) {
(xPos = 0);
}
if (yPos > 127) {
(yPos = 127);
}
if (yPos < 0) {
(yPos = 0);
}
// draw the point
TFTscreen.stroke(255, 255, 255);
TFTscreen.point(xPos, yPos);
// read the value of the pin, and erase the screen if pressed
if (digitalRead(erasePin) == HIGH) {
TFTscreen.background(0, 0, 0);
}
delay(33);
}

View File

@@ -0,0 +1,70 @@
/*
TFT Graph
This example for an Arduino screen reads
the value of an analog sensor on A0, and
graphs the values on the screen.
This example code is in the public domain.
Created 15 April 2013 by Scott Fitzgerald
http://www.arduino.cc/en/Tutorial/TFTGraph
*/
#include <TFT.h> // Arduino LCD library
#include <SPI.h>
// 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
TFT TFTscreen = TFT(cs, dc, rst);
// position of the line on screen
int xPos = 0;
void setup() {
// initialize the serial port
Serial.begin(9600);
// initialize the display
TFTscreen.begin();
// clear the screen with a pretty color
TFTscreen.background(250, 16, 200);
}
void loop() {
// read the sensor and map it to the screen height
int sensor = analogRead(A0);
int drawHeight = map(sensor, 0, 1023, 0, TFTscreen.height());
// print out the height to the serial monitor
Serial.println(drawHeight);
// draw a line in a nice color
TFTscreen.stroke(250, 180, 10);
TFTscreen.line(xPos, TFTscreen.height() - drawHeight, xPos, TFTscreen.height());
// if the graph has reached the screen edge
// erase the screen and start again
if (xPos >= 160) {
xPos = 0;
TFTscreen.background(250, 16, 200);
} else {
// increment the horizontal position:
xPos++;
}
delay(16);
}

View File

@@ -0,0 +1,135 @@
/*
TFT Pong
This example for the Arduino screen reads the values
of 2 potentiometers to move a rectangular platform
on the x and y axes. The platform can intersect
with a ball causing it to bounce.
This example code is in the public domain.
Created by Tom Igoe December 2012
Modified 15 April 2013 by Scott Fitzgerald
http://www.arduino.cc/en/Tutorial/TFTPong
*/
#include <TFT.h> // Arduino LCD library
#include <SPI.h>
// 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
TFT TFTscreen = TFT(cs, dc, rst);
// variables for the position of the ball and paddle
int paddleX = 0;
int paddleY = 0;
int oldPaddleX, oldPaddleY;
int ballDirectionX = 1;
int ballDirectionY = 1;
int ballSpeed = 10; // lower numbers are faster
int ballX, ballY, oldBallX, oldBallY;
void setup() {
// initialize the display
TFTscreen.begin();
// black background
TFTscreen.background(0, 0, 0);
}
void loop() {
// save the width and height of the screen
int myWidth = TFTscreen.width();
int myHeight = TFTscreen.height();
// map the paddle's location to the position of the potentiometers
paddleX = map(analogRead(A0), 512, -512, 0, myWidth) - 20 / 2;
paddleY = map(analogRead(A1), 512, -512, 0, myHeight) - 5 / 2;
// set the fill color to black and erase the previous
// position of the paddle if different from present
TFTscreen.fill(0, 0, 0);
if (oldPaddleX != paddleX || oldPaddleY != paddleY) {
TFTscreen.rect(oldPaddleX, oldPaddleY, 20, 5);
}
// draw the paddle on screen, save the current position
// as the previous.
TFTscreen.fill(255, 255, 255);
TFTscreen.rect(paddleX, paddleY, 20, 5);
oldPaddleX = paddleX;
oldPaddleY = paddleY;
// update the ball's position and draw it on screen
if (millis() % ballSpeed < 2) {
moveBall();
}
}
// this function determines the ball's position on screen
void moveBall() {
// if the ball goes offscreen, reverse the direction:
if (ballX > TFTscreen.width() || ballX < 0) {
ballDirectionX = -ballDirectionX;
}
if (ballY > TFTscreen.height() || ballY < 0) {
ballDirectionY = -ballDirectionY;
}
// check if the ball and the paddle occupy the same space on screen
if (inPaddle(ballX, ballY, paddleX, paddleY, 20, 5)) {
ballDirectionX = -ballDirectionX;
ballDirectionY = -ballDirectionY;
}
// update the ball's position
ballX += ballDirectionX;
ballY += ballDirectionY;
// erase the ball's previous position
TFTscreen.fill(0, 0, 0);
if (oldBallX != ballX || oldBallY != ballY) {
TFTscreen.rect(oldBallX, oldBallY, 5, 5);
}
// draw the ball's current position
TFTscreen.fill(255, 255, 255);
TFTscreen.rect(ballX, ballY, 5, 5);
oldBallX = ballX;
oldBallY = ballY;
}
// this function checks the position of the ball
// to see if it intersects with the paddle
boolean inPaddle(int x, int y, int rectX, int rectY, int rectWidth, int rectHeight) {
boolean result = false;
if ((x >= rectX && x <= (rectX + rectWidth)) &&
(y >= rectY && y <= (rectY + rectHeight))) {
result = true;
}
return result;
}

View File

@@ -0,0 +1,101 @@
/*
Esplora TFT Bitmap Logos
This example for the Arduino TFT screen is for use
with an Arduino Esplora.
This example reads an image file from a micro-SD card
and draws it on the screen, at random locations.
There is a .bmp file included with this sketch.
- open the sketch folder (Ctrl-K or Cmd-K)
- copy the "arduino.bmp" file to a micro-SD
- put the SD into the SD slot of the Arduino LCD module.
This example code is in the public domain.
Created 19 April 2013 by Enrico Gueli
http://www.arduino.cc/en/Tutorial/EsploraTFTBitmapLogo
*/
// include the necessary libraries
#include <Esplora.h>
#include <SPI.h>
#include <SD.h>
#include <TFT.h> // Arduino LCD library
// the Esplora pin connected to the chip select line for SD card
#define SD_CS 8
// this variable represents the image to be drawn on screen
PImage logo;
void setup() {
// initialize the GLCD and show a message
// asking the user to open the serial line
EsploraTFT.begin();
EsploraTFT.background(255, 255, 255);
EsploraTFT.stroke(0, 0, 255);
EsploraTFT.println();
EsploraTFT.println(F("Arduino LCD Bitmap Example"));
EsploraTFT.stroke(0, 0, 0);
EsploraTFT.println(F("Open serial monitor"));
EsploraTFT.println(F("to run the sketch"));
// initialize the serial port: it will be used to
// print some diagnostic info
Serial.begin(9600);
while (!Serial) {
// wait for serial port to connect. Needed for native USB port only
}
// try to access the SD card. If that fails (e.g.
// no card present), the Esplora's LED will turn red.
Serial.print(F("Initializing SD card..."));
if (!SD.begin(SD_CS)) {
Serial.println(F("failed!"));
Esplora.writeRed(255);
return;
}
Serial.println("OK!");
// clear the GLCD screen before starting
EsploraTFT.background(255, 255, 255);
// now that the SD card can be access, try to load the
// image file. The Esplora LED will turn green or red if
// the loading went OK or not.
Esplora.writeRGB(0, 0, 0);
logo = EsploraTFT.loadImage("arduino.bmp");
if (logo.isValid()) {
Esplora.writeGreen(255);
} else {
Esplora.writeRed(255);
}
}
void loop() {
// don't do anything if the image wasn't loaded correctly.
if (logo.isValid() == false) {
return;
}
Serial.println(F("drawing image"));
// get a random location where to draw the image.
// To avoid the image to be draw outside the screen,
// take into account the image size.
int x = random(EsploraTFT.width() - logo.width());
int y = random(EsploraTFT.height() - logo.height());
// draw the image to the screen
EsploraTFT.image(logo, x, y);
// wait a little bit before drawing again
delay(1500);
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.8 KiB

View File

@@ -0,0 +1,54 @@
/*
Esplora TFT Color Picker
This example for the Esplora with an Arduino TFT reads
the input of the joystick and slider, using the values
to change the screen's color.
This example code is in the public domain.
Created 15 April 2013 by Scott Fitzgerald
http://www.arduino.cc/en/Tutorial/TFTColorPicker
*/
#include <Esplora.h>
#include <TFT.h> // Arduino LCD library
#include <SPI.h>
void setup() {
Serial.begin(9600);
// initialize the LCD
EsploraTFT.begin();
// start out with a white screen
EsploraTFT.background(255, 255, 255);
}
void loop() {
// map the values from sensors
int xValue = map(Esplora.readJoystickX(), -512, 512, 0, 255); // read the joystick's X position
int yValue = map(Esplora.readJoystickY(), -512, 512, 0, 255); // read the joystick's Y position
int slider = map(Esplora.readSlider(), 0, 1023, 0, 255); // read the slider's position
// change the background color based on the mapped values
EsploraTFT.background(xValue, yValue, slider);
// print the mapped values to the Serial monitor
Serial.print("background(");
Serial.print(xValue);
Serial.print(" , ");
Serial.print(yValue);
Serial.print(" , ");
Serial.print(slider);
Serial.println(")");
delay(33);
}

View File

@@ -0,0 +1,80 @@
/*
Esplora TFT EtchASketch
This example for the Arduino TFT and Esplora draws
a white line on the screen, based on the position
of the joystick. To clear the screen, shake the
Esplora, using the values from the accelerometer.
This example code is in the public domain.
Created 15 April 2013 by Scott Fitzgerald
http://www.arduino.cc/en/Tutorial/EsploraTFTEtchASketch
*/
#include <Esplora.h>
#include <TFT.h> // Arduino LCD library
#include <SPI.h>
// initial position of the cursor
int xPos = EsploraTFT.width() / 2;
int yPos = EsploraTFT.height() / 2;
void setup() {
// initialize the display
EsploraTFT.begin();
// clear the background
EsploraTFT.background(0, 0, 0);
}
void loop() {
int xAxis = Esplora.readJoystickX(); // read the X axis
int yAxis = Esplora.readJoystickY(); // read the Y axis
// update the position of the line
// depending on the position of the joystick
if (xAxis < 10 && xAxis > -10) {
xPos = xPos;
} else {
xPos = xPos + (map(xAxis, -512, 512, 2, -2));
}
if (yAxis < 10 && yAxis > -10) {
yAxis = yAxis;
} else {
yPos = yPos + (map(yAxis, -512, 512, -2, 2));
}
// don't let the point go past the screen edges
if (xPos > 159) {
(xPos = 159);
}
if (xPos < 0) {
(xPos = 0);
}
if (yPos > 127) {
(yPos = 127);
}
if (yPos < 0) {
(yPos = 0);
}
// draw the point
EsploraTFT.stroke(255, 255, 255);
EsploraTFT.point(xPos, yPos);
// check the accelerometer values and clear
// the screen if it is being shaken
if (abs(Esplora.readAccelerometer(X_AXIS)) > 200 || abs(Esplora.readAccelerometer(Y_AXIS)) > 200) {
EsploraTFT.background(0, 0, 0);
}
delay(33);
}

View File

@@ -0,0 +1,55 @@
/*
Esplora TFT Graph
This example for the Esplora with an Arduino TFT reads
the value of the light sensor, and graphs the values on
the screen.
This example code is in the public domain.
Created 15 April 2013 by Scott Fitzgerald
http://www.arduino.cc/en/Tutorial/EsploraTFTGraph
*/
#include <Esplora.h>
#include <TFT.h> // Arduino LCD library
#include <SPI.h>
// position of the line on screen
int xPos = 0;
void setup() {
// initialize the screen
EsploraTFT.begin();
// clear the screen with a nice color
EsploraTFT.background(250, 16, 200);
}
void loop() {
// read the sensor value
int sensor = Esplora.readLightSensor();
// map the sensor value to the height of the screen
int graphHeight = map(sensor, 0, 1023, 0, EsploraTFT.height());
// draw the line in a pretty color
EsploraTFT.stroke(250, 180, 10);
EsploraTFT.line(xPos, EsploraTFT.height() - graphHeight, xPos, EsploraTFT.height());
// if the graph reaches the edge of the screen
// erase it and start over from the other side
if (xPos >= 160) {
xPos = 0;
EsploraTFT.background(250, 16, 200);
} else {
// increment the horizontal position:
xPos++;
}
delay(16);
}

View File

@@ -0,0 +1,62 @@
/*
Esplora TFT Horizon
This example for the Arduino TFT and Esplora draws
a line on the screen that stays level with the ground
as you tile the Esplora side to side
This example code is in the public domain.
Created 15 April 2013 by Scott Fitzgerald
http://www.arduino.cc/en/Tutorial/EsploraTFTHorizon
*/
#include <Esplora.h>
#include <TFT.h> // Arduino LCD library
#include <SPI.h>
// horizontal start and end positions
int yStart = EsploraTFT.height() / 2;
int yEnd = EsploraTFT.height() / 2;
// previous start and end positions
int oldEndY;
int oldStartY;
void setup() {
// initialize the display
EsploraTFT.begin();
// make the background black
EsploraTFT.background(0, 0, 0);
}
void loop() {
// read the x-axis of te accelerometer
int tilt = Esplora.readAccelerometer(X_AXIS);
// the values are 100 when tilted to the left
// and -100 when tilted to the right
// map these values to the start and end points
yStart = map(tilt, -100, 100, EsploraTFT.height(), 0);
yEnd = map(tilt, -100, 100, 0, EsploraTFT.height());
// if the previous values are different than the current values
// erase the previous line
if (oldStartY != yStart || oldEndY != yEnd) {
EsploraTFT.stroke(0, 0, 0);
EsploraTFT.line(0, oldStartY, EsploraTFT.width(), oldEndY);
}
// draw the line in magenta
EsploraTFT.stroke(255, 0, 255);
EsploraTFT.line(0, yStart, EsploraTFT.width(), yEnd);
// save the current start and end points
// to compare int he next loop
oldStartY = yStart;
oldEndY = yEnd;
delay(10);
}

View File

@@ -0,0 +1,126 @@
/*
Esplora TFT Pong
This example for the Esplora with an Arduino TFT screen reads
the value of the joystick to move a rectangular platform
on the x and y axes. The platform can intersect with a ball
causing it to bounce. The Esplora's slider adjusts the speed
of the ball.
This example code is in the public domain.
Created by Tom Igoe December 2012
Modified 15 April 2013 by Scott Fitzgerald
http://www.arduino.cc/en/Tutorial/EsploraTFTPong
*/
#include <Esplora.h>
#include <TFT.h> // Arduino LCD library
#include <SPI.h>
// variables for the position of the ball and paddle
int paddleX = 0;
int paddleY = 0;
int oldPaddleX, oldPaddleY;
int ballDirectionX = 1;
int ballDirectionY = 1;
int ballX, ballY, oldBallX, oldBallY;
void setup() {
Serial.begin(9600);
// initialize the display
EsploraTFT.begin();
// set the background the black
EsploraTFT.background(0, 0, 0);
}
void loop() {
// save the width and height of the screen
int myWidth = EsploraTFT.width();
int myHeight = EsploraTFT.height();
// map the paddle's location to the joystick's position
paddleX = map(Esplora.readJoystickX(), 512, -512, 0, myWidth) - 20 / 2;
paddleY = map(Esplora.readJoystickY(), -512, 512, 0, myHeight) - 5 / 2;
Serial.print(paddleX);
Serial.print(" ");
Serial.println(paddleY);
// set the fill color to black and erase the previous
// position of the paddle if different from present
EsploraTFT.fill(0, 0, 0);
if (oldPaddleX != paddleX || oldPaddleY != paddleY) {
EsploraTFT.rect(oldPaddleX, oldPaddleY, 20, 5);
}
// draw the paddle on screen, save the current position
// as the previous.
EsploraTFT.fill(255, 255, 255);
EsploraTFT.rect(paddleX, paddleY, 20, 5);
oldPaddleX = paddleX;
oldPaddleY = paddleY;
// read the slider to determinde the speed of the ball
int ballSpeed = map(Esplora.readSlider(), 0, 1023, 0, 80) + 1;
if (millis() % ballSpeed < 2) {
moveBall();
}
}
// this function determines the ball's position on screen
void moveBall() {
// if the ball goes offscreen, reverse the direction:
if (ballX > EsploraTFT.width() || ballX < 0) {
ballDirectionX = -ballDirectionX;
}
if (ballY > EsploraTFT.height() || ballY < 0) {
ballDirectionY = -ballDirectionY;
}
// check if the ball and the paddle occupy the same space on screen
if (inPaddle(ballX, ballY, paddleX, paddleY, 20, 5)) {
ballDirectionY = -ballDirectionY;
}
// update the ball's position
ballX += ballDirectionX;
ballY += ballDirectionY;
// erase the ball's previous position
EsploraTFT.fill(0, 0, 0);
if (oldBallX != ballX || oldBallY != ballY) {
EsploraTFT.rect(oldBallX, oldBallY, 5, 5);
}
// draw the ball's current position
EsploraTFT.fill(255, 255, 255);
EsploraTFT.rect(ballX, ballY, 5, 5);
oldBallX = ballX;
oldBallY = ballY;
}
// this function checks the position of the ball
// to see if it intersects with the paddle
boolean inPaddle(int x, int y, int rectX, int rectY, int rectWidth, int rectHeight) {
boolean result = false;
if ((x >= rectX && x <= (rectX + rectWidth)) &&
(y >= rectY && y <= (rectY + rectHeight))) {
result = true;
}
return result;
}

View File

@@ -0,0 +1,64 @@
/*
Esplora TFT Temperature Display
This example for the Arduino TFT screen is for use
with an Arduino Esplora.
This example reads the temperature of the Esplora's
on board thermisistor and displays it on an attached
LCD screen, updating every second.
This example code is in the public domain.
Created 15 April 2013 by Scott Fitzgerald
http://www.arduino.cc/en/Tutorial/EsploraTFTTemp
*/
// include the necessary libraries
#include <Esplora.h>
#include <TFT.h> // Arduino LCD library
#include <SPI.h>
char tempPrintout[3]; // array to hold the temperature data
void setup() {
// Put this line at the beginning of every sketch that uses the GLCD
EsploraTFT.begin();
// clear the screen with a black background
EsploraTFT.background(0, 0, 0);
// set the text color to magenta
EsploraTFT.stroke(200, 20, 180);
// set the text to size 2
EsploraTFT.setTextSize(2);
// start the text at the top left of the screen
// this text is going to remain static
EsploraTFT.text("Degrees in C :\n ", 0, 0);
// set the text in the loop to size 5
EsploraTFT.setTextSize(5);
}
void loop() {
// read the temperature in Celcius and store it in a String
String temperature = String(Esplora.readTemperature(DEGREES_C));
// convert the string to a char array
temperature.toCharArray(tempPrintout, 3);
// set the text color to white
EsploraTFT.stroke(255, 255, 255);
// print the temperature one line below the static text
EsploraTFT.text(tempPrintout, 0, 30);
delay(1000);
// erase the text for the next loop
EsploraTFT.stroke(0, 0, 0);
EsploraTFT.text(tempPrintout, 0, 30);
}

View File

@@ -0,0 +1,21 @@
This is a library for the Adafruit 1.8" SPI display.
This library works with the Adafruit 1.8" TFT Breakout w/SD card
----> http://www.adafruit.com/products/358
as well as Adafruit raw 1.8" TFT display
----> http://www.adafruit.com/products/618
Check out the links above for our tutorials and wiring diagrams.
These displays use SPI to communicate, 4 or 5 pins are required
to interface (RST is optional).
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
Written by Limor Fried/Ladyada for Adafruit Industries.
MIT license, all text above must be included in any redistribution
To download. click the DOWNLOADS button in the top right corner, rename the uncompressed folder Adafruit_ST7735. Check that the Adafruit_ST7735 folder contains Adafruit_ST7735.cpp and Adafruit_ST7735.
Place the Adafruit_ST7735 library folder your <arduinosketchfolder>/libraries/ folder. You may need to create the libraries subfolder if its your first library. Restart the IDE
Also requires the Adafruit_GFX library for Arduino.

View File

@@ -0,0 +1,25 @@
Software License Agreement (BSD License)
Copyright (c) 2012, Adafruit Industries. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holders nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

View File

@@ -0,0 +1,18 @@
TFT Library
============
An Arduino library for the Arduino TFT LCD screen.
This library enables an Arduino board to communicate with an Arduino TFT LCD screen. It simplifies the process for drawing shapes, lines, images, and text to the screen.
The Arduino TFT library extends the Adafruit GFX, and Adafruit ST7735 libraries that it is based on. The GFX library is responsible for the drawing routines, while the ST7735 library is specific to the screen on the Arduino GTFT. The Arduino specific additions were designed to work as similarly to the Processing API as possible.
Onboard the screen is a SD card slot, which can be used through the SD library.
The TFT library relies on the SPI library for communication with the screen and SD card, and needs to be included in all sketches.
https://github.com/adafruit/Adafruit-GFX-Library
https://github.com/adafruit/Adafruit-ST7735-Library
http://www.arduino.cc/en/Reference/SD
http://www.arduino.cc/en/Reference/SPI
http://www.arduino.cc/en/Reference/TFTLibrary

View File

@@ -0,0 +1,20 @@
#######################################
# Syntax Coloring Map For Arduino GLCD
#######################################
#######################################
# Datatypes (KEYWORD1)
#######################################
TFT KEYWORD1 TFTLibrary
#######################################
# Methods and Functions (KEYWORD2)
#######################################
#######################################
# Constants (LITERAL1)
#######################################
EsploraTFT LITERAL1

View File

@@ -0,0 +1,9 @@
name=TFT
version=1.0.6
author=Arduino, Adafruit
maintainer=Arduino <info@arduino.cc>
sentence=Allows drawing text, images, and shapes on the Arduino TFT graphical display.
paragraph=This library is compatible with most of the TFT display based on the ST7735 chipset.
category=Display
url=http://www.arduino.cc/en/Reference/TFTLibrary
architectures=*

View File

@@ -0,0 +1,51 @@
/* Copyright (c) 2013, Enrico Gueli <enrico.gueli@gmail.com> All
rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "TFT.h"
#if ARDUINO_AVR_ESPLORA
TFT EsploraTFT(7, 0, 1);
#endif
TFT::TFT(uint8_t CS, uint8_t RS, uint8_t RST)
: Adafruit_ST7735(CS, RS, RST)
{
// as we already know the orientation (landscape, therefore rotated),
// set default width and height without need to call begin() first.
_width = ST7735_TFTHEIGHT;
_height = ST7735_TFTWIDTH;
}
void TFT::begin() {
//initR(INITR_REDTAB);
initG();
setRotation(1);
}

View File

@@ -0,0 +1,58 @@
/* Copyright (c) 2013, Enrico Gueli <enrico.gueli@gmail.com> All
rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _ARDUINO_TFT_H
#define _ARDUINO_TFT_H
#include "Arduino.h"
#include "utility/Adafruit_GFX.h"
#include "utility/Adafruit_ST7735.h"
/// The Arduino LCD is a ST7735-based device.
/// By default, it is mounted horizontally.
/// TFT class follows the convention of other
/// Arduino library classes by adding a begin() method
/// to be called in the setup() routine.
/// @author Enrico Gueli <enrico.gueli@gmail.com>
class TFT : public Adafruit_ST7735 {
public:
TFT(uint8_t CS, uint8_t RS, uint8_t RST);
void begin();
};
/// Esplora boards have hard-wired connections with
/// the Arduino LCD if mounted on the onboard connector.
#if ARDUINO_AVR_ESPLORA // are we building for Esplora?
extern TFT EsploraTFT;
#endif
#endif // _ARDUINO_TFT_H

View File

@@ -0,0 +1,715 @@
/*
This is the core graphics library for all our displays, providing a common
set of graphics primitives (points, lines, circles, etc.). It needs to be
paired with a hardware-specific library for each display device we carry
(to handle the lower-level functions).
Adafruit invests time and resources providing this open source code, please
support Adafruit & open-source hardware by purchasing products from Adafruit!
Copyright (c) 2013 Adafruit Industries. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
- Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
#include "Adafruit_GFX.h"
#include "glcdfont.c"
#ifdef __AVR__
#include <avr/pgmspace.h>
#else
#define pgm_read_byte(addr) (*(const unsigned char *)(addr))
#endif
Adafruit_GFX::Adafruit_GFX(int16_t w, int16_t h) :
WIDTH(w), HEIGHT(h)
{
_width = WIDTH;
_height = HEIGHT;
rotation = 0;
cursor_y = cursor_x = 0;
textsize = 1;
textcolor = textbgcolor = 0xFFFF;
wrap = true;
}
// draw a circle outline
void Adafruit_GFX::drawCircle(int16_t x0, int16_t y0, int16_t r,
uint16_t color) {
int16_t f = 1 - r;
int16_t ddF_x = 1;
int16_t ddF_y = -2 * r;
int16_t x = 0;
int16_t y = r;
drawPixel(x0, y0+r, color);
drawPixel(x0, y0-r, color);
drawPixel(x0+r, y0, color);
drawPixel(x0-r, y0, color);
while (x<y) {
if (f >= 0) {
y--;
ddF_y += 2;
f += ddF_y;
}
x++;
ddF_x += 2;
f += ddF_x;
drawPixel(x0 + x, y0 + y, color);
drawPixel(x0 - x, y0 + y, color);
drawPixel(x0 + x, y0 - y, color);
drawPixel(x0 - x, y0 - y, color);
drawPixel(x0 + y, y0 + x, color);
drawPixel(x0 - y, y0 + x, color);
drawPixel(x0 + y, y0 - x, color);
drawPixel(x0 - y, y0 - x, color);
}
}
void Adafruit_GFX::drawCircleHelper( int16_t x0, int16_t y0,
int16_t r, uint8_t cornername, uint16_t color) {
int16_t f = 1 - r;
int16_t ddF_x = 1;
int16_t ddF_y = -2 * r;
int16_t x = 0;
int16_t y = r;
while (x<y) {
if (f >= 0) {
y--;
ddF_y += 2;
f += ddF_y;
}
x++;
ddF_x += 2;
f += ddF_x;
if (cornername & 0x4) {
drawPixel(x0 + x, y0 + y, color);
drawPixel(x0 + y, y0 + x, color);
}
if (cornername & 0x2) {
drawPixel(x0 + x, y0 - y, color);
drawPixel(x0 + y, y0 - x, color);
}
if (cornername & 0x8) {
drawPixel(x0 - y, y0 + x, color);
drawPixel(x0 - x, y0 + y, color);
}
if (cornername & 0x1) {
drawPixel(x0 - y, y0 - x, color);
drawPixel(x0 - x, y0 - y, color);
}
}
}
void Adafruit_GFX::fillCircle(int16_t x0, int16_t y0, int16_t r,
uint16_t color)
{
drawFastVLine(x0, y0-r, 2*r+1, color);
fillCircleHelper(x0, y0, r, 3, 0, color);
}
// used to do circles and roundrects
void Adafruit_GFX::fillCircleHelper(int16_t x0, int16_t y0, int16_t r,
uint8_t cornername, int16_t delta, uint16_t color)
{
int16_t f = 1 - r;
int16_t ddF_x = 1;
int16_t ddF_y = -2 * r;
int16_t x = 0;
int16_t y = r;
while (x<y) {
if (f >= 0) {
y--;
ddF_y += 2;
f += ddF_y;
}
x++;
ddF_x += 2;
f += ddF_x;
if (cornername & 0x1) {
drawFastVLine(x0+x, y0-y, 2*y+1+delta, color);
drawFastVLine(x0+y, y0-x, 2*x+1+delta, color);
}
if (cornername & 0x2) {
drawFastVLine(x0-x, y0-y, 2*y+1+delta, color);
drawFastVLine(x0-y, y0-x, 2*x+1+delta, color);
}
}
}
// Bresenham's algorithm - thx wikpedia
void Adafruit_GFX::drawLine(int16_t x0, int16_t y0,
int16_t x1, int16_t y1,
uint16_t color)
{
int16_t steep = abs(y1 - y0) > abs(x1 - x0);
if (steep) {
swap(x0, y0);
swap(x1, y1);
}
if (x0 > x1) {
swap(x0, x1);
swap(y0, y1);
}
int16_t dx, dy;
dx = x1 - x0;
dy = abs(y1 - y0);
int16_t err = dx / 2;
int16_t ystep;
if (y0 < y1) {
ystep = 1;
} else {
ystep = -1;
}
for (; x0<=x1; x0++) {
if (steep) {
drawPixel(y0, x0, color);
} else {
drawPixel(x0, y0, color);
}
err -= dy;
if (err < 0) {
y0 += ystep;
err += dx;
}
}
}
// Draw a rectangle
void Adafruit_GFX::drawRect(int16_t x, int16_t y,
int16_t w, int16_t h,
uint16_t color)
{
drawFastHLine(x, y, w, color);
drawFastHLine(x, y+h-1, w, color);
drawFastVLine(x, y, h, color);
drawFastVLine(x+w-1, y, h, color);
}
void Adafruit_GFX::drawFastVLine(int16_t x, int16_t y,
int16_t h, uint16_t color)
{
// Update in subclasses if desired!
drawLine(x, y, x, y+h-1, color);
}
void Adafruit_GFX::drawFastHLine(int16_t x, int16_t y,
int16_t w, uint16_t color)
{
// Update in subclasses if desired!
drawLine(x, y, x+w-1, y, color);
}
void Adafruit_GFX::fillRect(int16_t x, int16_t y, int16_t w, int16_t h,
uint16_t color)
{
// Update in subclasses if desired!
for (int16_t i=x; i<x+w; i++) {
drawFastVLine(i, y, h, color);
}
}
void Adafruit_GFX::fillScreen(uint16_t color) {
fillRect(0, 0, _width, _height, color);
}
// Draw a rounded rectangle
void Adafruit_GFX::drawRoundRect(int16_t x, int16_t y, int16_t w,
int16_t h, int16_t r, uint16_t color)
{
// smarter version
drawFastHLine(x+r , y , w-2*r, color); // Top
drawFastHLine(x+r , y+h-1, w-2*r, color); // Bottom
drawFastVLine(x , y+r , h-2*r, color); // Left
drawFastVLine(x+w-1, y+r , h-2*r, color); // Right
// draw four corners
drawCircleHelper(x+r , y+r , r, 1, color);
drawCircleHelper(x+w-r-1, y+r , r, 2, color);
drawCircleHelper(x+w-r-1, y+h-r-1, r, 4, color);
drawCircleHelper(x+r , y+h-r-1, r, 8, color);
}
// Fill a rounded rectangle
void Adafruit_GFX::fillRoundRect(int16_t x, int16_t y, int16_t w,
int16_t h, int16_t r, uint16_t color)
{
// smarter version
fillRect(x+r, y, w-2*r, h, color);
// draw four corners
fillCircleHelper(x+w-r-1, y+r, r, 1, h-2*r-1, color);
fillCircleHelper(x+r , y+r, r, 2, h-2*r-1, color);
}
// Draw a triangle
void Adafruit_GFX::drawTriangle(int16_t x0, int16_t y0,
int16_t x1, int16_t y1,
int16_t x2, int16_t y2, uint16_t color) {
drawLine(x0, y0, x1, y1, color);
drawLine(x1, y1, x2, y2, color);
drawLine(x2, y2, x0, y0, color);
}
// Fill a triangle
void Adafruit_GFX::fillTriangle ( int16_t x0, int16_t y0,
int16_t x1, int16_t y1,
int16_t x2, int16_t y2, uint16_t color) {
int16_t a, b, y, last;
// Sort coordinates by Y order (y2 >= y1 >= y0)
if (y0 > y1) {
swap(y0, y1); swap(x0, x1);
}
if (y1 > y2) {
swap(y2, y1); swap(x2, x1);
}
if (y0 > y1) {
swap(y0, y1); swap(x0, x1);
}
if(y0 == y2) { // Handle awkward all-on-same-line case as its own thing
a = b = x0;
if(x1 < a) a = x1;
else if(x1 > b) b = x1;
if(x2 < a) a = x2;
else if(x2 > b) b = x2;
drawFastHLine(a, y0, b-a+1, color);
return;
}
int16_t
dx01 = x1 - x0,
dy01 = y1 - y0,
dx02 = x2 - x0,
dy02 = y2 - y0,
dx12 = x2 - x1,
dy12 = y2 - y1,
sa = 0,
sb = 0;
// For upper part of triangle, find scanline crossings for segments
// 0-1 and 0-2. If y1=y2 (flat-bottomed triangle), the scanline y1
// is included here (and second loop will be skipped, avoiding a /0
// error there), otherwise scanline y1 is skipped here and handled
// in the second loop...which also avoids a /0 error here if y0=y1
// (flat-topped triangle).
if(y1 == y2) last = y1; // Include y1 scanline
else last = y1-1; // Skip it
for(y=y0; y<=last; y++) {
a = x0 + sa / dy01;
b = x0 + sb / dy02;
sa += dx01;
sb += dx02;
/* longhand:
a = x0 + (x1 - x0) * (y - y0) / (y1 - y0);
b = x0 + (x2 - x0) * (y - y0) / (y2 - y0);
*/
if(a > b) swap(a,b);
drawFastHLine(a, y, b-a+1, color);
}
// For lower part of triangle, find scanline crossings for segments
// 0-2 and 1-2. This loop is skipped if y1=y2.
sa = dx12 * (y - y1);
sb = dx02 * (y - y0);
for(; y<=y2; y++) {
a = x1 + sa / dy12;
b = x0 + sb / dy02;
sa += dx12;
sb += dx02;
/* longhand:
a = x1 + (x2 - x1) * (y - y1) / (y2 - y1);
b = x0 + (x2 - x0) * (y - y0) / (y2 - y0);
*/
if(a > b) swap(a,b);
drawFastHLine(a, y, b-a+1, color);
}
}
void Adafruit_GFX::drawBitmap(int16_t x, int16_t y,
const uint8_t *bitmap, int16_t w, int16_t h,
uint16_t color)
{
int16_t i, j, byteWidth = (w + 7) / 8;
for(j=0; j<h; j++) {
for(i=0; i<w; i++ ) {
if(pgm_read_byte(bitmap + j * byteWidth + i / 8) & (128 >> (i & 7))) {
drawPixel(x+i, y+j, color);
}
}
}
}
#if ARDUINO >= 100
size_t Adafruit_GFX::write(uint8_t c) {
#else
void Adafruit_GFX::write(uint8_t c) {
#endif
if (c == '\n') {
cursor_y += textsize*8;
cursor_x = 0;
} else if (c == '\r') {
// skip em
} else {
drawChar(cursor_x, cursor_y, c, textcolor, textbgcolor, textsize);
cursor_x += textsize*6;
if (wrap && (cursor_x > (_width - textsize*6))) {
cursor_y += textsize*8;
cursor_x = 0;
}
}
#if ARDUINO >= 100
return 1;
#endif
}
// draw a character
void Adafruit_GFX::drawChar(int16_t x, int16_t y, unsigned char c,
uint16_t color, uint16_t bg, uint8_t size)
{
if((x >= _width) || // Clip right
(y >= _height) || // Clip bottom
((x + 6 * size - 1) < 0) || // Clip left
((y + 8 * size - 1) < 0)) // Clip top
return;
for (int8_t i=0; i<6; i++ ) {
uint8_t line;
if (i == 5)
line = 0x0;
else
line = pgm_read_byte(font+(c*5)+i);
for (int8_t j = 0; j<8; j++) {
if (line & 0x1) {
if (size == 1) // default size
drawPixel(x+i, y+j, color);
else { // big size
fillRect(x+(i*size), y+(j*size), size, size, color);
}
} else if (bg != color) {
if (size == 1) // default size
drawPixel(x+i, y+j, bg);
else { // big size
fillRect(x+i*size, y+j*size, size, size, bg);
}
}
line >>= 1;
}
}
}
void Adafruit_GFX::setCursor(int16_t x, int16_t y)
{
cursor_x = x;
cursor_y = y;
}
void Adafruit_GFX::setTextSize(uint8_t s)
{
textsize = (s > 0) ? s : 1;
}
void Adafruit_GFX::setTextColor(uint16_t c) {
textcolor = c;
textbgcolor = c;
// for 'transparent' background, we'll set the bg
// to the same as fg instead of using a flag
}
void Adafruit_GFX::setTextColor(uint16_t c, uint16_t b)
{
textcolor = c;
textbgcolor = b;
}
void Adafruit_GFX::setTextWrap(boolean w)
{
wrap = w;
}
uint8_t Adafruit_GFX::getRotation(void)
{
return rotation;
}
void Adafruit_GFX::setRotation(uint8_t x)
{
rotation = (x & 3);
switch (x) {
case 0:
case 2:
_width = WIDTH;
_height = HEIGHT;
break;
case 1:
case 3:
_width = HEIGHT;
_height = WIDTH;
break;
}
}
// return the size of the display (per current rotation)
int16_t Adafruit_GFX::width(void)
{
return _width;
}
int16_t Adafruit_GFX::height(void)
{
return _height;
}
void Adafruit_GFX::invertDisplay(boolean i)
{
// Do nothing, must be subclassed if supported
}
uint16_t Adafruit_GFX::newColor(uint8_t r, uint8_t g, uint8_t b)
{
return ((r & 0xF8) << 8) | ((g & 0xFC) << 3) | (b >> 3);
}
void Adafruit_GFX::background(uint8_t red, uint8_t green, uint8_t blue)
{
background(newColor(red, green, blue));
}
void Adafruit_GFX::background(color c) {
fillScreen(c);
}
void Adafruit_GFX::stroke(uint8_t red, uint8_t green, uint8_t blue)
{
stroke(newColor(red, green, blue));
}
void Adafruit_GFX::stroke(color c)
{
useStroke = true;
strokeColor = c;
setTextColor(c);
}
void Adafruit_GFX::noStroke()
{
useStroke = false;
}
void Adafruit_GFX::noFill() {
useFill = false;
}
void Adafruit_GFX::fill(uint8_t red, uint8_t green, uint8_t blue)
{
fill(newColor(red, green, blue));
}
void Adafruit_GFX::fill(color c)
{
useFill = true;
fillColor = c;
}
void Adafruit_GFX::text(const char * text, int16_t x, int16_t y)
{
if (!useStroke)
return;
setTextWrap(false);
setTextColor(strokeColor);
setCursor(x, y);
print(text);
}
void Adafruit_GFX::textWrap(const char * text, int16_t x, int16_t y)
{
if (!useStroke)
return;
setTextWrap(true);
setTextColor(strokeColor);
setCursor(x, y);
print(text);
}
void Adafruit_GFX::textSize(uint8_t size)
{
setTextSize(size);
}
void Adafruit_GFX::point(int16_t x, int16_t y)
{
if (!useStroke)
return;
drawPixel(x, y, strokeColor);
}
void Adafruit_GFX::line(int16_t x1, int16_t y1, int16_t x2, int16_t y2)
{
if (!useStroke)
return;
if (x1 == x2) {
if (y1 < y2)
drawFastVLine(x1, y1, y2 - y1, strokeColor);
else
drawFastVLine(x1, y2, y1 - y2, strokeColor);
}
else if (y1 == y2) {
if (x1 < x2)
drawFastHLine(x1, y1, x2 - x1, strokeColor);
else
drawFastHLine(x2, y1, x1 - x2, strokeColor);
}
else {
drawLine(x1, y1, x2, y2, strokeColor);
}
}
void Adafruit_GFX::rect(int16_t x, int16_t y, int16_t width, int16_t height)
{
if (useFill) {
fillRect(x, y, width, height, fillColor);
}
if (useStroke) {
drawRect(x, y, width, height, strokeColor);
}
}
void Adafruit_GFX::rect(int16_t x, int16_t y, int16_t width, int16_t height, int16_t radius)
{
if (radius == 0) {
rect(x, y, width, height);
}
if (useFill) {
fillRoundRect(x, y, width, height, radius, fillColor);
}
if (useStroke) {
drawRoundRect(x, y, width, height, radius, strokeColor);
}
}
void Adafruit_GFX::circle(int16_t x, int16_t y, int16_t r)
{
if (r == 0)
return;
if (useFill) {
fillCircle(x, y, r, fillColor);
}
if (useStroke) {
drawCircle(x, y, r, strokeColor);
}
}
void Adafruit_GFX::triangle(int16_t x1, int16_t y1, int16_t x2, int16_t y2, int16_t x3, int16_t y3)
{
if (useFill) {
fillTriangle(x1, y1, x2, y2, x3, y3, fillColor);
}
if (useStroke) {
drawTriangle(x1, y1, x2, y2, x3, y3, strokeColor);
}
}
#if defined(__SD_H__) // Arduino SD library
#define BUFFPIXEL 20
void Adafruit_GFX::image(PImage & img, uint16_t x, uint16_t y) {
int w, h, row, col;
uint8_t r, g, b;
uint32_t pos = 0;
uint8_t sdbuffer[3*BUFFPIXEL]; // pixel buffer (R+G+B per pixel)
uint8_t buffidx = sizeof(sdbuffer); // Current position in sdbuffer
// Crop area to be loaded
w = img._bmpWidth;
h = img._bmpHeight;
if((x+w-1) >= width()) w = width() - x;
if((y+h-1) >= height()) h = height() - y;
/*
// Set TFT address window to clipped image bounds
setAddrWindow(x, y, x+w-1, y+h-1);
*/
for (row=0; row<h; row++) { // For each scanline...
// Seek to start of scan line. It might seem labor-
// intensive to be doing this on every line, but this
// method covers a lot of gritty details like cropping
// and scanline padding. Also, the seek only takes
// place if the file position actually needs to change
// (avoids a lot of cluster math in SD library).
if(img._flip) // Bitmap is stored bottom-to-top order (normal BMP)
pos = img._bmpImageoffset + (img._bmpHeight - 1 - row) * img._rowSize;
else // Bitmap is stored top-to-bottom
pos = img._bmpImageoffset + row * img._rowSize;
if(img._bmpFile.position() != pos) { // Need seek?
img._bmpFile.seek(pos);
buffidx = sizeof(sdbuffer); // Force buffer reload
}
for (col=0; col<w; col++) { // For each pixel...
// Time to read more pixel data?
if (buffidx >= sizeof(sdbuffer)) { // Indeed
img._bmpFile.read(sdbuffer, sizeof(sdbuffer));
buffidx = 0; // Set index to beginning
}
// Convert pixel from BMP to TFT format, push to display
b = sdbuffer[buffidx++];
g = sdbuffer[buffidx++];
r = sdbuffer[buffidx++];
//pushColor(tft.Color565(r,g,b));
drawPixel(x + col, y + row, newColor(r, g, b));
} // end pixel
} // end scanline
}
#endif

View File

@@ -0,0 +1,372 @@
/******************************************************************
This is the core graphics library for all our displays, providing
basic graphics primitives (points, lines, circles, etc.). It needs
to be paired with a hardware-specific library for each display
device we carry (handling the lower-level functions).
Adafruit invests time and resources providing this open
source code, please support Adafruit and open-source hardware
by purchasing products from Adafruit!
Written by Limor Fried/Ladyada for Adafruit Industries.
Processing-like API written by Enrico Gueli for Officine Arduino.
BSD license, check license.txt for more information.
All text above must be included in any redistribution.
******************************************************************/
#ifndef _ADAFRUIT_GFX_H
#define _ADAFRUIT_GFX_H
#if ARDUINO >= 100
#include "Arduino.h"
#include "Print.h"
#else
#include "WProgram.h"
#endif
/*
* This library can work with or without the presence of an SD
* reading library (to load images). At the moment, only the
* Arduino SD library is supported; it is included in
* standard Arduino libraries.
*
* The presence of the SD library is detected by looking at the
* __SD_H__ preprocessor variable, defined into
* Arduino SD library to avoid double inclusion. This means
* that in order to use the image-related API of Adafruit_GFX,
* SD.h *must* be included before Adafruit_GFX.
*
* The bottom part of this include file contains the actual image
* loading code; if it was in a separate .cpp file, there were no
* way to check if the SD library was present or not.
*
* A partial solution was to include SD.h anyway, see if that works
* (i.e. it is found in the include search path) and act accordingly.
* But this solution relied on the preprocessor to issue only a
* warning when an include file is not found. Avr-gcc, used for
* Arduino 8-bit MCUs, does that, but the standard gcc-4.4, used for
* Arduino Due, issues a fatal error and stops compilation.
*
* The best solution so far is to put the code here. It works if this
* include is used only in one .cpp file in the build (this is the
* case of most Arduino sketches); if used in multiple .cpp files,
* the linker may complain about duplicate definitions.
*
*/
#if defined(__SD_H__) // Arduino SD library
#include "PImage.h"
#else
#warning "The SD library was not found. loadImage() and image() won't be supported."
#endif
#define swap(a, b) { int16_t t = a; a = b; b = t; }
/* TODO
enum RectMode {
CORNER,
CORNERS,
RADIUS,
CENTER
};
*/
typedef uint16_t color;
class Adafruit_GFX : public Print {
public:
Adafruit_GFX(int16_t w, int16_t h); // Constructor
// This MUST be defined by the subclass
virtual void drawPixel(int16_t x, int16_t y, uint16_t color) = 0;
// These MAY be overridden by the subclass to provide device-specific
// optimized code. Otherwise 'generic' versions are used.
virtual void
drawLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1,
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),
drawRect(int16_t x, int16_t y, int16_t w, int16_t h,
uint16_t color),
fillRect(int16_t x, int16_t y, int16_t w, int16_t h,
uint16_t color),
fillScreen(uint16_t color),
invertDisplay(boolean i);
// These exist only with Adafruit_GFX (no subclass overrides)
void
drawCircle(int16_t x0, int16_t y0, int16_t r, uint16_t color),
drawCircleHelper(int16_t x0, int16_t y0,
int16_t r, uint8_t cornername, uint16_t color),
fillCircle(int16_t x0, int16_t y0, int16_t r, uint16_t color),
fillCircleHelper(int16_t x0, int16_t y0, int16_t r,
uint8_t cornername, int16_t delta, uint16_t color),
drawTriangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1,
int16_t x2, int16_t y2, uint16_t color),
fillTriangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1,
int16_t x2, int16_t y2, uint16_t color),
drawRoundRect(int16_t x0, int16_t y0, int16_t w, int16_t h,
int16_t radius, uint16_t color),
fillRoundRect(int16_t x0, int16_t y0, int16_t w, int16_t h,
int16_t radius, uint16_t color),
drawBitmap(int16_t x, int16_t y,
const uint8_t *bitmap, int16_t w, int16_t h,
uint16_t color),
drawChar(int16_t x, int16_t y, unsigned char c,
uint16_t color, uint16_t bg, uint8_t size),
setCursor(int16_t x, int16_t y),
setTextColor(uint16_t c),
setTextColor(uint16_t c, uint16_t bg),
setTextSize(uint8_t s),
setTextWrap(boolean w),
setRotation(uint8_t r);
#if ARDUINO >= 100
virtual size_t write(uint8_t);
#else
virtual void write(uint8_t);
#endif
int16_t
height(void),
width(void);
uint8_t getRotation(void);
/*
* Processing-like graphics primitives
*/
/// transforms a color in 16-bit form given the RGB components.
/// The default implementation makes a 5-bit red, a 6-bit
/// green and a 5-bit blue (MSB to LSB). Devices that use
/// different scheme should override this.
virtual uint16_t newColor(uint8_t red, uint8_t green, uint8_t blue);
void
// http://processing.org/reference/background_.html
background(uint8_t red, uint8_t green, uint8_t blue),
background(color c),
// http://processing.org/reference/fill_.html
fill(uint8_t red, uint8_t green, uint8_t blue),
fill(color c),
// http://processing.org/reference/noFill_.html
noFill(),
// http://processing.org/reference/stroke_.html
stroke(uint8_t red, uint8_t green, uint8_t blue),
stroke(color c),
// http://processing.org/reference/noStroke_.html
noStroke(),
text(const char * text, int16_t x, int16_t y),
textWrap(const char * text, int16_t x, int16_t y),
textSize(uint8_t size),
// similar to ellipse() in Processing, but with
// a single radius.
// http://processing.org/reference/ellipse_.html
circle(int16_t x, int16_t y, int16_t r),
point(int16_t x, int16_t y),
line(int16_t x1, int16_t y1, int16_t x2, int16_t y2),
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);
/* TODO
void rectMode(RectMode mode);
void pushStyle();
void popStyle();
*/
#if defined(__SD_H__) // Arduino SD library
PImage loadImage(const char * fileName) { return PImage::loadImage(fileName); }
void image(PImage & img, uint16_t x, uint16_t y);
#endif
protected:
int16_t
WIDTH, HEIGHT; // this is the 'raw' display w/h - never changes
int16_t
_width, _height, // dependent on rotation
cursor_x, cursor_y;
uint16_t
textcolor, textbgcolor;
uint8_t
textsize,
rotation;
boolean
wrap; // If set, 'wrap' text at right edge of display
/*
* Processing-style graphics state
*/
color strokeColor;
bool useStroke;
color fillColor;
bool useFill;
};
#if defined(__SD_H__) // Arduino SD library
#define BUFFPIXEL 20
void Adafruit_GFX::image(PImage & img, uint16_t x, uint16_t y) {
int w, h, row, col;
uint8_t r, g, b;
uint32_t pos = 0;
uint8_t sdbuffer[3*BUFFPIXEL]; // pixel buffer (R+G+B per pixel)
uint8_t buffidx = sizeof(sdbuffer); // Current position in sdbuffer
// Crop area to be loaded
w = img._bmpWidth;
h = img._bmpHeight;
if((x+w-1) >= width()) w = width() - x;
if((y+h-1) >= height()) h = height() - y;
/*
// Set TFT address window to clipped image bounds
setAddrWindow(x, y, x+w-1, y+h-1);
*/
for (row=0; row<h; row++) { // For each scanline...
// Seek to start of scan line. It might seem labor-
// intensive to be doing this on every line, but this
// method covers a lot of gritty details like cropping
// and scanline padding. Also, the seek only takes
// place if the file position actually needs to change
// (avoids a lot of cluster math in SD library).
if(img._flip) // Bitmap is stored bottom-to-top order (normal BMP)
pos = img._bmpImageoffset + (img._bmpHeight - 1 - row) * img._rowSize;
else // Bitmap is stored top-to-bottom
pos = img._bmpImageoffset + row * img._rowSize;
if(img._bmpFile.position() != pos) { // Need seek?
img._bmpFile.seek(pos);
buffidx = sizeof(sdbuffer); // Force buffer reload
}
for (col=0; col<w; col++) { // For each pixel...
// Time to read more pixel data?
if (buffidx >= sizeof(sdbuffer)) { // Indeed
img._bmpFile.read(sdbuffer, sizeof(sdbuffer));
buffidx = 0; // Set index to beginning
}
// Convert pixel from BMP to TFT format, push to display
b = sdbuffer[buffidx++];
g = sdbuffer[buffidx++];
r = sdbuffer[buffidx++];
//pushColor(tft.Color565(r,g,b));
drawPixel(x + col, y + row, newColor(r, g, b));
} // end pixel
} // end scanline
}
// These read 16- and 32-bit types from the SD card file.
// BMP data is stored little-endian, Arduino is little-endian too.
// May need to reverse subscript order if porting elsewhere.
uint16_t PImage::read16(File f) {
uint16_t result;
((uint8_t *)&result)[0] = f.read(); // LSB
((uint8_t *)&result)[1] = f.read(); // MSB
return result;
}
uint32_t PImage::read32(File f) {
uint32_t result;
((uint8_t *)&result)[0] = f.read(); // LSB
((uint8_t *)&result)[1] = f.read();
((uint8_t *)&result)[2] = f.read();
((uint8_t *)&result)[3] = f.read(); // MSB
return result;
}
PImage PImage::loadImage(const char * fileName) {
File bmpFile;
int bmpWidth, bmpHeight; // W+H in pixels
uint8_t bmpDepth; // Bit depth (currently must be 24)
uint32_t bmpImageoffset; // Start of image data in file
uint32_t rowSize; // Not always = bmpWidth; may have padding
bool flip = true; // BMP is stored bottom-to-top
// Open requested file on SD card
if ((bmpFile = SD.open(fileName)) == NULL) {
Serial.print(F("loadImage: file not found: "));
Serial.println(fileName);
return PImage(); // load error
}
// Parse BMP header
if(read16(bmpFile) != 0x4D42) { // BMP signature
Serial.println(F("loadImage: file doesn't look like a BMP"));
return PImage();
}
Serial.print(F("File size: ")); Serial.println(read32(bmpFile));
(void)read32(bmpFile); // Read & ignore creator bytes
bmpImageoffset = read32(bmpFile); // Start of image data
Serial.print(F("Image Offset: ")); Serial.println(bmpImageoffset, DEC);
// Read DIB header
Serial.print(F("Header size: ")); Serial.println(read32(bmpFile));
bmpWidth = read32(bmpFile);
bmpHeight = read32(bmpFile);
if(read16(bmpFile) != 1) { // # planes -- must be '1'
Serial.println(F("loadImage: invalid n. of planes"));
return PImage();
}
bmpDepth = read16(bmpFile); // bits per pixel
Serial.print(F("Bit Depth: ")); Serial.println(bmpDepth);
if((bmpDepth != 24) || (read32(bmpFile) != 0)) { // 0 = uncompressed {
Serial.println(F("loadImage: invalid pixel format"));
return PImage();
}
Serial.print(F("Image size: "));
Serial.print(bmpWidth);
Serial.print('x');
Serial.println(bmpHeight);
// BMP rows are padded (if needed) to 4-byte boundary
rowSize = (bmpWidth * 3 + 3) & ~3;
// If bmpHeight is negative, image is in top-down order.
// This is not canon but has been observed in the wild.
if(bmpHeight < 0) {
bmpHeight = -bmpHeight;
flip = false;
}
return PImage(bmpFile, bmpWidth, bmpHeight, bmpDepth, bmpImageoffset, rowSize, flip);
}
#endif
#endif // _ADAFRUIT_GFX_H

View File

@@ -0,0 +1,751 @@
/***************************************************
This is a library for the Adafruit 1.8" SPI display.
This library works with the Adafruit 1.8" TFT Breakout w/SD card
----> http://www.adafruit.com/products/358
as well as Adafruit raw 1.8" TFT display
----> http://www.adafruit.com/products/618
Check out the links above for our tutorials and wiring diagrams
These displays use SPI to communicate, 4 or 5 pins are required to
interface (RST is optional)
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
Written by Limor Fried/Ladyada for Adafruit Industries.
MIT license, all text above must be included in any redistribution
****************************************************/
#include "Adafruit_ST7735.h"
#include <avr/pgmspace.h>
#include <limits.h>
#include "pins_arduino.h"
#include "wiring_private.h"
#include <SPI.h>
inline uint16_t swapcolor(uint16_t x) {
return (x << 11) | (x & 0x07E0) | (x >> 11);
}
// Constructor when using software SPI. All output pins are configurable.
Adafruit_ST7735::Adafruit_ST7735(uint8_t cs, uint8_t rs, uint8_t sid,
uint8_t sclk, uint8_t rst) : Adafruit_GFX(ST7735_TFTWIDTH, ST7735_TFTHEIGHT)
{
_cs = cs;
_rs = rs;
_sid = sid;
_sclk = sclk;
_rst = rst;
hwSPI = false;
}
// Constructor when using hardware SPI. Faster, but must use SPI pins
// specific to each board type (e.g. 11,13 for Uno, 51,52 for Mega, etc.)
Adafruit_ST7735::Adafruit_ST7735(uint8_t cs, uint8_t rs, uint8_t rst) :
Adafruit_GFX(ST7735_TFTWIDTH, ST7735_TFTHEIGHT)
{
_cs = cs;
_rs = rs;
_rst = rst;
hwSPI = true;
_sid = _sclk = 0;
}
inline void Adafruit_ST7735::spiwrite(uint8_t c) {
//Serial.println(c, HEX);
if (hwSPI) {
SPI.transfer(c);
} else {
// Fast SPI bitbang swiped from LPD8806 library
for(uint8_t bit = 0x80; bit; bit >>= 1) {
if(c & bit) *dataport |= datapinmask;
else *dataport &= ~datapinmask;
*clkport |= clkpinmask;
*clkport &= ~clkpinmask;
}
}
}
void Adafruit_ST7735::writecommand(uint8_t c) {
#ifdef SPI_HAS_TRANSACTION
if (hwSPI) SPI.beginTransaction(spisettings);
#endif
#ifdef __ARDUINO_ARC__
digitalWrite(_rs, LOW);
#else
*rsport &= ~rspinmask;
#endif
*csport &= ~cspinmask;
//Serial.print("C ");
spiwrite(c);
*csport |= cspinmask;
#ifdef SPI_HAS_TRANSACTION
if (hwSPI) SPI.endTransaction();
#endif
}
void Adafruit_ST7735::writedata(uint8_t c) {
#ifdef SPI_HAS_TRANSACTION
if (hwSPI) SPI.beginTransaction(spisettings);
#endif
#ifdef __ARDUINO_ARC__
digitalWrite(_rs, HIGH);
#else
*rsport |= rspinmask;
#endif
*csport &= ~cspinmask;
//Serial.print("D ");
spiwrite(c);
*csport |= cspinmask;
#ifdef SPI_HAS_TRANSACTION
if (hwSPI) SPI.endTransaction();
#endif
}
// Rather than a bazillion writecommand() and writedata() calls, screen
// initialization commands and arguments are organized in these tables
// stored in PROGMEM. The table may look bulky, but that's mostly the
// formatting -- storage-wise this is hundreds of bytes more compact
// than the equivalent code. Companion function follows.
#define DELAY 0x80
PROGMEM const static unsigned char
Bcmd[] = { // Initialization commands for 7735B screens
18, // 18 commands in list:
ST7735_SWRESET, DELAY, // 1: Software reset, no args, w/delay
50, // 50 ms delay
ST7735_SLPOUT , DELAY, // 2: Out of sleep mode, no args, w/delay
255, // 255 = 500 ms delay
ST7735_COLMOD , 1+DELAY, // 3: Set color mode, 1 arg + delay:
0x05, // 16-bit color
10, // 10 ms delay
ST7735_FRMCTR1, 3+DELAY, // 4: Frame rate control, 3 args + delay:
0x00, // fastest refresh
0x06, // 6 lines front porch
0x03, // 3 lines back porch
10, // 10 ms delay
ST7735_MADCTL , 1 , // 5: Memory access ctrl (directions), 1 arg:
0x08, // Row addr/col addr, bottom to top refresh
ST7735_DISSET5, 2 , // 6: Display settings #5, 2 args, no delay:
0x15, // 1 clk cycle nonoverlap, 2 cycle gate
// rise, 3 cycle osc equalize
0x02, // Fix on VTL
ST7735_INVCTR , 1 , // 7: Display inversion control, 1 arg:
0x0, // Line inversion
ST7735_PWCTR1 , 2+DELAY, // 8: Power control, 2 args + delay:
0x02, // GVDD = 4.7V
0x70, // 1.0uA
10, // 10 ms delay
ST7735_PWCTR2 , 1 , // 9: Power control, 1 arg, no delay:
0x05, // VGH = 14.7V, VGL = -7.35V
ST7735_PWCTR3 , 2 , // 10: Power control, 2 args, no delay:
0x01, // Opamp current small
0x02, // Boost frequency
ST7735_VMCTR1 , 2+DELAY, // 11: Power control, 2 args + delay:
0x3C, // VCOMH = 4V
0x38, // VCOML = -1.1V
10, // 10 ms delay
ST7735_PWCTR6 , 2 , // 12: Power control, 2 args, no delay:
0x11, 0x15,
ST7735_GMCTRP1,16 , // 13: Magical unicorn dust, 16 args, no delay:
0x09, 0x16, 0x09, 0x20, // (seriously though, not sure what
0x21, 0x1B, 0x13, 0x19, // these config values represent)
0x17, 0x15, 0x1E, 0x2B,
0x04, 0x05, 0x02, 0x0E,
ST7735_GMCTRN1,16+DELAY, // 14: Sparkles and rainbows, 16 args + delay:
0x0B, 0x14, 0x08, 0x1E, // (ditto)
0x22, 0x1D, 0x18, 0x1E,
0x1B, 0x1A, 0x24, 0x2B,
0x06, 0x06, 0x02, 0x0F,
10, // 10 ms delay
ST7735_CASET , 4 , // 15: Column addr set, 4 args, no delay:
0x00, 0x02, // XSTART = 2
0x00, 0x81, // XEND = 129
ST7735_RASET , 4 , // 16: Row addr set, 4 args, no delay:
0x00, 0x02, // XSTART = 1
0x00, 0x81, // XEND = 160
ST7735_NORON , DELAY, // 17: Normal display on, no args, w/delay
10, // 10 ms delay
ST7735_DISPON , DELAY, // 18: Main screen turn on, no args, w/delay
255 }, // 255 = 500 ms delay
Rcmd1[] = { // Init for 7735R, part 1 (red or green tab)
15, // 15 commands in list:
ST7735_SWRESET, DELAY, // 1: Software reset, 0 args, w/delay
150, // 150 ms delay
ST7735_SLPOUT , DELAY, // 2: Out of sleep mode, 0 args, w/delay
255, // 500 ms delay
ST7735_FRMCTR1, 3 , // 3: Frame rate ctrl - normal mode, 3 args:
0x01, 0x2C, 0x2D, // Rate = fosc/(1x2+40) * (LINE+2C+2D)
ST7735_FRMCTR2, 3 , // 4: Frame rate control - idle mode, 3 args:
0x01, 0x2C, 0x2D, // Rate = fosc/(1x2+40) * (LINE+2C+2D)
ST7735_FRMCTR3, 6 , // 5: Frame rate ctrl - partial mode, 6 args:
0x01, 0x2C, 0x2D, // Dot inversion mode
0x01, 0x2C, 0x2D, // Line inversion mode
ST7735_INVCTR , 1 , // 6: Display inversion ctrl, 1 arg, no delay:
0x07, // No inversion
ST7735_PWCTR1 , 3 , // 7: Power control, 3 args, no delay:
0xA2,
0x02, // -4.6V
0x84, // AUTO mode
ST7735_PWCTR2 , 1 , // 8: Power control, 1 arg, no delay:
0xC5, // VGH25 = 2.4C VGSEL = -10 VGH = 3 * AVDD
ST7735_PWCTR3 , 2 , // 9: Power control, 2 args, no delay:
0x0A, // Opamp current small
0x00, // Boost frequency
ST7735_PWCTR4 , 2 , // 10: Power control, 2 args, no delay:
0x8A, // BCLK/2, Opamp current small & Medium low
0x2A,
ST7735_PWCTR5 , 2 , // 11: Power control, 2 args, no delay:
0x8A, 0xEE,
ST7735_VMCTR1 , 1 , // 12: Power control, 1 arg, no delay:
0x0E,
ST7735_INVOFF , 0 , // 13: Don't invert display, no args, no delay
ST7735_MADCTL , 1 , // 14: Memory access control (directions), 1 arg:
0xC8, // row addr/col addr, bottom to top refresh
ST7735_COLMOD , 1 , // 15: set color mode, 1 arg, no delay:
0x05 }, // 16-bit color
Rcmd2green[] = { // Init for 7735R, part 2 (green tab only)
2, // 2 commands in list:
ST7735_CASET , 4 , // 1: Column addr set, 4 args, no delay:
0x00, 0x02, // XSTART = 0
0x00, 0x7F+0x02, // XEND = 127
ST7735_RASET , 4 , // 2: Row addr set, 4 args, no delay:
0x00, 0x01, // XSTART = 0
0x00, 0x9F+0x01 }, // XEND = 159
Rcmd2red[] = { // Init for 7735R, part 2 (red tab only)
2, // 2 commands in list:
ST7735_CASET , 4 , // 1: Column addr set, 4 args, no delay:
0x00, 0x00, // XSTART = 0
0x00, 0x7F, // XEND = 127
ST7735_RASET , 4 , // 2: Row addr set, 4 args, no delay:
0x00, 0x00, // XSTART = 0
0x00, 0x9F }, // XEND = 159
Rcmd3[] = { // Init for 7735R, part 3 (red or green tab)
4, // 4 commands in list:
ST7735_GMCTRP1, 16 , // 1: Magical unicorn dust, 16 args, no delay:
0x02, 0x1c, 0x07, 0x12,
0x37, 0x32, 0x29, 0x2d,
0x29, 0x25, 0x2B, 0x39,
0x00, 0x01, 0x03, 0x10,
ST7735_GMCTRN1, 16 , // 2: Sparkles and rainbows, 16 args, no delay:
0x03, 0x1d, 0x07, 0x06,
0x2E, 0x2C, 0x29, 0x2D,
0x2E, 0x2E, 0x37, 0x3F,
0x00, 0x00, 0x02, 0x10,
ST7735_NORON , DELAY, // 3: Normal display on, no args, w/delay
10, // 10 ms delay
ST7735_DISPON , DELAY, // 4: Main screen turn on, no args w/delay
100 }, // 100 ms delay
Gcmd[] = { // Initialization commands for 7735B screens
19, // 18 commands in list:
ST7735_SWRESET, DELAY, // 1: Software reset, no args, w/delay
50, // 50 ms delay
ST7735_SLPOUT , DELAY, // 2: Out of sleep mode, no args, w/delay
100, // 255 = 500 ms delay
0x26 , 1, // 3: Set default gamma
0x04, // 16-bit color
0xb1, 2, // 4: Frame Rate
0x0b,
0x14,
0xc0, 2, // 5: VRH1[4:0] & VC[2:0]
0x08,
0x00,
0xc1, 1, // 6: BT[2:0]
0x05,
0xc5, 2, // 7: VMH[6:0] & VML[6:0]
0x41,
0x30,
0xc7, 1, // 8: LCD Driving control
0xc1,
0xEC, 1, // 9: Set pumping color freq
0x1b,
0x3a , 1 + DELAY, // 10: Set color format
0x55, // 16-bit color
100,
0x2a, 4, // 11: Set Column Address
0x00,
0x00,
0x00,
0x7f,
0x2b, 4, // 12: Set Page Address
0x00,
0x00,
0x00,
0x9f,
0x36, 1, // 12+1: Set Scanning Direction
0xc8,
0xb7, 1, // 14: Set Source Output Direciton
0x00,
0xf2, 1, // 15: Enable Gamma bit
0x00,
0xe0, 15 + DELAY, // 16: magic
0x28, 0x24, 0x22, 0x31,
0x2b, 0x0e, 0x53, 0xa5,
0x42, 0x16, 0x18, 0x12,
0x1a, 0x14, 0x03,
50,
0xe1, 15 + DELAY, // 17: more magic
0x17, 0x1b, 0x1d, 0x0e,
0x14, 0x11, 0x2c, 0xa5,
0x3d, 0x09, 0x27, 0x2d,
0x25, 0x2b, 0x3c,
50,
ST7735_NORON , DELAY, // 17: Normal display on, no args, w/delay
10, // 10 ms delay
ST7735_DISPON , DELAY, // 18: Main screen turn on, no args, w/delay
255 }; // 255 = 500 ms delay
// Companion code to the above tables. Reads and issues
// a series of LCD commands stored in PROGMEM byte array.
void Adafruit_ST7735::commandList(const uint8_t *addr) {
uint8_t numCommands, numArgs;
uint16_t ms;
numCommands = pgm_read_byte(addr++); // Number of commands to follow
while(numCommands--) { // For each command...
writecommand(pgm_read_byte(addr++)); // Read, issue command
numArgs = pgm_read_byte(addr++); // Number of args to follow
ms = numArgs & DELAY; // If hibit set, delay follows args
numArgs &= ~DELAY; // Mask out delay bit
while(numArgs--) { // For each argument...
writedata(pgm_read_byte(addr++)); // Read, issue argument
}
if(ms) {
ms = pgm_read_byte(addr++); // Read post-command delay time (ms)
if(ms == 255) ms = 500; // If 255, delay for 500 ms
delay(ms);
}
}
}
// Initialization code common to both 'B' and 'R' type displays
void Adafruit_ST7735::commonInit(const uint8_t *cmdList) {
colstart = rowstart = 0; // May be overridden in init func
pinMode(_rs, OUTPUT);
pinMode(_cs, OUTPUT);
csport = portOutputRegister(digitalPinToPort(_cs));
cspinmask = digitalPinToBitMask(_cs);
rsport = portOutputRegister(digitalPinToPort(_rs));
rspinmask = digitalPinToBitMask(_rs);
if(hwSPI) { // Using hardware SPI
SPI.begin();
#ifdef SPI_HAS_TRANSACTION
spisettings = SPISettings(4000000L, MSBFIRST, SPI_MODE0);
#else
#if defined(ARDUINO_ARCH_SAM)
SPI.setClockDivider(24); // 4 MHz (half speed)
#else
SPI.setClockDivider(SPI_CLOCK_DIV4); // 4 MHz (half speed)
#endif
SPI.setBitOrder(MSBFIRST);
SPI.setDataMode(SPI_MODE0);
#endif // SPI_HAS_TRANSACTION
} else {
pinMode(_sclk, OUTPUT);
pinMode(_sid , OUTPUT);
clkport = portOutputRegister(digitalPinToPort(_sclk));
clkpinmask = digitalPinToBitMask(_sclk);
dataport = portOutputRegister(digitalPinToPort(_sid));
datapinmask = digitalPinToBitMask(_sid);
*clkport &= ~clkpinmask;
*dataport &= ~datapinmask;
}
// toggle RST low to reset; CS low so it'll listen to us
*csport &= ~cspinmask;
if (_rst) {
pinMode(_rst, OUTPUT);
digitalWrite(_rst, HIGH);
delay(500);
digitalWrite(_rst, LOW);
delay(500);
digitalWrite(_rst, HIGH);
delay(500);
}
if(cmdList) commandList(cmdList);
}
// Initialization for ST7735B screens
void Adafruit_ST7735::initB(void) {
commonInit(Bcmd);
}
// Initialization for ST7735B screens
void Adafruit_ST7735::initG(void) {
commonInit(Gcmd);
}
// Initialization for ST7735R screens (green or red tabs)
void Adafruit_ST7735::initR(uint8_t options) {
commonInit(Rcmd1);
if(options == INITR_GREENTAB) {
commandList(Rcmd2green);
colstart = 2;
rowstart = 1;
} else {
// colstart, rowstart left at default '0' values
commandList(Rcmd2red);
}
commandList(Rcmd3);
tabcolor = options;
}
void Adafruit_ST7735::setAddrWindow(uint8_t x0, uint8_t y0, uint8_t x1,
uint8_t y1) {
writecommand(ST7735_CASET); // Column addr set
writedata(0x00);
writedata(x0+colstart); // XSTART
writedata(0x00);
writedata(x1+colstart); // XEND
writecommand(ST7735_RASET); // Row addr set
writedata(0x00);
writedata(y0+rowstart); // YSTART
writedata(0x00);
writedata(y1+rowstart); // YEND
writecommand(ST7735_RAMWR); // write to RAM
}
void Adafruit_ST7735::pushColor(uint16_t color) {
#ifdef SPI_HAS_TRANSACTION
if (hwSPI) SPI.beginTransaction(spisettings);
#endif
#ifdef __ARDUINO_ARC__
digitalWrite(_rs, HIGH);
#else
*rsport |= rspinmask;
#endif
*csport &= ~cspinmask;
if (tabcolor == INITR_BLACKTAB) color = swapcolor(color);
spiwrite(color >> 8);
spiwrite(color);
*csport |= cspinmask;
#ifdef SPI_HAS_TRANSACTION
if (hwSPI) SPI.endTransaction();
#endif
}
void Adafruit_ST7735::drawPixel(int16_t x, int16_t y, uint16_t color) {
if((x < 0) ||(x >= _width) || (y < 0) || (y >= _height)) return;
setAddrWindow(x,y,x+1,y+1);
#ifdef SPI_HAS_TRANSACTION
if (hwSPI) SPI.beginTransaction(spisettings);
#endif
#ifdef __ARDUINO_ARC__
digitalWrite(_rs, HIGH);
#else
*rsport |= rspinmask;
#endif
*csport &= ~cspinmask;
if (tabcolor == INITR_BLACKTAB) color = swapcolor(color);
spiwrite(color >> 8);
spiwrite(color);
*csport |= cspinmask;
#ifdef SPI_HAS_TRANSACTION
if (hwSPI) SPI.endTransaction();
#endif
}
void Adafruit_ST7735::drawFastVLine(int16_t x, int16_t y, int16_t h,
uint16_t color) {
// Rudimentary clipping
if((x >= _width) || (y >= _height)) return;
if((y+h-1) >= _height) h = _height-y;
setAddrWindow(x, y, x, y+h-1);
if (tabcolor == INITR_BLACKTAB) color = swapcolor(color);
uint8_t hi = color >> 8, lo = color;
#ifdef SPI_HAS_TRANSACTION
if (hwSPI) SPI.beginTransaction(spisettings);
#endif
#ifdef __ARDUINO_ARC__
digitalWrite(_rs, HIGH);
#else
*rsport |= rspinmask;
#endif
*csport &= ~cspinmask;
while (h--) {
spiwrite(hi);
spiwrite(lo);
}
*csport |= cspinmask;
#ifdef SPI_HAS_TRANSACTION
if (hwSPI) SPI.endTransaction();
#endif
}
void Adafruit_ST7735::drawFastHLine(int16_t x, int16_t y, int16_t w,
uint16_t color) {
// Rudimentary clipping
if((x >= _width) || (y >= _height)) return;
if((x+w-1) >= _width) w = _width-x;
setAddrWindow(x, y, x+w-1, y);
if (tabcolor == INITR_BLACKTAB) color = swapcolor(color);
uint8_t hi = color >> 8, lo = color;
#ifdef SPI_HAS_TRANSACTION
if (hwSPI) SPI.beginTransaction(spisettings);
#endif
#ifdef __ARDUINO_ARC__
digitalWrite(_rs, HIGH);
#else
*rsport |= rspinmask;
#endif
*csport &= ~cspinmask;
while (w--) {
spiwrite(hi);
spiwrite(lo);
}
*csport |= cspinmask;
#ifdef SPI_HAS_TRANSACTION
if (hwSPI) SPI.endTransaction();
#endif
}
void Adafruit_ST7735::fillScreen(uint16_t color) {
fillRect(0, 0, _width, _height, color);
}
// fill a rectangle
void Adafruit_ST7735::fillRect(int16_t x, int16_t y, int16_t w, int16_t h,
uint16_t color) {
// rudimentary clipping (drawChar w/big text requires this)
if((x >= _width) || (y >= _height)) return;
if((x + w - 1) >= _width) w = _width - x;
if((y + h - 1) >= _height) h = _height - y;
if (tabcolor == INITR_BLACKTAB) color = swapcolor(color);
setAddrWindow(x, y, x+w-1, y+h-1);
uint8_t hi = color >> 8, lo = color;
#ifdef SPI_HAS_TRANSACTION
if (hwSPI) SPI.beginTransaction(spisettings);
#endif
#ifdef __ARDUINO_ARC__
digitalWrite(_rs, HIGH);
#else
*rsport |= rspinmask;
#endif
*csport &= ~cspinmask;
for(y=h; y>0; y--) {
for(x=w; x>0; x--) {
spiwrite(hi);
spiwrite(lo);
}
}
*csport |= cspinmask;
#ifdef SPI_HAS_TRANSACTION
if (hwSPI) SPI.endTransaction();
#endif
}
#define MADCTL_MY 0x80
#define MADCTL_MX 0x40
#define MADCTL_MV 0x20
#define MADCTL_ML 0x10
#define MADCTL_RGB 0x08
#define MADCTL_MH 0x04
void Adafruit_ST7735::setRotation(uint8_t m) {
writecommand(ST7735_MADCTL);
rotation = m % 4; // can't be higher than 3
switch (rotation) {
case 0:
writedata(MADCTL_MX | MADCTL_MY | MADCTL_RGB);
_width = ST7735_TFTWIDTH;
_height = ST7735_TFTHEIGHT;
break;
case 1:
writedata(MADCTL_MY | MADCTL_MV | MADCTL_RGB);
_width = ST7735_TFTHEIGHT;
_height = ST7735_TFTWIDTH;
break;
case 2:
writedata(MADCTL_RGB);
_width = ST7735_TFTWIDTH;
_height = ST7735_TFTHEIGHT;
break;
case 3:
writedata(MADCTL_MX | MADCTL_MV | MADCTL_RGB);
_width = ST7735_TFTHEIGHT;
_height = ST7735_TFTWIDTH;
break;
}
}
void Adafruit_ST7735::invertDisplay(boolean i) {
writecommand(i ? ST7735_INVON : ST7735_INVOFF);
}
////////// stuff not actively being used, but kept for posterity
/*
uint8_t Adafruit_ST7735::spiread(void) {
uint8_t r = 0;
if (_sid > 0) {
r = shiftIn(_sid, _sclk, MSBFIRST);
} else {
//SID_DDR &= ~_BV(SID);
//int8_t i;
//for (i=7; i>=0; i--) {
// SCLK_PORT &= ~_BV(SCLK);
// r <<= 1;
// r |= (SID_PIN >> SID) & 0x1;
// SCLK_PORT |= _BV(SCLK);
//}
//SID_DDR |= _BV(SID);
}
return r;
}
void Adafruit_ST7735::dummyclock(void) {
if (_sid > 0) {
digitalWrite(_sclk, LOW);
digitalWrite(_sclk, HIGH);
} else {
// SCLK_PORT &= ~_BV(SCLK);
//SCLK_PORT |= _BV(SCLK);
}
}
uint8_t Adafruit_ST7735::readdata(void) {
*portOutputRegister(rsport) |= rspin;
*portOutputRegister(csport) &= ~ cspin;
uint8_t r = spiread();
*portOutputRegister(csport) |= cspin;
return r;
}
uint8_t Adafruit_ST7735::readcommand8(uint8_t c) {
digitalWrite(_rs, LOW);
*portOutputRegister(csport) &= ~ cspin;
spiwrite(c);
digitalWrite(_rs, HIGH);
pinMode(_sid, INPUT); // input!
digitalWrite(_sid, LOW); // low
spiread();
uint8_t r = spiread();
*portOutputRegister(csport) |= cspin;
pinMode(_sid, OUTPUT); // back to output
return r;
}
uint16_t Adafruit_ST7735::readcommand16(uint8_t c) {
digitalWrite(_rs, LOW);
if (_cs)
digitalWrite(_cs, LOW);
spiwrite(c);
pinMode(_sid, INPUT); // input!
uint16_t r = spiread();
r <<= 8;
r |= spiread();
if (_cs)
digitalWrite(_cs, HIGH);
pinMode(_sid, OUTPUT); // back to output
return r;
}
uint32_t Adafruit_ST7735::readcommand32(uint8_t c) {
digitalWrite(_rs, LOW);
if (_cs)
digitalWrite(_cs, LOW);
spiwrite(c);
pinMode(_sid, INPUT); // input!
dummyclock();
dummyclock();
uint32_t r = spiread();
r <<= 8;
r |= spiread();
r <<= 8;
r |= spiread();
r <<= 8;
r |= spiread();
if (_cs)
digitalWrite(_cs, HIGH);
pinMode(_sid, OUTPUT); // back to output
return r;
}
*/

View File

@@ -0,0 +1,156 @@
/***************************************************
This is a library for the Adafruit 1.8" SPI display.
This library works with the Adafruit 1.8" TFT Breakout w/SD card
----> http://www.adafruit.com/products/358
as well as Adafruit raw 1.8" TFT display
----> http://www.adafruit.com/products/618
Check out the links above for our tutorials and wiring diagrams
These displays use SPI to communicate, 4 or 5 pins are required to
interface (RST is optional)
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
Written by Limor Fried/Ladyada for Adafruit Industries.
MIT license, all text above must be included in any redistribution
****************************************************/
#ifndef _ADAFRUIT_ST7735H_
#define _ADAFRUIT_ST7735H_
#if ARDUINO >= 100
#include "Arduino.h"
#include "Print.h"
#else
#include "WProgram.h"
#endif
#include "Adafruit_GFX.h"
#include <SPI.h>
#include <avr/pgmspace.h>
// some flags for initR() :(
#define INITR_GREENTAB 0x0
#define INITR_REDTAB 0x1
#define INITR_BLACKTAB 0x2
#define ST7735_TFTWIDTH 128
#define ST7735_TFTHEIGHT 160
#define ST7735_NOP 0x00
#define ST7735_SWRESET 0x01
#define ST7735_RDDID 0x04
#define ST7735_RDDST 0x09
#define ST7735_SLPIN 0x10
#define ST7735_SLPOUT 0x11
#define ST7735_PTLON 0x12
#define ST7735_NORON 0x13
#define ST7735_INVOFF 0x20
#define ST7735_INVON 0x21
#define ST7735_DISPOFF 0x28
#define ST7735_DISPON 0x29
#define ST7735_CASET 0x2A
#define ST7735_RASET 0x2B
#define ST7735_RAMWR 0x2C
#define ST7735_RAMRD 0x2E
#define ST7735_PTLAR 0x30
#define ST7735_COLMOD 0x3A
#define ST7735_MADCTL 0x36
#define ST7735_FRMCTR1 0xB1
#define ST7735_FRMCTR2 0xB2
#define ST7735_FRMCTR3 0xB3
#define ST7735_INVCTR 0xB4
#define ST7735_DISSET5 0xB6
#define ST7735_PWCTR1 0xC0
#define ST7735_PWCTR2 0xC1
#define ST7735_PWCTR3 0xC2
#define ST7735_PWCTR4 0xC3
#define ST7735_PWCTR5 0xC4
#define ST7735_VMCTR1 0xC5
#define ST7735_RDID1 0xDA
#define ST7735_RDID2 0xDB
#define ST7735_RDID3 0xDC
#define ST7735_RDID4 0xDD
#define ST7735_PWCTR6 0xFC
#define ST7735_GMCTRP1 0xE0
#define ST7735_GMCTRN1 0xE1
// Color definitions
#define ST7735_BLACK 0x0000
#define ST7735_BLUE 0x001F
#define ST7735_RED 0xF800
#define ST7735_GREEN 0x07E0
#define ST7735_CYAN 0x07FF
#define ST7735_MAGENTA 0xF81F
#define ST7735_YELLOW 0xFFE0
#define ST7735_WHITE 0xFFFF
class Adafruit_ST7735 : public Adafruit_GFX {
public:
Adafruit_ST7735(uint8_t CS, uint8_t RS, uint8_t SID, uint8_t SCLK,
uint8_t RST);
Adafruit_ST7735(uint8_t CS, uint8_t RS, uint8_t RST);
void initB(void), // for ST7735B displays
initG(void), // for ILI9163C displays
initR(uint8_t options = INITR_GREENTAB), // for ST7735R
setAddrWindow(uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1),
pushColor(uint16_t color),
fillScreen(uint16_t color),
drawPixel(int16_t x, int16_t y, 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,
uint16_t color),
setRotation(uint8_t r),
invertDisplay(boolean i);
uint16_t Color565(uint8_t r, uint8_t g, uint8_t b) { return newColor(r, g, b);}
/* These are not for current use, 8-bit protocol only!
uint8_t readdata(void),
readcommand8(uint8_t);
uint16_t readcommand16(uint8_t);
uint32_t readcommand32(uint8_t);
void dummyclock(void);
*/
private:
uint8_t tabcolor;
void spiwrite(uint8_t),
writecommand(uint8_t c),
writedata(uint8_t d),
commandList(const uint8_t *addr),
commonInit(const uint8_t *cmdList);
//uint8_t spiread(void);
boolean hwSPI;
#ifdef SPI_HAS_TRANSACTION
SPISettings spisettings;
#endif
#if defined(ARDUINO_ARCH_SAM) || defined(__ARDUINO_ARC__) || \
defined(ARDUINO_ARCH_STM32)
volatile uint32_t *dataport, *clkport, *csport, *rsport;
uint32_t _cs, _rs, _rst, _sid, _sclk,
datapinmask, clkpinmask, cspinmask, rspinmask,
colstart, rowstart; // some displays need this changed
#else
volatile uint8_t *dataport, *clkport, *csport, *rsport;
uint8_t _cs, _rs, _rst, _sid, _sclk,
datapinmask, clkpinmask, cspinmask, rspinmask,
colstart, rowstart; // some displays need this changed
#endif
};
#endif

View File

@@ -0,0 +1,65 @@
#ifndef _PIMAGE_H
#define _PIMAGE_H
class Adafruit_GFX;
#if defined(__SD_H__) // Arduino SD library
/// This class mimics Processing's PImage, but with fewer
/// capabilities. It allows an image stored in the SD card to be
/// drawn to the display.
/// @author Enrico Gueli <enrico.gueli@gmail.com>
class PImage {
public:
PImage() :
_valid(false),
_bmpWidth(0),
_bmpHeight(0) { }
void draw(Adafruit_GFX & glcd, int16_t x, int16_t y);
static PImage loadImage(const char * fileName);
void close() { _bmpFile.close(); }
bool isValid() { return _valid; }
int width() { return _bmpWidth; }
int height() { return _bmpHeight; }
private:
friend class Adafruit_GFX;
File _bmpFile;
int _bmpWidth, _bmpHeight; // W+H in pixels
uint8_t _bmpDepth; // Bit depth (currently must be 24)
uint32_t _bmpImageoffset; // Start of image data in file
uint32_t _rowSize; // Not always = bmpWidth; may have padding
bool _flip;
bool _valid;
PImage(File & bmpFile, int bmpWidth, int bmpHeight, uint8_t bmpDepth, uint32_t bmpImageoffset, uint32_t rowSize, bool flip) :
_bmpFile(bmpFile),
_bmpWidth(bmpWidth),
_bmpHeight(bmpHeight),
_bmpDepth(bmpDepth),
_bmpImageoffset(bmpImageoffset),
_rowSize(rowSize),
_flip(flip),
_valid(true) // since Adafruit_GFX is friend, we could just let it write the variables and save some CPU cycles
{ }
static uint16_t read16(File f);
static uint32_t read32(File f);
// TODO close the file in ~PImage and PImage(const PImage&)
};
#endif
#endif // _PIMAGE_H

View File

@@ -0,0 +1,269 @@
#if !defined(ARDUINO_ARCH_SAM) && !defined(__ARDUINO_ARC__) && \
!defined(ARDUINO_ARCH_STM32)
#include <avr/io.h>
#endif
#include <avr/pgmspace.h>
#ifndef FONT5X7_H
#define FONT5X7_H
// standard ascii 5x7 font
static const unsigned char font[] PROGMEM = {
0x00, 0x00, 0x00, 0x00, 0x00,
0x3E, 0x5B, 0x4F, 0x5B, 0x3E,
0x3E, 0x6B, 0x4F, 0x6B, 0x3E,
0x1C, 0x3E, 0x7C, 0x3E, 0x1C,
0x18, 0x3C, 0x7E, 0x3C, 0x18,
0x1C, 0x57, 0x7D, 0x57, 0x1C,
0x1C, 0x5E, 0x7F, 0x5E, 0x1C,
0x00, 0x18, 0x3C, 0x18, 0x00,
0xFF, 0xE7, 0xC3, 0xE7, 0xFF,
0x00, 0x18, 0x24, 0x18, 0x00,
0xFF, 0xE7, 0xDB, 0xE7, 0xFF,
0x30, 0x48, 0x3A, 0x06, 0x0E,
0x26, 0x29, 0x79, 0x29, 0x26,
0x40, 0x7F, 0x05, 0x05, 0x07,
0x40, 0x7F, 0x05, 0x25, 0x3F,
0x5A, 0x3C, 0xE7, 0x3C, 0x5A,
0x7F, 0x3E, 0x1C, 0x1C, 0x08,
0x08, 0x1C, 0x1C, 0x3E, 0x7F,
0x14, 0x22, 0x7F, 0x22, 0x14,
0x5F, 0x5F, 0x00, 0x5F, 0x5F,
0x06, 0x09, 0x7F, 0x01, 0x7F,
0x00, 0x66, 0x89, 0x95, 0x6A,
0x60, 0x60, 0x60, 0x60, 0x60,
0x94, 0xA2, 0xFF, 0xA2, 0x94,
0x08, 0x04, 0x7E, 0x04, 0x08,
0x10, 0x20, 0x7E, 0x20, 0x10,
0x08, 0x08, 0x2A, 0x1C, 0x08,
0x08, 0x1C, 0x2A, 0x08, 0x08,
0x1E, 0x10, 0x10, 0x10, 0x10,
0x0C, 0x1E, 0x0C, 0x1E, 0x0C,
0x30, 0x38, 0x3E, 0x38, 0x30,
0x06, 0x0E, 0x3E, 0x0E, 0x06,
0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x5F, 0x00, 0x00,
0x00, 0x07, 0x00, 0x07, 0x00,
0x14, 0x7F, 0x14, 0x7F, 0x14,
0x24, 0x2A, 0x7F, 0x2A, 0x12,
0x23, 0x13, 0x08, 0x64, 0x62,
0x36, 0x49, 0x56, 0x20, 0x50,
0x00, 0x08, 0x07, 0x03, 0x00,
0x00, 0x1C, 0x22, 0x41, 0x00,
0x00, 0x41, 0x22, 0x1C, 0x00,
0x2A, 0x1C, 0x7F, 0x1C, 0x2A,
0x08, 0x08, 0x3E, 0x08, 0x08,
0x00, 0x80, 0x70, 0x30, 0x00,
0x08, 0x08, 0x08, 0x08, 0x08,
0x00, 0x00, 0x60, 0x60, 0x00,
0x20, 0x10, 0x08, 0x04, 0x02,
0x3E, 0x51, 0x49, 0x45, 0x3E,
0x00, 0x42, 0x7F, 0x40, 0x00,
0x72, 0x49, 0x49, 0x49, 0x46,
0x21, 0x41, 0x49, 0x4D, 0x33,
0x18, 0x14, 0x12, 0x7F, 0x10,
0x27, 0x45, 0x45, 0x45, 0x39,
0x3C, 0x4A, 0x49, 0x49, 0x31,
0x41, 0x21, 0x11, 0x09, 0x07,
0x36, 0x49, 0x49, 0x49, 0x36,
0x46, 0x49, 0x49, 0x29, 0x1E,
0x00, 0x00, 0x14, 0x00, 0x00,
0x00, 0x40, 0x34, 0x00, 0x00,
0x00, 0x08, 0x14, 0x22, 0x41,
0x14, 0x14, 0x14, 0x14, 0x14,
0x00, 0x41, 0x22, 0x14, 0x08,
0x02, 0x01, 0x59, 0x09, 0x06,
0x3E, 0x41, 0x5D, 0x59, 0x4E,
0x7C, 0x12, 0x11, 0x12, 0x7C,
0x7F, 0x49, 0x49, 0x49, 0x36,
0x3E, 0x41, 0x41, 0x41, 0x22,
0x7F, 0x41, 0x41, 0x41, 0x3E,
0x7F, 0x49, 0x49, 0x49, 0x41,
0x7F, 0x09, 0x09, 0x09, 0x01,
0x3E, 0x41, 0x41, 0x51, 0x73,
0x7F, 0x08, 0x08, 0x08, 0x7F,
0x00, 0x41, 0x7F, 0x41, 0x00,
0x20, 0x40, 0x41, 0x3F, 0x01,
0x7F, 0x08, 0x14, 0x22, 0x41,
0x7F, 0x40, 0x40, 0x40, 0x40,
0x7F, 0x02, 0x1C, 0x02, 0x7F,
0x7F, 0x04, 0x08, 0x10, 0x7F,
0x3E, 0x41, 0x41, 0x41, 0x3E,
0x7F, 0x09, 0x09, 0x09, 0x06,
0x3E, 0x41, 0x51, 0x21, 0x5E,
0x7F, 0x09, 0x19, 0x29, 0x46,
0x26, 0x49, 0x49, 0x49, 0x32,
0x03, 0x01, 0x7F, 0x01, 0x03,
0x3F, 0x40, 0x40, 0x40, 0x3F,
0x1F, 0x20, 0x40, 0x20, 0x1F,
0x3F, 0x40, 0x38, 0x40, 0x3F,
0x63, 0x14, 0x08, 0x14, 0x63,
0x03, 0x04, 0x78, 0x04, 0x03,
0x61, 0x59, 0x49, 0x4D, 0x43,
0x00, 0x7F, 0x41, 0x41, 0x41,
0x02, 0x04, 0x08, 0x10, 0x20,
0x00, 0x41, 0x41, 0x41, 0x7F,
0x04, 0x02, 0x01, 0x02, 0x04,
0x40, 0x40, 0x40, 0x40, 0x40,
0x00, 0x03, 0x07, 0x08, 0x00,
0x20, 0x54, 0x54, 0x78, 0x40,
0x7F, 0x28, 0x44, 0x44, 0x38,
0x38, 0x44, 0x44, 0x44, 0x28,
0x38, 0x44, 0x44, 0x28, 0x7F,
0x38, 0x54, 0x54, 0x54, 0x18,
0x00, 0x08, 0x7E, 0x09, 0x02,
0x18, 0xA4, 0xA4, 0x9C, 0x78,
0x7F, 0x08, 0x04, 0x04, 0x78,
0x00, 0x44, 0x7D, 0x40, 0x00,
0x20, 0x40, 0x40, 0x3D, 0x00,
0x7F, 0x10, 0x28, 0x44, 0x00,
0x00, 0x41, 0x7F, 0x40, 0x00,
0x7C, 0x04, 0x78, 0x04, 0x78,
0x7C, 0x08, 0x04, 0x04, 0x78,
0x38, 0x44, 0x44, 0x44, 0x38,
0xFC, 0x18, 0x24, 0x24, 0x18,
0x18, 0x24, 0x24, 0x18, 0xFC,
0x7C, 0x08, 0x04, 0x04, 0x08,
0x48, 0x54, 0x54, 0x54, 0x24,
0x04, 0x04, 0x3F, 0x44, 0x24,
0x3C, 0x40, 0x40, 0x20, 0x7C,
0x1C, 0x20, 0x40, 0x20, 0x1C,
0x3C, 0x40, 0x30, 0x40, 0x3C,
0x44, 0x28, 0x10, 0x28, 0x44,
0x4C, 0x90, 0x90, 0x90, 0x7C,
0x44, 0x64, 0x54, 0x4C, 0x44,
0x00, 0x08, 0x36, 0x41, 0x00,
0x00, 0x00, 0x77, 0x00, 0x00,
0x00, 0x41, 0x36, 0x08, 0x00,
0x02, 0x01, 0x02, 0x04, 0x02,
0x3C, 0x26, 0x23, 0x26, 0x3C,
0x1E, 0xA1, 0xA1, 0x61, 0x12,
0x3A, 0x40, 0x40, 0x20, 0x7A,
0x38, 0x54, 0x54, 0x55, 0x59,
0x21, 0x55, 0x55, 0x79, 0x41,
0x21, 0x54, 0x54, 0x78, 0x41,
0x21, 0x55, 0x54, 0x78, 0x40,
0x20, 0x54, 0x55, 0x79, 0x40,
0x0C, 0x1E, 0x52, 0x72, 0x12,
0x39, 0x55, 0x55, 0x55, 0x59,
0x39, 0x54, 0x54, 0x54, 0x59,
0x39, 0x55, 0x54, 0x54, 0x58,
0x00, 0x00, 0x45, 0x7C, 0x41,
0x00, 0x02, 0x45, 0x7D, 0x42,
0x00, 0x01, 0x45, 0x7C, 0x40,
0xF0, 0x29, 0x24, 0x29, 0xF0,
0xF0, 0x28, 0x25, 0x28, 0xF0,
0x7C, 0x54, 0x55, 0x45, 0x00,
0x20, 0x54, 0x54, 0x7C, 0x54,
0x7C, 0x0A, 0x09, 0x7F, 0x49,
0x32, 0x49, 0x49, 0x49, 0x32,
0x32, 0x48, 0x48, 0x48, 0x32,
0x32, 0x4A, 0x48, 0x48, 0x30,
0x3A, 0x41, 0x41, 0x21, 0x7A,
0x3A, 0x42, 0x40, 0x20, 0x78,
0x00, 0x9D, 0xA0, 0xA0, 0x7D,
0x39, 0x44, 0x44, 0x44, 0x39,
0x3D, 0x40, 0x40, 0x40, 0x3D,
0x3C, 0x24, 0xFF, 0x24, 0x24,
0x48, 0x7E, 0x49, 0x43, 0x66,
0x2B, 0x2F, 0xFC, 0x2F, 0x2B,
0xFF, 0x09, 0x29, 0xF6, 0x20,
0xC0, 0x88, 0x7E, 0x09, 0x03,
0x20, 0x54, 0x54, 0x79, 0x41,
0x00, 0x00, 0x44, 0x7D, 0x41,
0x30, 0x48, 0x48, 0x4A, 0x32,
0x38, 0x40, 0x40, 0x22, 0x7A,
0x00, 0x7A, 0x0A, 0x0A, 0x72,
0x7D, 0x0D, 0x19, 0x31, 0x7D,
0x26, 0x29, 0x29, 0x2F, 0x28,
0x26, 0x29, 0x29, 0x29, 0x26,
0x30, 0x48, 0x4D, 0x40, 0x20,
0x38, 0x08, 0x08, 0x08, 0x08,
0x08, 0x08, 0x08, 0x08, 0x38,
0x2F, 0x10, 0xC8, 0xAC, 0xBA,
0x2F, 0x10, 0x28, 0x34, 0xFA,
0x00, 0x00, 0x7B, 0x00, 0x00,
0x08, 0x14, 0x2A, 0x14, 0x22,
0x22, 0x14, 0x2A, 0x14, 0x08,
0xAA, 0x00, 0x55, 0x00, 0xAA,
0xAA, 0x55, 0xAA, 0x55, 0xAA,
0x00, 0x00, 0x00, 0xFF, 0x00,
0x10, 0x10, 0x10, 0xFF, 0x00,
0x14, 0x14, 0x14, 0xFF, 0x00,
0x10, 0x10, 0xFF, 0x00, 0xFF,
0x10, 0x10, 0xF0, 0x10, 0xF0,
0x14, 0x14, 0x14, 0xFC, 0x00,
0x14, 0x14, 0xF7, 0x00, 0xFF,
0x00, 0x00, 0xFF, 0x00, 0xFF,
0x14, 0x14, 0xF4, 0x04, 0xFC,
0x14, 0x14, 0x17, 0x10, 0x1F,
0x10, 0x10, 0x1F, 0x10, 0x1F,
0x14, 0x14, 0x14, 0x1F, 0x00,
0x10, 0x10, 0x10, 0xF0, 0x00,
0x00, 0x00, 0x00, 0x1F, 0x10,
0x10, 0x10, 0x10, 0x1F, 0x10,
0x10, 0x10, 0x10, 0xF0, 0x10,
0x00, 0x00, 0x00, 0xFF, 0x10,
0x10, 0x10, 0x10, 0x10, 0x10,
0x10, 0x10, 0x10, 0xFF, 0x10,
0x00, 0x00, 0x00, 0xFF, 0x14,
0x00, 0x00, 0xFF, 0x00, 0xFF,
0x00, 0x00, 0x1F, 0x10, 0x17,
0x00, 0x00, 0xFC, 0x04, 0xF4,
0x14, 0x14, 0x17, 0x10, 0x17,
0x14, 0x14, 0xF4, 0x04, 0xF4,
0x00, 0x00, 0xFF, 0x00, 0xF7,
0x14, 0x14, 0x14, 0x14, 0x14,
0x14, 0x14, 0xF7, 0x00, 0xF7,
0x14, 0x14, 0x14, 0x17, 0x14,
0x10, 0x10, 0x1F, 0x10, 0x1F,
0x14, 0x14, 0x14, 0xF4, 0x14,
0x10, 0x10, 0xF0, 0x10, 0xF0,
0x00, 0x00, 0x1F, 0x10, 0x1F,
0x00, 0x00, 0x00, 0x1F, 0x14,
0x00, 0x00, 0x00, 0xFC, 0x14,
0x00, 0x00, 0xF0, 0x10, 0xF0,
0x10, 0x10, 0xFF, 0x10, 0xFF,
0x14, 0x14, 0x14, 0xFF, 0x14,
0x10, 0x10, 0x10, 0x1F, 0x00,
0x00, 0x00, 0x00, 0xF0, 0x10,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xF0, 0xF0, 0xF0, 0xF0, 0xF0,
0xFF, 0xFF, 0xFF, 0x00, 0x00,
0x00, 0x00, 0x00, 0xFF, 0xFF,
0x0F, 0x0F, 0x0F, 0x0F, 0x0F,
0x38, 0x44, 0x44, 0x38, 0x44,
0x7C, 0x2A, 0x2A, 0x3E, 0x14,
0x7E, 0x02, 0x02, 0x06, 0x06,
0x02, 0x7E, 0x02, 0x7E, 0x02,
0x63, 0x55, 0x49, 0x41, 0x63,
0x38, 0x44, 0x44, 0x3C, 0x04,
0x40, 0x7E, 0x20, 0x1E, 0x20,
0x06, 0x02, 0x7E, 0x02, 0x02,
0x99, 0xA5, 0xE7, 0xA5, 0x99,
0x1C, 0x2A, 0x49, 0x2A, 0x1C,
0x4C, 0x72, 0x01, 0x72, 0x4C,
0x30, 0x4A, 0x4D, 0x4D, 0x30,
0x30, 0x48, 0x78, 0x48, 0x30,
0xBC, 0x62, 0x5A, 0x46, 0x3D,
0x3E, 0x49, 0x49, 0x49, 0x00,
0x7E, 0x01, 0x01, 0x01, 0x7E,
0x2A, 0x2A, 0x2A, 0x2A, 0x2A,
0x44, 0x44, 0x5F, 0x44, 0x44,
0x40, 0x51, 0x4A, 0x44, 0x40,
0x40, 0x44, 0x4A, 0x51, 0x40,
0x00, 0x00, 0xFF, 0x01, 0x03,
0xE0, 0x80, 0xFF, 0x00, 0x00,
0x08, 0x08, 0x6B, 0x6B, 0x08,
0x36, 0x12, 0x36, 0x24, 0x36,
0x06, 0x0F, 0x09, 0x0F, 0x06,
0x00, 0x00, 0x18, 0x18, 0x00,
0x00, 0x00, 0x10, 0x10, 0x00,
0x30, 0x40, 0xFF, 0x01, 0x01,
0x00, 0x1F, 0x01, 0x01, 0x1E,
0x00, 0x19, 0x1D, 0x17, 0x12,
0x00, 0x3C, 0x3C, 0x3C, 0x3C,
0x00, 0x00, 0x00, 0x00, 0x00,
};
#endif

View File

@@ -0,0 +1,70 @@
#######################################
# Syntax Coloring Map For Adafruit_GFX
# and Adafruit_ST7735
#######################################
#######################################
# Datatypes (KEYWORD1)
#######################################
Adafruit_GFX KEYWORD1
Adafruit_ST7735 KEYWORD1
PImage KEYWORD1
#######################################
# Methods and Functions (KEYWORD2)
#######################################
drawPixel KEYWORD2
invertDisplay KEYWORD2
drawLine KEYWORD2
drawFastVLine KEYWORD2
drawFastHLine KEYWORD2
drawRect KEYWORD2
fillRect KEYWORD2
fillScreen KEYWORD2
drawCircle KEYWORD2
drawCircleHelper KEYWORD2
fillCircle KEYWORD2
fillCircleHelper KEYWORD2
drawTriangle KEYWORD2
fillTriangle KEYWORD2
drawRoundRect KEYWORD2
fillRoundRect KEYWORD2
drawBitmap KEYWORD2
drawChar KEYWORD2
setCursor KEYWORD2
setTextColor KEYWORD2
setTextSize KEYWORD2
setTextWrap KEYWORD2
height KEYWORD2
width KEYWORD2
setRotation KEYWORD2
getRotation KEYWORD2
newColor KEYWORD2
background KEYWORD2
fill KEYWORD2
noFill KEYWORD2
stroke KEYWORD2
noStroke KEYWORD2
text KEYWORD2
textWrap KEYWORD2
textSize KEYWORD2
circle KEYWORD2
point KEYWORD2
quad KEYWORD2
rect KEYWORD2
triangle KEYWORD2
loadImage KEYWORD2
image KEYWORD2
draw KEYWORD2
isValid KEYWORD2
#######################################
# Constants (LITERAL1)
#######################################