Moderators: adafruit_support_bill, adafruit
/**************************************************************************/
/*!
@brief Converts a 24-bit RGB color to an equivalent 16-bit RGB565 value
@param[in] r
8-bit red
@param[in] g
8-bit green
@param[in] b
8-bit blue
@section Example
@code
// Get 16-bit equivalent of 24-bit color
uint16_t gray = drawRGB24toRGB565(0x33, 0x33, 0x33);
@endcode
*/
/**************************************************************************/
uint16_t drawRGB24toRGB565(uint8_t r, uint8_t g, uint8_t b)
{
return ((r / 8) << 11) | ((g / 4) << 5) | (b / 8);
}/**************************************************************************/
/*!
@brief Converts a 16-bit RGB565 color to a standard 32-bit BGRA32
color (with alpha set to 0xFF)
@param[in] color
16-bit rgb565 color
@section Example
@code
// First convert 24-bit color to RGB565
uint16_t rgb565 = drawRGB24toRGB565(0xFF, 0x00, 0x00);
// Convert RGB565 color back to BGRA32
uint32_t bgra32 = drawRGB565toBGRA32(rgb565);
// Display results
printf("BGRA32: 0x%08X R: %u G: %u B: %u A: %u \r\n",
bgra32,
(bgra32 & 0x000000FF), // Blue
(bgra32 & 0x0000FF00) >> 8, // Green
(bgra32 & 0x00FF0000) >> 16, // Red
(bgra32 & 0xFF000000) >> 24); // Alpha
@endcode
*/
/**************************************************************************/
uint32_t drawRGB565toBGRA32(uint16_t color)
{
uint32_t bits = (uint32_t)color;
uint32_t blue = bits & 0x001F; // 5 bits blue
uint32_t green = bits & 0x07E0; // 6 bits green
uint32_t red = bits & 0xF800; // 5 bits red
// Return shifted bits with alpha set to 0xFF
return (red << 8) | (green << 5) | (blue << 3) | 0xFF000000;
}#include "TFTLCD.h"
#include "TouchScreen.h"
/* For the 8 data pins:
Duemilanove/Diecimila/UNO/etc ('168 and '328 chips) microcontoller:
D0 connects to digital 8
D1 connects to digital 9
D2 connects to digital 2
D3 connects to digital 3
D4 connects to digital 4
D5 connects to digital 5
D6 connects to digital 6
D7 connects to digital 7
For Mega's use pins 22 thru 29 (on the double header at the end)
*/
#define YP A3 // must be an analog pin, use "An" notation!
#define XM A2 // must be an analog pin, use "An" notation!
#define YM 9 // can be a digital pin
#define XP 8 // can be a digital pin
#define TS_MINX 150
#define TS_MINY 120
#define TS_MAXX 920
#define TS_MAXY 940
// For better pressure precision, we need to know the resistance
// between X+ and X- Use any multimeter to read it
// For the one we're using, its 300 ohms across the X plate
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
#define LCD_CS A3
#define LCD_CD A2
#define LCD_WR A1
#define LCD_RD A0
// optional
#define LCD_RESET A4
// Color definitions
#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 JJCOLOR 0x1CB6
TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);
int i = 0;
int backlight = 10;
int blv = 255;
long r;
long g;
long b;
char red[10];
char green[10];
char blue[10];
char output[10];
unsigned long answer;
void setup(void) {
pinMode(backlight, OUTPUT);
Serial.begin(9600);
tft.setRotation(1);
tft.reset();
uint16_t identifier = tft.readRegister(0x0);
if (identifier == 0x9325) {
Serial.println("Found ILI9325 controller.");
}
else if (identifier == 0x9328) {
Serial.println("Found ILI9328 controller.");
}
else {
Serial.print("Unknown driver chip ");
Serial.println(identifier, HEX);
while (1);
}
tft.initDisplay();
tft.fillScreen(BLACK);
tft.fillRect(71, 70, 50, 100, JJCOLOR);
tft.fillRect(134, 70, 50, 100, JJCOLOR);
tft.fillRect(197, 70, 50, 100, JJCOLOR);
tft.drawRect(46, 45, 228, 150, WHITE);
for(i = 0 ; i <= blv; i+=1) {
analogWrite(10, i);
delay(2);
}
delay(250);
tft.drawString(85, 100, "J", WHITE, 5);
delay(250);
tft.drawString(147, 100, "O", WHITE, 5);
delay(250);
tft.drawString(210, 100, "S", WHITE, 5);
delay(500);
tft.drawString(84, 210, "Jeremy Saglimbeni (c) 2011", WHITE);
tft.drawString(123, 230, "jandjpro.com", WHITE);
delay(500);
tft.drawString(108, 20,"Thanks ktownsend!!!", WHITE);
delay(1500);
tft.fillScreen(BLACK);
drawscreen();
showred();
showgrn();
showblu();
tft.fillRect(0, 0, 320, 10, JJCOLOR);
tft.drawString(247, 223, "0x", WHITE, 2);
tft.drawRect(244, 220, 76, 20, WHITE);
tft.drawString(252, 207, "HEX Output", WHITE);
tft.drawString(60, 207, "Preview Swatch", WHITE);
tft.drawString(60, 217, "(Press to reset)", WHITE);
showhex();
tft.drawString(1, 1, "8:8:8 RGB to 5:6:5 HEX Color Converter - jersagfast", WHITE);
}
#define MINPRESSURE 10
#define MAXPRESSURE 1000
void loop() {
Point p = ts.getPoint();
// if you're sharing pins, you'll need to fix the directions of the touchscreen pins!
//pinMode(XP, OUTPUT);
pinMode(XM, OUTPUT);
pinMode(YP, OUTPUT);
//pinMode(YM, OUTPUT);
// we have some minimum pressure we consider 'valid'
// pressure of 0 means no pressing!
if (p.z > MINPRESSURE && p.z < MAXPRESSURE) {
/*
Serial.print("X = ");
Serial.print(p.x);
Serial.print("\tY = ");
Serial.print(p.y);
Serial.print("\tPressure = ");
Serial.println(p.z);
*/
// turn from 0->1023 to tft.width
p.x = map(p.x, TS_MINX, TS_MAXX, 240, 0);
p.y = map(p.y, TS_MINY, TS_MAXY, 320, 0);
/*
Serial.print("(");
Serial.print(p.x);
Serial.print(", ");
Serial.print(p.y);
Serial.println(")");
*/
// red buttons
if (p.y > 3 && p.y < 65 && p.x > 210 && p.x < 264) {
r = r - 1;
if (r <= 0) {
r = 0;
}
showred();
showhex();
}
if (p.y > 269 && p.y < 324 && p.x > 210 && p.x < 264) {
r = r + 1;
if (r >= 255) {
r = 255;
}
showred();
showhex();
}
// green buttons
if (p.y > 3 && p.y < 65 && p.x > 139 && p.x < 195) {
g = g - 1;
if (g <= 0) {
g = 0;
}
showgrn();
showhex();
}
if (p.y > 269 && p.y < 324 && p.x > 139 && p.x < 195) {
g = g + 1;
if (g >= 255) {
g = 255;
}
showgrn();
showhex();
}
// blue buttons
if (p.y > 3 && p.y < 66 && p.x > 72 && p.x < 126) {
b = b - 1;
if (b <= 0) {
b = 0;
}
showblu();
showhex();
}
if (p.y > 269 && p.y < 324 && p.x > 72 && p.x < 126) {
b = b + 1;
if (b >= 255) {
b = 255;
}
showblu();
showhex();
}
// reset button
if (p.y > 1 && p.y < 55 && p.x > 12 && p.x < 57) {
r = 0;
g = 0;
b = 0;
showred();
showgrn();
showblu();
showhex();
}
/*
// uncomment this to get serial output when screen is pressed
Serial.print("r:");
Serial.print(r);
Serial.print(" g:");
Serial.print(g);
Serial.print(" b:");
Serial.println(b);
Serial.print("HEX Output:0x");
Serial.println(answer, HEX);
*/
}
}
void showred() {
itoa (r, red, 10);
tft.fillRect(144, 45, 36, 16, BLACK);
tft.drawString(144, 45, red, WHITE, 2);
}
void showgrn() {
itoa (g, green, 10);
tft.fillRect(144, 105, 36, 16, BLACK);
tft.drawString(144, 105, green, WHITE, 2);
}
void showblu() {
itoa (b, blue, 10);
tft.fillRect(144, 165, 36, 16, BLACK);
tft.drawString(144, 165, blue, WHITE, 2);
}
void showhex() {
answer = ((r / 8) << 11) | ((g / 4) << 5) | (b / 8); //Thanks ktownsend for this!
tft.fillRect(0, 200, 50, 40, answer);
itoa (answer, output, HEX);
tft.fillRect(271, 223, 46, 16, BLACK);
tft.drawString(271, 223, output, WHITE, 2);
}
void drawscreen() {
// Red
tft.fillRect(0, 20, 60, 50, RED);
tft.drawRect(0, 20, 60, 50, WHITE);
tft.drawRect(80, 20, 160, 50, JJCOLOR);
tft.fillRect(260, 20, 60, 50, GREEN);
tft.drawRect(260, 20, 60, 50, WHITE);
tft.drawString(22, 33, "-", WHITE, 3);
tft.drawString(134, 31, "RED Value", RED);
tft.drawString(282, 33, "+", WHITE, 3);
//Green
tft.fillRect(0, 80, 60, 50, RED);
tft.drawRect(0, 80, 60, 50, WHITE);
tft.drawRect(80, 80, 160, 50, JJCOLOR);
tft.fillRect(260, 80, 60, 50, GREEN);
tft.drawRect(260, 80, 60, 50, WHITE);
tft.drawString(22, 93, "-", WHITE, 3);
tft.drawString(128, 91, "Green Value", GREEN);
tft.drawString(282, 93, "+", WHITE, 3);
//Blue
tft.fillRect(0, 140, 60, 50, RED);
tft.drawRect(0, 140, 60, 50, WHITE);
tft.drawRect(80, 140, 160, 50, JJCOLOR);
tft.fillRect(260, 140, 60, 50, GREEN);
tft.drawRect(260, 140, 60, 50, WHITE);
tft.drawString(22, 153, "-", WHITE, 3);
tft.drawString(130, 151, "Blue Value", BLUE);
tft.drawString(282, 153, "+", WHITE, 3);
}
Return to Other Adafruit products
Users browsing this forum: No registered users and 5 guests