hi guys i have buyed a ST7789 IPS LCD(if you have one,put vcc to 5v it fixes display without rese)
i did a text terminal with it but i cant do commands i use 4x4 keypad and i have problems with keypad and string
#include <Keypad.h>
#include <Adafruit_ST7735.h>
#include <Adafruit_ST7789.h>
#include <Adafruit_ST77xx.h>
#include <Adafruit_GFX.h>
#include <SPI.h>
const byte ROWS = 4;
const byte COLS = 4;
byte rowPins[ROWS] = {2, 4, 7, A0};
byte colPins[COLS] = {A1, A2, A3, A4};
char Keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
Keypad keypad = Keypad( makeKeymap(Keys), rowPins, colPins, ROWS, COLS );
// ST7789 TFT module connections
#define TFT_CS 10
#define TFT_RST 8 // define reset pin, or set to -1 and connect to Arduino RESET pin
#define TFT_DC 9 // define data/command pin
// Initialize Adafruit ST7789 TFT library
Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, TFT_RST);
float p = 3.1415926;
void setup(void) {
Serial.begin(9600);
// if the display has CS pin try with SPI_MODE0
tft.init(240, 240, SPI_MODE2); // Init ST7789 display 240x240 pixel
// if the screen is flipped, remove this command
tft.setRotation(2);
uint16_t time = millis();
tft.fillScreen(ST77XX_BLACK);
time = millis() - time;
// large block of text
tft.setCursor(0, 0);
tft.fillScreen(ST77XX_BLACK);
tft.setTextSize(2);
tft.setTextWrap(true);
tft.setTextColor(ST77XX_WHITE);
tft.print("Komut Giriniz_ ");
}
String keycode="";
void tftchar(char text, uint16_t color) {
tft.setTextColor(color);
tft.setTextWrap(true);
tft.print(text);
delay(0);
}
int i=0;
char code[1];
void loop() {
char key = keypad.getKey();
uint16_t colortext=ST77XX_WHITE;
if (key)
{
tftchar(key,colortext);
code=code+key;
if (code="AC") {
colortext=ST77XX_GREEN;
code="";
}
}
}
This is an array that can hold a single character. Can you see a problem ?
Declare the array large enough to hold your longest possible string + 1 character. Use a variable initially set to zero as the index to the array
When you get a character from the keypad put it into the array at the current index position, increment the index variable and put a '\0' in the array at that position to mark the end of the input so far
Keep doing that until you either receive the number of characters that you need or you get a special character such as '*'
At that point you will have a C style string that you can print
/*
read the keypad and build a cString
Returns:
true if '#' is entered else false
*/
bool readKeypad()
{
char ch = customKeypad.getKey();
static uint8_t index;
// clear the keypad buffer when indexis zero
if (index == 0)
{
memset(keypadBuffer, '\0', sizeof(keypadBuffer));
}
// if a key was pressed
if (ch)
{
// if 'backspace'
if (ch == '*' && index > 0)
{
// remove character
keypadBuffer[index - 1] = '\0';
index--;
// done
return false;
}
// if 'enter'
if (ch == '#')
{
// add terminating '\0' to be sure
keypadBuffer[index] = '\0';
// reset index
index = 0;
// indicate complete
return true;
}
// if buffer not full
if (index < sizeof(keypadBuffer) - 1)
{
// add to cString
keypadBuffer[index] = ch;
index++;
}
}
return false;
}
keypadBuffer is a (global) character array, not a String.
// store keypad input; up to 15 characters
char keypadBuffer[16];
consider -- look at how "key" is handled inside of "loop()", how the code array is prepared for the next input by setting idx to zero and the '#" is overwritten. "strcmp()" returns zero with there is a match, hence the "! strcmp()"
#include <stdio.h>
#include <string.h>
const int CodeLen = 20;
char code [CodeLen];
int idx = 0;
void
loop (void)
{
char key = getchar ();
if (key) {
code [idx++] = key;
if (CodeLen == idx || '#' == key) {
code [idx-1] = '\0'; // overwrite '#' w/ NUL
idx = 0;
if (!strcmp ("AC", code))
printf (" AC found\n");
else
printf (" unknown\n");
}
}
}
int
main ()
{
while (1)
loop ();
return 0;
}