Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Adafruit_GFX.h>
- #include <Adafruit_TFTLCD.h>
- #include <SD.h>
- #include <SPI.h>
- #define LCD_CS A3
- #define LCD_CD A2
- #define LCD_WR A1
- #define LCD_RD A0
- #define SD_CS 10
- Adafruit_TFTLCD display(LCD_CS, LCD_CD, LCD_WR, LCD_RD, A4);
- #define BLACK 0x0000
- #define BLUE 0x001F
- #define RED 0xF800
- #define GREEN 0x07E0
- #define CYAN 0x07FF
- #define MAGENTA 0xF81F
- #define YELLOW 0xFFE0
- #define WHITE 0xFFFF
- #define ORANGE 0xFD20
- #define GREENYELLOW 0xAFE5
- #define NAVY 0x000F
- #define DARKGREEN 0x03E0
- #define DARKCYAN 0x03EF
- #define MAROON 0x7800
- #define PURPLE 0x780F
- #define OLIVE 0x7BE0
- #define LIGHTGREY 0xC618
- #define DARKGREY 0x7BEF
- uint8_t previous_mode = 0;
- void setup() {
- Serial.begin(9600);
- Serial.println(F("TFT LCD test"));
- #ifdef USE_ADAFRUIT_SHIELD_PINOUT
- Serial.println(F("Using Adafruit 2.4\" TFT Arduino Shield Pinout"));
- #else
- Serial.println(F("Using Adafruit 2.4\" TFT Breakout Board Pinout"));
- #endif
- Serial.print("TFT size is ");
- Serial.print(display.width());
- Serial.print("x");
- Serial.println(display.height());
- display.reset();
- uint16_t identifier = display.readID();
- if (identifier == 0x9325) {
- Serial.println(F("Found ILI9325 LCD driver"));
- } else if (identifier == 0x9328) {
- Serial.println(F("Found ILI9328 LCD driver"));
- } else if (identifier == 0x7575) {
- Serial.println(F("Found HX8347G LCD driver"));
- } else if (identifier == 0x9341) {
- Serial.println(F("Found ILI9341 LCD driver"));
- } else if (identifier == 0x8357) {
- Serial.println(F("Found HX8357D LCD driver"));
- } else {
- Serial.print(F("Unknown LCD driver chip: "));
- Serial.println(identifier, HEX);
- return;
- }
- display.begin(identifier);
- display.setRotation(3);
- Serial.print(F("Initializing SD card..."));
- if (!SD.begin(SD_CS)) {
- Serial.println(F("failed!"));
- return;
- }
- }
- void loop() {
- uint8_t current_mode = random(0, 6);
- current_mode = 3;
- if (current_mode != previous_mode) {
- display.fillScreen(BLACK);
- switch (current_mode) {
- case 0: {
- drawTarget(WHITE);
- drawCompositeWave(0, 0, display.width(), display.height(), 2, 2, 0.5, 3, 0.1, 6, random(1, 360), GREENYELLOW);
- break;
- }
- case 1: {
- drawGrid(24, WHITE);
- drawCompositeWave(0, 0, display.width(), display.height(), 2, 3, 0.33, 4, 0.25, 5, random(1, 360), GREENYELLOW);
- break;
- }
- case 2: {
- drawCircularGauge(display.width() / 2 - (display.height() - 40) / 2, 20, display.height() - 40, display.height() - 40, 16, 180, 75, WHITE);
- drawCompositeWave(display.width() / 2 - 60, display.height() / 2 - 60, 120, 120, 4, 2, 0.75, 3, 0.1, 6, random(1, 360), GREENYELLOW);
- drawHorizontalRandomBarChart(8, 8, 40, display.height() * 0.75, 8, 4, RED);
- drawVerticalTriangleGauge(display.width() - 40, 40, 32, 60, WHITE, RED, 60);
- break;
- }
- case 3: {
- bmpDraw("samhain.bmp", 0, 0);
- drawHorizontalRandomBarChart(8, 16, 40, display.height() * 0.4, 8, 4, RED);
- display.setCursor(display.width() - 24, 0);
- display.setTextColor(RED);
- display.setTextSize(4);
- display.print("6");
- delay(500);
- break;
- }
- case 4: {
- display.fillRect(0, display.height() * 0.625, display.width(), display.height() * 0.375, DARKGREEN);
- drawBorder(WHITE);
- drawCompositeWave(0, 0, display.width(), display.height() * 0.6, 2, 3, 0.33, 4, 0.25, 5, random(1, 360), WHITE);
- drawCompositeWave(0, display.height() * 0.625, display.width(), display.height() * 0.375, 4, 2, 0.75, 3, 0.1, 6, random(1, 360), RED);
- break;
- }
- case 5: {
- drawGrid(display.width() / 6 + 0.5, WHITE);
- display.drawCircle(display.width() / 2, display.height(), display.height() * 0.9, WHITE);
- display.drawCircle(display.width() / 2, display.height(), display.height() * 0.6, WHITE);
- display.drawCircle(display.width() / 2, display.height(), display.height() * 0.3, WHITE);
- display.fillCircle(display.width() * 0.75, display.height() * 0.25, 8, GREEN);
- display.fillCircle(display.width() * 0.75, display.height() * 0.25, 6, GREENYELLOW);
- break;
- }
- case 6: {
- drawTarget(WHITE);
- drawSineWave(0, 0, display.width(), display.height(), 0.66, 3, random(1, 360), GREEN);
- break;
- }
- case 7: {
- display.drawLine(display.width() / 2, 20, display.width() / 2, display.height() - 19, WHITE);
- display.drawLine(display.width() / 2 + 20, display.height() / 2, display.width() - 20, display.height() / 2, WHITE);
- display.setCursor(display.width() - 64, 20);
- display.setTextColor(RED);
- display.setTextSize(8);
- display.print("6");
- delay(500);
- drawHorizontalRandomBarChart(20, 20, display.width() * 0.33, display.height() / 3, 8, 4, RED);
- drawVerticalRandomBarChart(20, display.height() / 2, display.width() * 0.33, display.height() * 0.4, 8, 4, GREEN);
- drawSineWave(display.width() / 2 + 20, display.height() / 2, display.width() * 0.4, display.height() / 2 - 20, 0.4, 7, random(1, 360), GREEN);
- break;
- }
- delay(1000);
- }
- }
- previous_mode = current_mode;
- }
- void drawBorder(uint16_t color) {
- display.drawRect(0, 0, display.width(), display.height(), color);
- }
- void drawGrid(uint8_t step_size, uint16_t color) {
- drawBorder(color);
- for (uint16_t x = 0; x <= display.width(); x += step_size) {
- display.drawLine(x, 0, x, display.height(), color);
- }
- for (uint16_t y = 0; y <= display.height(); y += step_size) {
- display.drawLine(0, y, display.width(), y, color);
- }
- }
- void drawTarget(uint16_t color) {
- uint8_t border = 8, step_size = 16, line_width = 10, line_step = line_width / 2;
- uint16_t x_start = border - 1, x_center = display.width() / 2, x_end = display.width() - border, y_start = border - 1, y_center = display.height() / 2, y_end = display.height() - border;
- display.drawLine(x_center, y_start, x_center, y_end, color);
- display.drawLine(x_start, y_center, x_end, y_center, color);
- //Vertical steps
- display.drawLine(x_start, y_center - line_step * 4, x_start, y_center + line_step * 4, color);
- display.drawLine(x_start + step_size, y_center - line_step * 2, x_start + step_size, y_center + line_step * 2, color);
- display.drawLine(x_end - step_size, y_center - line_step * 2, x_end - step_size, y_center + line_step * 2, color);
- display.drawLine(x_end, y_center - line_step * 4, x_end, y_center + line_step * 4, color);
- for (uint16_t x_current = x_start + 2 * step_size; x_current < x_end - 2 * step_size; x_current += step_size) {
- display.drawLine(x_current, y_center - line_step, x_current, y_center + line_step, color);
- }
- //Horizontal steps
- display.drawLine(x_center - line_step * 4, y_start, x_center + line_step * 4, y_start, color);
- display.drawLine(x_center - line_step * 2, y_start + step_size, x_center + line_step * 2, y_start + step_size, color);
- display.drawLine(x_center - line_step * 2, y_end - step_size, x_center + line_step * 2, y_end - step_size, color);
- display.drawLine(x_center - line_step * 4, y_end, x_center + line_step * 4, y_end, color);
- for (uint16_t y_current = y_start + 2 * step_size; y_current < y_end - 2 * step_size; y_current += step_size) {
- display.drawLine(x_center - line_step, y_current, x_center + line_step, y_current, color);
- }
- }
- void drawSineWave(uint16_t x, uint16_t y, uint16_t width, uint16_t height, float amplitude, float frequency, float phase, uint16_t color) {
- // amplitude: vertical compression (1 = screen height)
- // frequency: horizontal compression (1 = screen width)
- // phase: horitontal shift (in degrees)
- uint16_t vTop = y, vCenter = y + height / 2, vBottom = y + height - 1 , step_size = 2;
- int old_x = x, old_y = vCenter, new_y = old_y;
- for (int new_x = x; new_x < x + width; new_x += step_size) {
- float value = amplitude * sin((frequency * M_PI / 180 * new_x) + phase);
- new_y = vCenter + value * (height / 2) ;
- new_y = min(max(new_y, vTop), vBottom);
- if (new_x == x) {
- display.drawPixel(new_x, new_y, color);
- } else {
- display.drawLine(old_x, old_y, new_x, new_y, color);
- display.drawLine(old_x + 1, old_y - 1, new_x + 1, new_y - 1, color);
- }
- old_x = new_x;
- old_y = new_y;
- }
- }
- void drawCompositeWave(uint16_t x, uint16_t y, uint16_t width, uint16_t height, float amplitude1, float frequency1, float amplitude2, float frequency2, float amplitude3, float frequency3, uint16_t phase, uint16_t color) {
- // amplitude: vertical compression (1 = height)
- // frequency: horizontal compression (1 = width)
- // phase: horitontal shift (in degrees)
- uint16_t vTop = y, vCenter = y + height / 2, vBottom = y + height - 1 , step_size = 2;
- int old_x = x, old_y = vCenter, new_y = old_y;
- for (int new_x = x; new_x < x + width; new_x += step_size) {
- float value1 = amplitude1 * sin((frequency1 * M_PI / 180 * new_x) + phase);
- float value2 = amplitude2 * cos((frequency2 * M_PI / 180 * new_x) + phase);
- float value3 = amplitude3 * tan((frequency3 * M_PI / 180 * new_x) + phase);
- float value = value1 * value2 * value3 * 0.66;
- new_y = vCenter + value * (height / 2) ;
- new_y = min(max(new_y, vTop), vBottom);
- if (new_x == x) {
- display.drawPixel(new_x, new_y, color);
- } else {
- display.drawLine(old_x, old_y, new_x, new_y, color);
- display.drawLine(old_x + 1, old_y - 1, new_x + 1, new_y - 1, color);
- }
- old_x = new_x;
- old_y = new_y;
- }
- }
- void drawCircularGauge(uint16_t x, uint16_t y, uint16_t width, uint16_t height, int line_width, int start_angle, float percentage, uint16_t color) {
- int radius_x = width / 2,
- radius_y = height / 2,
- x_center = x + radius_x,
- y_center = y + radius_y;
- float DEG2RAD = 0.0174532925;
- byte seg_degrees = 6;
- int seg_count = map(percentage, 0, 100, 0, 360) / seg_degrees;
- // Calculate first pair of coordinates for segment start
- float start_x = cos((start_angle - 90) * DEG2RAD);
- float start_y = sin((start_angle - 90) * DEG2RAD);
- uint16_t x0 = start_x * (radius_x - line_width) + x_center;
- uint16_t y0 = start_y * (radius_y - line_width) + y_center;
- uint16_t x1 = start_x * radius_x + x_center;
- uint16_t y1 = start_y * radius_y + y_center;
- display.fillRect(x, y, width, height, BLACK); // Reset the display area's background
- for (int current_angle = start_angle; current_angle < start_angle + seg_degrees * seg_count; current_angle += seg_degrees) {
- float start_x2 = cos((current_angle + 1 - 90) * DEG2RAD);
- float start_y2 = sin((current_angle + 1 - 90) * DEG2RAD);
- int x2 = start_x2 * (radius_x - line_width) + x_center;
- int y2 = start_y2 * (radius_y - line_width) + y_center;
- int x3 = start_x2 * radius_x + x_center;
- int y3 = start_y2 * radius_y + y_center;
- display.fillTriangle(x0, y0, x1, y1, x2, y2, color);
- display.fillTriangle(x1, y1, x2, y2, x3, y3, color);
- // Copy segment end to segement start for next segment
- x0 = x2;
- y0 = y2;
- x1 = x3;
- y1 = y3;
- }
- }
- void drawHorizontalTriangleGauge(uint16_t x0, uint16_t y0, uint16_t width, uint16_t height, uint16_t color_frame, uint16_t color_fill, float percentage) {
- uint16_t width_filled = map(percentage, 0, 100, 0, width - 2),
- x1 = x0,
- y1 = y0 + height - 1,
- x2 = x0 + width - 1,
- y2 = y1;
- display.fillRect(x0, y0, width, height, BLACK); // Reset the display area's background
- display.fillRect(x0 + 1, y0, width_filled, height - 1, color_fill);
- display.fillTriangle(x0, y0, x2, y0, x0, y1 - 1, BLACK);
- display.drawTriangle(x0, y1, x2, y0, x2, y2, color_frame);
- }
- void drawVerticalTriangleGauge(uint16_t x0, uint16_t y0, uint16_t width, uint16_t height, uint16_t color_frame, uint16_t color_fill, float percentage) {
- uint16_t height_empty = map(percentage, 0, 100, height - 2, 0),
- x1 = x0,
- y1 = y0 + height - 1,
- x2 = x0 + width - 1,
- y2 = y1;
- display.fillRect(x0, y0, width, height, BLACK); // Reset the display area's background
- display.fillTriangle(x0, y0, x2, y0, x0, y2, color_fill);
- display.fillRect(x0 + 1, y0 + 1, width - 2, height_empty, BLACK);
- display.drawTriangle(x0, y0, x2, y0, x0, y2, color_frame);
- display.drawLine(x0, y2, x2, y0, color_frame);
- }
- void drawVerticalRandomBarChart(uint16_t x0, uint16_t y0, uint16_t width, uint16_t height, uint16_t bar_width, uint16_t bar_spacing, uint16_t color) {
- display.fillRect(x0, y0, width, height, BLACK); // Reset the display area's background
- display.drawLine(x0, y0, x0, y0 + height - 1, color);
- display.drawLine(x0, y0 + height - 1, x0 + width - 1, y0 + height - 1, color);
- for (int x1 = x0 + bar_spacing; x1 < width - bar_spacing; x1 += (bar_width + bar_spacing)) {
- int bar_height = random(0, height);
- display.fillRect(x1, y0 + height - bar_height, bar_width, bar_height, color);
- }
- }
- void drawHorizontalRandomBarChart(uint16_t x0, uint16_t y0, uint16_t width, uint16_t height, uint16_t bar_height, uint16_t bar_spacing, uint16_t color) {
- display.fillRect(x0, y0, width, height, BLACK); // Reset the display area's background
- display.drawLine(x0, y0, x0, y0 + height - 1, color);
- display.drawLine(x0, y0 + height - 1, x0 + width - 1, y0 + height - 1, color);
- for (int y1 = (y0 + bar_spacing); y1 < (y0 + height); y1 += (bar_height + bar_spacing)) {
- int bar_width = random(0, width);
- display.fillRect(x0, y1, bar_width, bar_height, color);
- }
- }
- #define BUFFPIXEL 20 //Printing speed 20 is meant to be the best, you can go to 60 but using too much RAM
- //drawing function no touchy :D
- void bmpDraw(char *filename, int x, int y) {
- 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
- uint8_t sdbuffer[3 * BUFFPIXEL]; // pixel in buffer (R+G+B per pixel)
- uint16_t lcdbuffer[BUFFPIXEL]; // pixel out buffer (16-bit per pixel)
- uint8_t buffidx = sizeof(sdbuffer); // Current position in sdbuffer
- boolean goodBmp = false; // Set to true on valid header parse
- boolean flip = true; // BMP is stored bottom-to-top
- int w, h, row, col;
- uint8_t r, g, b;
- uint32_t pos = 0, startTime = millis();
- uint8_t lcdidx = 0;
- boolean first = true;
- if ((x >= display.width()) || (y >= display.height()))
- return;
- Serial.println();
- progmemPrint(PSTR("Loading image '"));
- Serial.print(filename);
- Serial.println('\'');
- // Open requested file on SD card
- if ((bmpFile = SD.open(filename)) == NULL) {
- progmemPrintln(PSTR("File not found"));
- return;
- }
- // Parse BMP header
- if (read16(bmpFile) == 0x4D42) { // BMP signature
- progmemPrint(PSTR("File size: "));
- Serial.println(read32(bmpFile));
- (void)read32(bmpFile); // Read & ignore creator bytes
- bmpImageoffset = read32(bmpFile); // Start of image data
- progmemPrint(PSTR("Image Offset: "));
- Serial.println(bmpImageoffset, DEC);
- // Read DIB header
- progmemPrint(PSTR("Header size: "));
- Serial.println(read32(bmpFile));
- bmpWidth = read32(bmpFile);
- bmpHeight = read32(bmpFile);
- if (read16(bmpFile) == 1) { // # planes -- must be '1'
- bmpDepth = read16(bmpFile); // bits per pixel
- progmemPrint(PSTR("Bit Depth: "));
- Serial.println(bmpDepth);
- if ((bmpDepth == 24) && (read32(bmpFile) == 0)) { // 0 = uncompressed
- goodBmp = true; // Supported BMP format -- proceed!
- progmemPrint(PSTR("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;
- }
- // Crop area to be loaded
- w = bmpWidth;
- h = bmpHeight;
- if ((x + w - 1) >= display.width())
- w = display.width() - x;
- if ((y + h - 1) >= display.height())
- h = display.height() - y;
- // Set TFT address window to clipped image bounds
- display.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 (flip) // Bitmap is stored bottom-to-top order (normal BMP)
- pos = bmpImageoffset + (bmpHeight - 1 - row) * rowSize;
- else // Bitmap is stored top-to-bottom
- pos = bmpImageoffset + row * rowSize;
- if (bmpFile.position() != pos) { // Need seek?
- bmpFile.seek(pos);
- buffidx = sizeof(sdbuffer); // Force buffer reload
- }
- for (col = 0; col < w; col++) { // For each column...
- // Time to read more pixel data?
- if (buffidx >= sizeof(sdbuffer)) { // Indeed
- // Push LCD buffer to the display first
- if (lcdidx > 0) {
- display.pushColors(lcdbuffer, lcdidx, first);
- lcdidx = 0;
- first = false;
- }
- bmpFile.read(sdbuffer, sizeof(sdbuffer));
- buffidx = 0; // Set index to beginning
- }
- // Convert pixel from BMP to TFT format
- b = sdbuffer[buffidx++];
- g = sdbuffer[buffidx++];
- r = sdbuffer[buffidx++];
- lcdbuffer[lcdidx++] = display.color565(r, g, b);
- } // end pixel
- } // end scanline
- // Write any remaining data to LCD
- if (lcdidx > 0) {
- display.pushColors(lcdbuffer, lcdidx, first);
- }
- progmemPrint(PSTR("Loaded in "));
- Serial.print(millis() - startTime);
- Serial.println(" ms");
- } // end goodBmp
- }
- }
- bmpFile.close();
- if (!goodBmp)
- progmemPrintln(PSTR("BMP format not recognized."));
- }
- // 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 read16(File f) {
- uint16_t result;
- ((uint8_t *)&result)[0] = f.read(); // LSB
- ((uint8_t *)&result)[1] = f.read(); // MSB
- return result;
- }
- uint32_t 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;
- }
- // Copy string from flash to serial port
- // Source string MUST be inside a PSTR() declaration!
- void progmemPrint(const char *str) {
- char c;
- while (c = pgm_read_byte(str++))
- Serial.print(c);
- }
- // Same as above, with trailing newline
- void progmemPrintln(const char *str) {
- progmemPrint(str);
- Serial.println();
- }
Add Comment
Please, Sign In to add comment