To get user input from Serial Monitor:
void setup() {
Serial.begin(9600);
}
void loop() {
if (Serial.available() > 0){
String stringIn = Serial.readString();
stringIn.toUpperCase();
Serial.println(stringIn);
}
}
To get user input from Serial Monitor:
void setup() {
Serial.begin(9600);
}
void loop() {
if (Serial.available() > 0){
String stringIn = Serial.readString();
stringIn.toUpperCase();
Serial.println(stringIn);
}
}
This post show how to generate QR Code with Nano RP2040 Connect (Arduino Framework) using QRCode library by Richard Moore, also display on ST7735 SPI TFT, and finally adapt to WiFiNINA > AP_SimpleWebServer example to control onboard LED.
Library
Install QRCode library by Richard Moore in Arduino IDE's Libraries Manager.
/************************************************
* TFT_ST7735 nano RP2040 Connect
* ------------------------------
* VCC 3V3
* GND GND
* CS 10
* RESET 9
* A0(DC) 8
* SDA 11
* SCK 12
* LED 3V3
* **********************************************/
/**
* Arduino nano RP2040 Connect exercise to generate QR Code,
* with display on Serial Monitor and ST7735 SPI TFT.
* Modified from QRCode example.
*
* Library: QRCode by Richard Moore
* https://github.com/ricmoo/qrcode/
*
*/
#include <Adafruit_GFX.h>
#include <Adafruit_ST7735.h>
#include "qrcode.h"
/************************************************
* TFT_ST7735 nano RP2040 Connect
* ------------------------------
* VCC 3V3
* GND GND
* CS 10
* RESET 9
* A0(DC) 8
* SDA 11
* SCK 12
* LED 3V3
* **********************************************/
// TFT display use software SPI interface.
#define TFT_MOSI 11 // Data out
#define TFT_SCLK 12 // Clock out
#define TFT_CS 10 // Chip select line for TFT display
#define TFT_DC 8 // Data/command line for TFT
#define TFT_RST 9 // Reset line for TFT (or connect to VCC)
Adafruit_ST7735 tft_ST7735 = Adafruit_ST7735(TFT_CS, TFT_DC,
TFT_MOSI, TFT_SCLK, TFT_RST);
void setup() {
delay(1000);
Serial.begin(115200);
delay(1000);
Serial.println("- setup() started -");
tft_ST7735.initR(INITR_BLACKTAB);
tft_ST7735.setRotation(2);
// tft display RGB to make sure it's work properly
tft_ST7735.fillScreen(ST7735_BLACK);
delay(300);
tft_ST7735.fillScreen(ST7735_RED);
delay(300);
tft_ST7735.fillScreen(ST7735_GREEN);
delay(300);
tft_ST7735.fillScreen(ST7735_BLUE);
delay(300);
tft_ST7735.fillScreen(ST7735_WHITE);
delay(300);
// Start time
uint32_t dt = millis();
// Create the QR code
QRCode qrcode;
const char *data = "http://arduino-er.blogspot.com/";
const uint8_t ecc = 0; //lowest level of error correction
const uint8_t version = 3;
uint8_t qrcodeData[qrcode_getBufferSize(version)];
qrcode_initText(&qrcode,
qrcodeData,
version, ecc,
data);
// Delta time
dt = millis() - dt;
Serial.print("QR Code Generation Time: ");
Serial.print(dt);
Serial.print("\n\n");
Serial.println(data);
Serial.print("qrcode.version: ");
Serial.println(qrcode.version);
Serial.print("qrcode.ecc: ");
Serial.println(qrcode.ecc);
Serial.print("qrcode.size: ");
Serial.println(qrcode.size);
Serial.print("qrcode.mode: ");
Serial.println(qrcode.mode);
Serial.print("qrcode.mask: ");
Serial.println(qrcode.mask);
Serial.println();
const int xy_scale = 3;
const int x_offset = (tft_ST7735.width() - xy_scale*qrcode.size)/2;
const int y_offset = (tft_ST7735.height() - xy_scale*qrcode.size)/2;
// Top quiet zone
Serial.print("\n\n\n\n");
for (uint8_t y = 0; y < qrcode.size; y++) {
// Left quiet zone
Serial.print(" ");
// Each horizontal module
for (uint8_t x = 0; x < qrcode.size; x++) {
// Print each module (UTF-8 \u2588 is a solid block)
bool mod = qrcode_getModule(&qrcode, x, y);
//Serial.print(mod ? "\u2588\u2588": " ");
if(mod){
Serial.print("██"); //same as "\u2588\u2588"
//direct paste "██" copied from Serial Monitor
int px = x_offset + (x * xy_scale);
int py = y_offset + (y * xy_scale);
tft_ST7735.fillRect(px, py, xy_scale, xy_scale, ST7735_BLACK);
}else{
Serial.print(" ");
}
}
Serial.print("\n");
}
// Bottom quiet zone
Serial.print("\n\n\n\n");
}
void loop() {
}
/**
* Arduino nano RP2040 Connect exercise to generate QR Code,
* WiFiWebServer with QRCode on ST7735 SPI TFT
*
* Library: QRCode by Richard Moore
* https://github.com/ricmoo/qrcode/
*
*/
#include <WiFiNINA.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ST7735.h>
#include "qrcode.h"
/************************************************
* TFT_ST7735 nano RP2040 Connect
* ------------------------------
* VCC 3V3
* GND GND
* CS 10
* RESET 9
* A0(DC) 8
* SDA 11
* SCK 12
* LED 3V3
* **********************************************/
// TFT display use software SPI interface.
//
// Hardware SPI pins are specific to the Arduino board type and
// cannot be remapped to alternate pins.
//
// In this exercise, we are going to implement a web server to
// turn ON/OFF on board LED, which is connected to D13. It's
// conflict with hardware SPI SCK.
// So we cannot use hardware SPI to controll ST7735.
#define TFT_MOSI 11 // Data out
#define TFT_SCLK 12 // Clock out
#define TFT_CS 10 // Chip select line for TFT display
#define TFT_DC 8 // Data/command line for TFT
#define TFT_RST 9 // Reset line for TFT (or connect to VCC)
Adafruit_ST7735 tft_ST7735 = Adafruit_ST7735(TFT_CS, TFT_DC,
TFT_MOSI, TFT_SCLK, TFT_RST);
char ssid[] = "ssid"; // your network SSID (name)
char pass[] = "password"; // your network password (use for WPA, or use as key for WEP)
int status = WL_IDLE_STATUS;
WiFiServer server(80);
void setup() {
delay(1000);
Serial.begin(115200);
delay(1000);
Serial.println("- setup() started -");
Serial.print("LED_BUILTIN: ");
Serial.println(LED_BUILTIN);
pinMode(LED_BUILTIN, OUTPUT); // set the LED pin mode
tft_ST7735.initR(INITR_BLACKTAB);
tft_ST7735.setRotation(2);
tft_ST7735.setTextWrap(true);
tft_ST7735.setTextColor(ST77XX_BLACK);
// tft display RGB to make sure it's work properly
digitalWrite(LED_BUILTIN, HIGH);
tft_ST7735.fillScreen(ST7735_BLACK);
delay(300);
digitalWrite(LED_BUILTIN, LOW);
tft_ST7735.fillScreen(ST7735_RED);
delay(300);
digitalWrite(LED_BUILTIN, HIGH);
tft_ST7735.fillScreen(ST7735_GREEN);
delay(300);
digitalWrite(LED_BUILTIN, LOW);
tft_ST7735.fillScreen(ST7735_BLUE);
delay(300);
digitalWrite(LED_BUILTIN, HIGH);
tft_ST7735.fillScreen(ST7735_WHITE);
delay(300);
// check for the WiFi module:
if (WiFi.status() == WL_NO_MODULE) {
Serial.println("Communication with WiFi module failed!");
tft_ST7735.setCursor(0, 0);
tft_ST7735.print("Communication with WiFi module failed!");
// don't continue
while (true);
}
Serial.print("WiFi.firmwareVersion(): ");
Serial.println(WiFi.firmwareVersion());
// attempt to connect to WiFi network:
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
delay(10000);
}
server.begin();
// you're connected now, so print out the status:
printWifiStatus();
// Start time
uint32_t dt = millis();
// Create the QR code
QRCode qrcode;
char myIpQrData[30] ="http://";
prepareIpQrData(myIpQrData);
const uint8_t ecc = 0; //lowest level of error correction
const uint8_t version = 3;
uint8_t qrcodeData[qrcode_getBufferSize(version)];
qrcode_initText(&qrcode,
qrcodeData,
version, ecc,
myIpQrData);
// Delta time
dt = millis() - dt;
Serial.print("QR Code Generation Time: ");
Serial.print(dt);
Serial.print("\n\n");
Serial.println(myIpQrData);
Serial.print("qrcode.version: ");
Serial.println(qrcode.version);
Serial.print("qrcode.ecc: ");
Serial.println(qrcode.ecc);
Serial.print("qrcode.size: ");
Serial.println(qrcode.size);
Serial.print("qrcode.mode: ");
Serial.println(qrcode.mode);
Serial.print("qrcode.mask: ");
Serial.println(qrcode.mask);
Serial.println();
const int xy_scale = 3;
const int x_offset = (tft_ST7735.width() - xy_scale*qrcode.size)/2;
const int y_offset = (tft_ST7735.height() - xy_scale*qrcode.size)/2;
// Top quiet zone
Serial.print("\n\n\n\n");
for (uint8_t y = 0; y < qrcode.size; y++) {
// Left quiet zone
Serial.print(" ");
// Each horizontal module
for (uint8_t x = 0; x < qrcode.size; x++) {
// Print each module (UTF-8 \u2588 is a solid block)
bool mod = qrcode_getModule(&qrcode, x, y);
//Serial.print(mod ? "\u2588\u2588": " ");
if(mod){
Serial.print("██"); //same as "\u2588\u2588"
//direct paste "██" copied from Serial Monitor
int px = x_offset + (x * xy_scale);
int py = y_offset + (y * xy_scale);
tft_ST7735.fillRect(px, py, xy_scale, xy_scale, ST7735_BLACK);
}else{
Serial.print(" ");
}
}
Serial.print("\n");
}
// Bottom quiet zone
Serial.print("\n\n\n\n");
digitalWrite(LED_BUILTIN, LOW);
}
//prepare ip address in char *,
//to pass to qrcode_initText()
char * prepareIpQrData(char *dest){
Serial.println("***********************");
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
Serial.println(ip[0]);
Serial.println(ip[1]);
Serial.println(ip[2]);
Serial.println(ip[3]);
Serial.println("-------------");
char buffer0[3];
char buffer1[3];
char buffer2[3];
char buffer3[3];
itoa(ip[0], buffer0, 10);
itoa(ip[1], buffer1, 10);
itoa(ip[2], buffer2, 10);
itoa(ip[3], buffer3, 10);
char str[15] = "";
char dot[] = ".";
strcat(dest, buffer0);
strcat(dest, dot);
strcat(dest, buffer1);
strcat(dest, dot);
strcat(dest, buffer2);
strcat(dest, dot);
strcat(dest, buffer3);
Serial.println(dest);
Serial.println("***********************");
}
//The web page part is mainly modified
//from WiFiNINA example > AP_SimpleWebServer
//that lets you blink an LED via the web.
void loop() {
// compare the previous status to the current status
if (status != WiFi.status()) {
// it has changed update the variable
status = WiFi.status();
if (status == WL_AP_CONNECTED) {
// a device has connected to the AP
Serial.println("Device connected to AP");
} else {
// a device has disconnected from the AP, and we are back in listening mode
Serial.println("Device disconnected from AP");
}
}
WiFiClient client = server.available(); // listen for incoming clients
if (client) { // if you get a client,
Serial.println("new client"); // print a message out the serial port
String currentLine = ""; // make a String to hold incoming data from the client
while (client.connected()) { // loop while the client's connected
delayMicroseconds(10); // This is required for the Arduino Nano RP2040 Connect
// - otherwise it will loop so fast that SPI will
// never be served.
if (client.available()) { // if there's bytes to read from the client,
char c = client.read(); // read a byte, then
Serial.write(c); // print it out the serial monitor
if (c == '\n') { // if the byte is a newline character
// if the current line is blank, you got two newline characters in a row.
// that's the end of the client HTTP request, so send a response:
if (currentLine.length() == 0) {
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
// and a content-type so the client knows what's coming, then a blank line:
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println();
client.println("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
// the content of the HTTP response follows the header:
client.print("Click <a href=\"/H\">here</a> turn the LED on<br>");
client.print("Click <a href=\"/L\">here</a> turn the LED off<br>");
// The HTTP response ends with another blank line:
client.println();
// break out of the while loop:
break;
}
else { // if you got a newline, then clear currentLine:
currentLine = "";
}
}
else if (c != '\r') { // if you got anything else but a carriage return character,
currentLine += c; // add it to the end of the currentLine
}
// Check to see if the client request was "GET /H" or "GET /L":
if (currentLine.endsWith("GET /H")) {
Serial.println("\n===> GET /H");
digitalWrite(LED_BUILTIN, HIGH); // GET /H turns the LED on
}
if (currentLine.endsWith("GET /L")) {
Serial.println("\n===> GET /L");
digitalWrite(LED_BUILTIN, LOW); // GET /L turns the LED off
}
}
}
// close the connection:
client.stop();
Serial.println("client disconnected");
}
}
void printWifiStatus() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your board's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
}
Exercises run on Arduino Nano RP2040 Connect (in Arduino framework) work with
1.8" 128x160 ST7735 SPI TFT/SD Card Module, to:
- simple display
something on ST7735 SPI TFT
- Display files in SD Card, and read txt file
from SD Card.
- Read bmp from SD Card and display on ST7735 SPI TFT
-
Try example of Adafruit_ImageReader
- Simplified version display bitmap
from SD and display on TFT using Adafruit_ImageReader.
In the following exercise Hardware SPI is used for ST7735 and SD Card SPI
interface. refer to last post, SPI pins in Arduino Nano RP2040 Connect is:
- MISO - D12
- MOSI - D11
- SCK -
D13
* Nano RP2040 Connect drive ST7735 SPI and SD using hardware SPI
*
* TFT_ST7735 nano RP2040 Connect
* ------------------------------
* VCC 3V3
* GND GND
* CS 10
* RESET 9
* A0(DC) 8
* SDA 11
* SCK 13
* LED 3V3
*
* TFT_ST7735 nano RP2040 Connect
* ------------------------------
* SD_CS 4
* SD_MOSI 11
* SD_MISO 12
* SD_SCK 13
****************************************************/
Exercise code:
Please notice that "Adafruit ST7735 and ST7789 Library" and "Adafruit GFX Library" are needed, make sure it's installed in Arduino IDE's Libraries Manager.
nanoRP2040_ST7735.ino, a simple exercise to display something on ST7735 SPI TFT.
/***************************************************
* nano RP2040 Connect exercise
* + ST7745/SD
*
* On Arduino nanp RP2040 Connect:
* (CIPO/MISO) - D12
* (COPI/MOSI) - D11
* (SCK) - D13
* (CS/SS) - Any GPIO (except for A6/A7
*
* This example drive ST7735 SPI and SD using hardware SPI
*
* TFT_ST7735 nano RP2040 Connect
* ------------------------------
* VCC 3V3
* GND GND
* CS 10
* RESET 9
* A0(DC) 8
* SDA 11
* SCK 13
* LED 3V3
*
****************************************************/
#include <Adafruit_GFX.h>
#include <Adafruit_ST7735.h>
#include <SD.h>
#include <SPI.h>
// TFT display using hardware SPI interface.
// Hardware SPI pins are specific to the Arduino board type and
// cannot be remapped to alternate pins.
#define TFT_CS 10 // Chip select line for TFT display
#define TFT_DC 8 // Data/command line for TFT
#define TFT_RST 9 // Reset line for TFT (or connect to +5V)
Adafruit_ST7735 tft_ST7735 = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);
void setup(void) {
delay(1000);
Serial.begin(115200);
delay(1000);
Serial.println("=====================");
Serial.println("- setup() -");
tft_ST7735.initR(INITR_BLACKTAB);
Serial.println("tft: "
+ String(tft_ST7735.width()) + " : "
+ String(tft_ST7735.height()));
tft_ST7735.fillScreen(ST7735_BLACK);
tft_ST7735.setTextWrap(true);
tft_ST7735.setTextColor(ST77XX_WHITE);
tft_ST7735.setCursor(0, 0);
tft_ST7735.print("Arduino nano RP2040 Connect");
tft_ST7735.println("\n");
//----------------------------------------
delay(2000);
tft_ST7735.setRotation(3);
tft_ST7735.setCursor(0, 30);
tft_ST7735.print("rotation: " + String(tft_ST7735.getRotation()));
tft_ST7735.setCursor(0, 40);
tft_ST7735.print(String(tft_ST7735.width()) + " x " + String(tft_ST7735.height()));
delay(2000);
tft_ST7735.fillScreen(ST77XX_RED);
tft_ST7735.setCursor(50, 50);
tft_ST7735.print("RED");
delay(1000);
tft_ST7735.fillScreen(ST77XX_GREEN);
tft_ST7735.setCursor(50, 50);
tft_ST7735.print("GREEN");
delay(1000);
tft_ST7735.fillScreen(ST77XX_BLUE);
tft_ST7735.setCursor(50, 50);
tft_ST7735.print("BLUE");
delay(1000);
delay(1000);
//----------------------------------------
Serial.println("\n- End of setup() -\n");
}
void loop() {
tft_ST7735.fillScreen(ST77XX_BLUE);
for(int offset=0; offset<tft_ST7735.height()/2; offset++){
int col;
if(offset%5 == 0)
col = ST77XX_WHITE;
else
col = ST77XX_BLACK;
tft_ST7735.drawRect(offset, offset,
tft_ST7735.width()-2*offset, tft_ST7735.height()-2*offset,
col);
delay(100);
}
delay(2000);
tft_ST7735.fillScreen(ST77XX_BLACK);
int cx = tft_ST7735.width()/2;
int cy = tft_ST7735.height()/2;
for(int r=0; r<tft_ST7735.height()/2; r=r+10){
tft_ST7735.drawCircle(cx, cy,
r,
ST77XX_WHITE);
delay(200);
}
delay(2000);
delay(2000);
}
/***************************************************
* nano RP2040 Connect exercise
* + ST7745/SD
*
* On Arduino nanp RP2040 Connect:
* (CIPO/MISO) - D12
* (COPI/MOSI) - D11
* (SCK) - D13
* (CS/SS) - Any GPIO (except for A6/A7
*
* This example drive ST7735 SPI and SD using hardware SPI
*
* TFT_ST7735 nano RP2040 Connect
* ------------------------------
* VCC 3V3
* GND GND
* CS 10
* RESET 9
* A0(DC) 8
* SDA 11
* SCK 13
* LED 3V3
*
* TFT_ST7735 nano RP2040 Connect
* ------------------------------
* SD_CS 4
* SD_MOSI 11
* SD_MISO 12
* SD_SCK 13
****************************************************/
#include <Adafruit_GFX.h>
#include <Adafruit_ST7735.h>
#include <SD.h>
#include <SPI.h>
// TFT display and SD card will share the hardware SPI interface.
// Hardware SPI pins are specific to the Arduino board type
#define SD_CS 4 // Chip select line for SD card
#define TFT_CS 10 // Chip select line for TFT display
#define TFT_DC 8 // Data/command line for TFT
#define TFT_RST 9 // Reset line for TFT (or connect to +5V)
Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);
File root;
File myFile;
void setup(void) {
delay(1000);
Serial.begin(115200);
delay(1000);
Serial.println("=====================");
Serial.println("- setup() -");
tft.initR(INITR_BLACKTAB);
Serial.println("tft: "
+ String(tft.width()) + " : "
+ String(tft.height()));
tft.fillScreen(ST7735_BLACK);
tft.setTextWrap(true);
tft.setTextColor(ST77XX_WHITE);
tft.setCursor(0, 0);
tft.print("Arduino nano RP2040 Connect");
tft.println("\n");
//----------------------------------------
Serial.println("Initializing SD card...");
if (!SD.begin(SD_CS)){
Serial.println("SD.begin() failed!");
}
else{
Serial.println("SD.begin() Success.");
root = SD.open("/");
printDirectory(root, 0);
Serial.println("===============================");
// open the file for reading:
myFile = SD.open("hello.txt");
if (myFile) {
Serial.println("test.txt:");
Serial.println("-------------------------------");
// read from the file until there's nothing else in it:
while (myFile.available()) {
//Serial.write(myFile.read());
byte b = myFile.read();
Serial.write(b);
tft.print((char)b);
}
// close the file:
myFile.close();
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
Serial.println("\n===============================\n");
}
//----------------------------------------
Serial.println("\n- End of setup() -\n");
}
void loop() {
delay(100);
}
//
// printDirectory() copy from:
// Examples > SD > listfiles
//
void printDirectory(File dir, int numTabs) {
while (true) {
File entry = dir.openNextFile();
if (! entry) {
// no more files
break;
}
for (uint8_t i = 0; i < numTabs; i++) {
Serial.print('\t');
}
Serial.print(entry.name());
if (entry.isDirectory()) {
Serial.println("/");
printDirectory(entry, numTabs + 1);
} else {
// files have sizes, directories do not
Serial.print("\t\t");
Serial.println(entry.size(), DEC);
}
entry.close();
}
}
/***************************************************
* nano RP2040 Connect exercise
* + ST7745/SD
*
* On Arduino nanp RP2040 Connect:
* (CIPO/MISO) - D12
* (COPI/MOSI) - D11
* (SCK) - D13
* (CS/SS) - Any GPIO (except for A6/A7
*
* This example drive ST7735 SPI and SD using hardware SPI
*
* TFT_ST7735 nano RP2040 Connect
* ------------------------------
* VCC 3V3
* GND GND
* CS 10
* RESET 9
* A0(DC) 8
* SDA 11
* SCK 13
* LED 3V3
*
* TFT_ST7735 nano RP2040 Connect
* ------------------------------
* SD_CS 4
* SD_MOSI 11
* SD_MISO 12
* SD_SCK 13
****************************************************/
#include <Adafruit_GFX.h>
#include <Adafruit_ST7735.h>
#include <SD.h>
#include <SPI.h>
// TFT display and SD card will share the hardware SPI interface.
// Hardware SPI pins are specific to the Arduino board type.
#define SD_CS 4 // Chip select line for SD card
#define TFT_CS 10 // Chip select line for TFT display
#define TFT_DC 8 // Data/command line for TFT
#define TFT_RST 9 // Reset line for TFT (or connect to +5V)
Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);
File root;
File myFile;
void setup(void) {
delay(1000);
Serial.begin(115200);
delay(1000);
Serial.println("=====================");
Serial.println("- setup() -");
tft.initR(INITR_BLACKTAB);
Serial.println("tft: "
+ String(tft.width()) + " : "
+ String(tft.height()));
tft.fillScreen(ST7735_BLACK);
tft.setTextWrap(true);
tft.setTextColor(ST77XX_WHITE);
tft.setCursor(0, 0);
tft.print("Arduino nano RP2040 Connect");
//----------------------------------------
Serial.println("Initializing SD card...");
if (!SD.begin(SD_CS)){
Serial.println("SD.begin() failed!");
}
else{
Serial.println("SD.begin() Success.");
root = SD.open("/");
printDirectory(root, 0);
}
//----------------------------------------
Serial.println("\n- End of setup() -\n");
}
const int NO_OF_BMP = 3;
char* bmpFiles[NO_OF_BMP] = {"/test.bmp", "/test2.bmp", "/test3.bmp"};
void loop() {
for(int i=0; i<NO_OF_BMP; i++){
bmpDraw(bmpFiles[i], 0, 0);
delay(2000);
}
}
//
// printDirectory() copy from:
// Examples > SD > listfiles
//
void printDirectory(File dir, int numTabs) {
while (true) {
File entry = dir.openNextFile();
if (! entry) {
// no more files
break;
}
for (uint8_t i = 0; i < numTabs; i++) {
Serial.print('\t');
}
Serial.print(entry.name());
if (entry.isDirectory()) {
Serial.println("/");
printDirectory(entry, numTabs + 1);
} else {
// files have sizes, directories do not
Serial.print("\t\t");
Serial.println(entry.size(), DEC);
}
entry.close();
}
}
//
// bmpDraw() copy from:
// Examples under "Adafruit ST7735 and ST7789 Library" > shieldtest
//
// This function opens a Windows Bitmap (BMP) file and
// displays it at the given coordinates. It's sped up
// by reading many pixels worth of data at a time
// (rather than pixel by pixel). Increasing the buffer
// size takes more of the Arduino's precious RAM but
// makes loading a little faster. 20 pixels seems a
// good balance.
#define BUFFPIXEL 20
void bmpDraw(char *filename, uint8_t x, uint8_t 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 buffer (R+G+B 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();
if((x >= tft.width()) || (y >= tft.height())) return;
Serial.println();
Serial.print("Loading image '");
Serial.print(filename);
Serial.println('\'');
// Open requested file on SD card
if ((bmpFile = SD.open(filename)) == NULL) {
Serial.print("File not found");
return;
}
// Parse BMP header
if(read16(bmpFile) == 0x4D42) { // BMP signature
Serial.print("File size: "); Serial.println(read32(bmpFile));
(void)read32(bmpFile); // Read & ignore creator bytes
bmpImageoffset = read32(bmpFile); // Start of image data
Serial.print("Image Offset: "); Serial.println(bmpImageoffset, DEC);
// Read DIB header
Serial.print("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
Serial.print("Bit Depth: "); Serial.println(bmpDepth);
if((bmpDepth == 24) && (read32(bmpFile) == 0)) { // 0 = uncompressed
goodBmp = true; // Supported BMP format -- proceed!
Serial.print("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) >= tft.width()) w = tft.width() - x;
if((y+h-1) >= tft.height()) h = tft.height() - y;
// Set TFT address window to clipped image bounds
tft.startWrite();
tft.setAddrWindow(x, y, w, h);
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?
tft.endWrite();
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
bmpFile.read(sdbuffer, sizeof(sdbuffer));
buffidx = 0; // Set index to beginning
tft.startWrite();
}
// Convert pixel from BMP to TFT format, push to display
b = sdbuffer[buffidx++];
g = sdbuffer[buffidx++];
r = sdbuffer[buffidx++];
tft.pushColor(tft.color565(r,g,b));
} // end pixel
} // end scanline
tft.endWrite();
Serial.print("Loaded in ");
Serial.print(millis() - startTime);
Serial.println(" ms");
} // end goodBmp
}
}
bmpFile.close();
if(!goodBmp) Serial.println("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;
}
// Adafruit_ImageReader test for Adafruit ST7735 TFT Breakout for Arduino.
// Demonstrates loading images from SD card or flash memory to the screen,
// to RAM, and how to query image file dimensions.
// Requires three BMP files in root directory of SD card:
// test.bmp, test2.bmp and test3.bmp.
// As written, this uses the microcontroller's SPI interface for the screen
// (not 'bitbang') and must be wired to specific pins.
#include <Adafruit_GFX.h> // Core graphics library
#include <Adafruit_ST7735.h> // Hardware-specific library
#include <SdFat.h> // SD card & FAT filesystem library
#include <Adafruit_ImageReader.h> // Image-reading functions
// TFT display and SD card share the hardware SPI interface, using
// 'select' pins for each to identify the active device on the bus.
#define SD_CS 4 // SD card select pin
#define TFT_CS 10 // TFT select pin
#define TFT_DC 8 // TFT display/command pin
#define TFT_RST 9 // Or set to -1 and connect to Arduino RESET pin
SdFat SD; // SD card filesystem
Adafruit_ImageReader reader(SD); // Image-reader object, pass in SD filesys
Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);
Adafruit_Image img; // An image loaded into RAM
int32_t width = 0, // BMP image dimensions
height = 0;
void setup(void) {
ImageReturnCode stat; // Status from image-reading functions
Serial.begin(115200);
tft.initR(INITR_BLACKTAB); // Initialize screen
// The Adafruit_ImageReader constructor call (above, before setup())
// accepts an uninitialized SdFat or FatFileSystem object. This MUST
// BE INITIALIZED before using any of the image reader functions!
Serial.print(F("Initializing filesystem..."));
// SD card is pretty straightforward, a single call...
if(!SD.begin(SD_CS, SD_SCK_MHZ(10))) { // Breakouts require 10 MHz limit due ...
Serial.println(F("SD begin() failed"));
for(;;); // Fatal error, do not continue
}
Serial.println(F("OK!"));
// Fill screen blue. Not a required step, this just shows that we're
// successfully communicating with the screen.
tft.fillScreen(ST7735_BLUE);
// Load full-screen BMP file 'test.bmp' at position (0,0) (top left).
// Notice the 'reader' object performs this, with 'tft' as an argument.
Serial.print(F("Loading test.bmp to screen..."));
stat = reader.drawBMP("/test.bmp", tft, 0, 0);
reader.printStatus(stat); // How'd we do?
// Query the dimensions of image 'test2.bmp' WITHOUT loading to screen:
Serial.print(F("Querying test2.bmp image size..."));
stat = reader.bmpDimensions("/test2.bmp", &width, &height);
reader.printStatus(stat); // How'd we do?
if(stat == IMAGE_SUCCESS) { // If it worked, print image size...
Serial.print(F("Image dimensions: "));
Serial.print(width);
Serial.write('x');
Serial.println(height);
}
// Load small BMP 'test3.bmp' into a GFX canvas in RAM.
Serial.print(F("Loading test3.bmp to canvas..."));
stat = reader.loadBMP("/test3.bmp", img);
reader.printStatus(stat); // How'd we do?
delay(2000); // Pause 2 seconds before moving on to loop()
}
void loop() {
for(int r=0; r<4; r++) { // For each of 4 rotations...
tft.setRotation(r); // Set rotation
tft.fillScreen(0); // and clear screen
reader.drawBMP("/test2.bmp", tft, 0, 0);
delay(1000); // Pause 1 sec.
img.draw(tft, // Pass in tft object
0 , // Horiz pos.
0); // Vert pos
delay(2000); // Pause 2 sec.
}
}
Related: