Hi,
I'm using following Components:
https://learn.adafruit.com/getting-star ... troduction
https://learn.adafruit.com/adafruit-3-5 ... t/overview
The Display is used in SPI mode so I can also use the sd card slot which I also need for my project. I have changed my Code yesterday but now The SD Card isn't working anymore and the Bluetooth device is also not initializing and doesn't advertising.
My .ino Sketch
Code: Select all
/*
PIN Configuration:
=====================================
Bluetooth:
- TXD --> 17
- RXD --> 16
- VCC --> 5 Volt
- GND --> GND
Temperatursensor:
- Signalpin (Mitte) --> A12 Prototypplatine = A2 (innentemperatur) und A4 (aussentemperatur)
- VCC (links) --> 5 Volt
- GND (rechts) --> GND
Potentiometer:
- VCC (links) --> 5 Volt
- GND (rechts) --> GND
- Signal (Mitte) --> A0
TFT (Pins in der Reihenfolge auf Platine SPI):
- GND --> GND
- 3-5V --> 5 Volt
- 3.3V -->
- CLK --> 52
- MISO --> 50
- MOSI --> 51
- CS --> 10 Prototypplatine = 38
- D/C --> 9 Prototypplatine = 36
- RST --> 8 Prototypplatine = 46
- Lite -->
- Y+ -->
- X+ -->
- Y- -->
- X- -->
- GND -->
- IM2 --> 3.3V
- IM1 -->
- IM0 -->
- Card CS --> 4 Prototypplatine = 26
- Card Detect -->
Bluetooth LE (Nur Prototypplatine)
- Vin --> 5 Volt
- Gnd --> GND
- SCK --> 52
- MISO --> 50
- MOSI --> 51
- REQ --> 30
- RDY --> 2
- RST --> 7
*/
#include <Arduino.h>
#include <Adafruit_GFX.h> // Core graphics library
#include "Adafruit_HX8357.h"
#include <SPI.h>
#include <SD.h>
#include "PictureWork.h"
#include "Adafruit_BLE_UART.h"
// Klasse fuer Grafiksachen
PictureWork* pw;
// Temperatur
// Frequenzlesen
#define FREQMAX 1000
volatile unsigned long freq1 = 0;
volatile unsigned long lastread1 = 0;
volatile unsigned long freq2 = 0;
volatile unsigned long lastread2 = 0;
// Bluetooth LE
#define ADAFRUITBLE_REQ 30
#define ADAFRUITBLE_RDY 2
#define ADAFRUITBLE_RST 7
Adafruit_BLE_UART BTLEserial = Adafruit_BLE_UART(ADAFRUITBLE_REQ, ADAFRUITBLE_RDY, ADAFRUITBLE_RST);
aci_evt_opcode_t laststatus = ACI_EVT_DISCONNECTED; // Letzten Status um aenderungen zu erkennen
String bluetoothString = "";
// SPI CHIPs
#define SPI_BT ADAFRUITBLE_REQ
#define SPI_TFT 38
#define SPI_SD 26
void setup(void) {
interrupts();
Serial.begin(9600);
// SPI stuff
pinMode(SPI_TFT, OUTPUT);
pinMode(SPI_SD, OUTPUT);
pinMode(SPI_BT, OUTPUT);
digitalWrite(SPI_TFT, LOW);
digitalWrite(SPI_SD, LOW);
digitalWrite(SPI_BT, HIGH);
// Interruptpins vorbereiten
pinMode(20, INPUT);
digitalWrite(20, HIGH);
pinMode(21, INPUT);
digitalWrite(21, HIGH);
// Interrupts aktivieren
attachInterrupt(2, interruptReact2, FALLING); // PIN 21
attachInterrupt(3, interruptReact1, FALLING); // PIN 20
// TFT und SD initialisieren und Boot Bildschirm anzeigen
pw = new PictureWork(36, 38, 46);
// Bluetooth starten
activateBluetooth(true);
BTLEserial.begin();
activateBluetooth(false);
}
void loop() {
// Bluetooth Kram
doBluetoothStuff();
// Temperatur auslesen und anzeigen
String innentemperatur = readTemp("Innentemperatur", A2);
String aussentemperatur = readTemp("Aussentemperatur", A4);
pw->printNormalString(10, 280, innentemperatur);
pw->printNormalString(10, 300, aussentemperatur);
// Geschwindigkeit anzeigen
printSpeed();
}
// Bluetooth kram machen
void doBluetoothStuff() {
activateBluetooth(true);
// Tell the nRF8001 to do whatever it should be working on.
BTLEserial.pollACI();
// Ask what is our current status
aci_evt_opcode_t status = BTLEserial.getState();
// If the status changed....
if (status != laststatus) {
// print it out!
if (status == ACI_EVT_DEVICE_STARTED) {
Serial.println(F("* Advertising started"));
}
if (status == ACI_EVT_CONNECTED) {
Serial.println(F("* Connected!"));
}
if (status == ACI_EVT_DISCONNECTED) {
Serial.println(F("* Disconnected or advertising timed out"));
}
// OK set the last status change to this one
laststatus = status;
}
if (status == ACI_EVT_CONNECTED) {
// Lets see if there's any data for us!
if (BTLEserial.available()) {
Serial.print("* "); Serial.print(BTLEserial.available()); Serial.println(F(" bytes available from BTLE"));
bluetoothString = "";
}
// OK while we still have something to read, get a character and print it out
while (BTLEserial.available()) {
char c = BTLEserial.read();
bluetoothString += c;
}
// String auffuellen (maximal 40 Zeichen)
for(int i=bluetoothString.length(); i < 40; i++) {
bluetoothString += " ";
}
}
activateBluetooth(false);
}
// Bluetooth / TFT wechseln
void activateBluetooth(boolean activate) {
if(activate) {
digitalWrite(SPI_TFT, HIGH);
digitalWrite(SPI_SD, HIGH);
digitalWrite(SPI_BT, LOW);
} else {
digitalWrite(SPI_BT, HIGH);
digitalWrite(SPI_TFT, LOW);
digitalWrite(SPI_SD, LOW);
}
}
// Geschwindigkeit auslesen und ausgeben
void printSpeed() {
int kmph = map(freq1, 0, 213, 0, 305);
String output = "";
output += kmph;
output += " km/h";
pw->printBigString(10, 30, output);
freq1 = 0;
}
// Temperatur auslesen und in Format "<title>:
String readTemp(const char* title, int pin) {
// 5 Samples pro Messung und dann der Mittelwert
float temp[5];
for(int i=0; i<5; i++) {
temp[i] = (5.0 * analogRead(pin) * 100.0) / 1024;
delay(20);
}
float tempC = (temp[1] + temp[2] + temp[3] + temp[4] + temp[5])/5;
// Text zusammenbauen
String output = "";
output += title;
output += ": ";
output += (byte)tempC;
output += (char)247; // Degree sign
output += "C ";
return output;
}
void interruptReact1() {
unsigned long newFreq = 1000 / (millis() - lastread1);
if(newFreq < FREQMAX) {
freq1 = newFreq;
}
lastread1 = millis();
}
void interruptReact2() {
unsigned long newFreq = 1000 / (millis() - lastread2);
if(newFreq < FREQMAX) {
freq2 = newFreq;
}
lastread2 = millis();
}
PictureWork.h
Code: Select all
#ifndef PICTUREWORK_H
#define PICTUREWORK_H
#include <Arduino.h>
#include <Adafruit_GFX.h> // Core graphics library
#include "Adafruit_HX8357.h"
#include <SPI.h>
#include <SD.h>
// TFT display and SD card will share the hardware SPI interface.
// Hardware SPI pins are specific to the Arduino board type and
// cannot be remapped to alternate pins. For Arduino Uno,
// Duemilanove, etc., pin 11 = MOSI, pin 12 = MISO, pin 13 = SCK.
struct ImagePixel {
uint8_t b, g, r;
};
class PictureWork {
private:
Adafruit_HX8357* tft;
ImagePixel balkenData[140];
int8_t dc_pin, cs_pin, rst_pin;
void printString(int x, int y, String text, int textSize);
public:
PictureWork(int8_t dc, int8_t cs, int8_t rst);
void initTft();
Adafruit_HX8357* getTft();
void loadBalken();
void drawBalken(int x, int y);
void bmpDraw(char *filename, uint8_t x, uint16_t y);
uint16_t read16(File &f);
uint32_t read32(File &f);
void printBigString(int x, int y, String text);
void printNormalString(int x, int y, String text);
};
#endif
PictureWork.cpp
Code: Select all
#include "PictureWork.h"
#include <Adafruit_GFX.h> // Core graphics library
#include "Adafruit_HX8357.h"
#include <SPI.h>
#include <SD.h>
#define SD_CS 26
#define BUFFPIXEL 20
PictureWork::PictureWork(int8_t dc, int8_t cs, int8_t rst) {
dc_pin = dc;
cs_pin = cs;
rst_pin = rst;
initTft();
}
void PictureWork::printBigString(int x, int y, String text) {
printString(x, y, text, 5);
}
void PictureWork::printNormalString(int x, int y, String text) {
printString(x, y, text, 2);
}
void PictureWork::printString(int x, int y, String text, int textSize) {
int str_len = text.length()+1;
char strBuffer[str_len];
text.toCharArray(strBuffer, str_len);
tft->setCursor(x,y);
tft->setTextSize(textSize);
tft->setTextColor(HX8357_WHITE, HX8357_BLACK);
tft->print(strBuffer);
}
Adafruit_HX8357* PictureWork::getTft() {
return tft;
}
/*
* Initialisiert den TFT Bildschirm, Laedt die Daten und zeigt ein Boot-Logo an
*/
void PictureWork::initTft() {
// Adafruit_HX8357(int8_t _CS, int8_t _DC, int8_t _MOSI, int8_t _SCLK,
// int8_t _RST, int8_t _MISO);
tft = new Adafruit_HX8357(cs_pin, dc_pin, rst_pin);
tft->begin(HX8357D);
tft->fillScreen(HX8357_WHITE);
Serial.print("Initializing SD card...");
if (!SD.begin(SD_CS)) {
Serial.println("failed!");
}
Serial.println("OK!");
tft->setRotation(1);
// Boot Bildschirm anzeigen
bmpDraw("boot.bmp", 110, 70);
loadBalken();
delay(1000);
// Ready for gauges
tft->fillScreen(HX8357_BLACK);
tft->setTextColor(HX8357_WHITE, HX8357_BLACK);
}
void PictureWork::loadBalken() {
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
uint8_t sdbuffer[3*BUFFPIXEL]; // pixel buffer (R+G+B per pixel)
uint32_t rowSize; // Not always = bmpWidth; may have padding
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();
int balkenLoadIndex = 0;
Serial.println();
Serial.println(F("Loading image balken.bmp"));
// Open requested file on SD card
if ((bmpFile = SD.open("balken.bmp")) == NULL) {
Serial.print(F("File not found"));
return;
}
// Parse BMP header
if(read16(bmpFile) == 0x4D42) { // BMP signature
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'
bmpDepth = read16(bmpFile); // bits per pixel
if((bmpDepth == 24) && (read32(bmpFile) == 0)) { // 0 = uncompressed
// 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;
Serial.print("Balkenbreite: ");
Serial.println(bmpWidth);
Serial.print("Balkenhoehe: ");
Serial.println(bmpHeight);
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 pixel...
// Time to read more pixel data?
if (buffidx >= sizeof(sdbuffer)) { // Indeed
bmpFile.read(sdbuffer, sizeof(sdbuffer));
buffidx = 0; // Set index to beginning
}
// Convert pixel from BMP to TFT format, push to display
balkenData[balkenLoadIndex++] = { sdbuffer[buffidx++], sdbuffer[buffidx++], sdbuffer[buffidx++] };
} // end pixel
} // end scanline
Serial.print(F("Loaded in "));
Serial.print(millis() - startTime);
Serial.println(" ms");
} // end goodBmp
}
}
bmpFile.close();
if(!goodBmp) Serial.println(F("BMP format not recognized."));
}
void PictureWork::drawBalken(int x, int y) {
int row, col;
int w = 5, h = 20;
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->setAddrWindow(x, y, x+w-1, y+h-1);
int dataIndex = 0;
for (row=0; row<20; row++) { // For each scanline...
for (col=0; col<5; col++) { // For each pixel...
ImagePixel pixel = balkenData[dataIndex++];
tft->pushColor(tft->color565(pixel.r,pixel.g,pixel.b));
} // end pixel
}
}
// 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.
void PictureWork::bmpDraw(char *filename, uint8_t x, uint16_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(F("Loading image '"));
Serial.print(filename);
Serial.println('\'');
// Open requested file on SD card
if ((bmpFile = SD.open(filename)) == NULL) {
Serial.print(F("File not found"));
return;
}
// Parse BMP header
if(read16(bmpFile) == 0x4D42) { // BMP signature
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'
bmpDepth = read16(bmpFile); // bits per pixel
Serial.print(F("Bit Depth: ")); Serial.println(bmpDepth);
if((bmpDepth == 24) && (read32(bmpFile) == 0)) { // 0 = uncompressed
goodBmp = true; // Supported BMP format -- proceed!
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;
}
// 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->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 pixel...
// Time to read more pixel data?
if (buffidx >= sizeof(sdbuffer)) { // Indeed
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++];
tft->pushColor(tft->color565(r,g,b));
} // end pixel
} // end scanline
Serial.print(F("Loaded in "));
Serial.print(millis() - startTime);
Serial.println(" ms");
} // end goodBmp
}
}
bmpFile.close();
if(!goodBmp) Serial.println(F("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 PictureWork::read16(File &f) {
uint16_t result;
((uint8_t *)&result)[0] = f.read(); // LSB
((uint8_t *)&result)[1] = f.read(); // MSB
return result;
}
uint32_t PictureWork::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;
}
I hope you can help me.
Kind regards
Thomas