// Write weekday to screen void ClockMaster::writeWeekDay(DateTime time_now){ tft.stroke(weekdayColor); tft.setTextSize(weekdaySize); tft.setCursor(weekdayXPos, weekdayYPos); tft.print(daysOfTheWeek[time_now.dayOfTheWeek()]); }
int main(int argc, char **argv) { // TFT tft ( 21 ,22 ,23 ); // with wiringpi library TFT tft ( 5, 6, 13 ); // with bcm library zou can set in ILI9341.c file tft.init(); tft.setAddrWindow( 0,0, 320, 240 ); Ball balls[100]; for ( int i =0; i < 100; i ++ ) { balls[i].x = rand () % 280 +20; balls[i].y = rand () % 200 +20; balls[i].color = rand () % 65500; balls[i].r = 2 + rand () % 8; } do { for ( int i =0; i < 100; i ++ ) { balls[i].update(); tft.fillCircle ( balls[i].x, 240 - balls[i].y, balls[i].r , balls[i].color); } tft.updateDisplay (); } while(1); return 0; }
// Clear (draw black) 9x9 square void Board::clearPoint(int xPoint, int yPoint){ TFTscreen.stroke(0, 0, 0); for(int i = -3; i <= 3; i++){ for(int j = -3; j <= 3; j++){ TFTscreen.point(xPoint + i, yPoint + j); } } }
// Write minute to screen void ClockMaster::writeMinute(DateTime time_now) { tft.stroke(minuteColor); tft.setTextSize(hourSize); tft.setCursor(minuteXPos, minuteYPos); if (time_now.minute() < 10) { tft.print("0"); } tft.print(time_now.minute()); }
// Write second to screen void ClockMaster::writeSecond(DateTime time_now) { tft.stroke(255, 255, 255); tft.setTextSize(secondSize); tft.setCursor(secondXPos, secondYPos); if (time_now.second() < 10) { tft.print("0"); } tft.print(time_now.second()); }
// Draw a 9x9 pellet void Board::drawPellet(Pellet pellet){ TFTscreen.stroke(56, 3, 200); for(int i = -3; i <= 3; i++){ for(int j = -3; j <= 3; j++){ TFTscreen.point(pellet.xPos + i, pellet.yPos + j); } } TFTscreen.stroke(0,0,0); }
// Prints highscore to screen void Board::setHighScore(uint8_t highscore){ TFTscreen.stroke(0, 72, 255); TFTscreen.setCursor(0, 70); TFTscreen.setTextSize(3); TFTscreen.print(highscore); TFTscreen.setTextSize(1); }
// Draws snake body as single point(1 pixel) void Board::drawPointFast(uint8_t xPoint, uint8_t yPoint){ // Set new stroke TFTscreen.stroke(rgbColour[0], rgbColour[1], rgbColour[2]); // Draw 9x9 square TFTscreen.point(xPoint, yPoint); // Set new colors updateColors(); }
// Write hour to screen void ClockMaster::writeHour(DateTime time_now) { tft.stroke(255, 255, 255); tft.setTextSize(hourSize); tft.setCursor(hourXPos, hourYPos); if (time_now.hour() < 10) { tft.print("0"); } tft.print(time_now.hour()); }
// Write day to screen void ClockMaster::writeDay(DateTime time_now) { dayXPos = monthLength + 4; // One space after month tft.stroke(255, 255, 255); tft.setTextSize(daySize); tft.setCursor(dayXPos, dayYPos); if (time_now.day() < 10) { tft.print("0"); } tft.print(time_now.day()); }
// Draws a 9X9 point with (xPoint, yPoint) as center void Board::drawPoint(uint8_t xPoint, uint8_t yPoint){ // set new stroke TFTscreen.stroke(rgbColour[0], rgbColour[1], rgbColour[2]); //Draw 9x9 square for(int i = -3; i <= 3; i++){ for(int j = -3; j <= 3; j++){ TFTscreen.point(((int)xPoint) + i, ((int)yPoint) + j); } } // Set new colors updateColors(); }
// Init the clock modules void ClockMaster::clockSetup(Alarm *alarm) { Serial.begin(9600); initRTC(); dht.begin(); tft.begin(); initScreen(); alarm->initAlarm(getTime()); }
void drawText(int16_t x, int16_t y, const String& str, uint16_t color) { int newX = x - str.length() * 5 / 2; for(uint32_t ii = 0; ii < str.length(); ++ii) { TFTscreen.drawChar(newX + ii * 5, y - 4, str[ii], color, 0x0, 1); } }
// Initiate the screen void ClockMaster::initScreen(){ tft.background(0, 0, 0); DateTime time_now = getTime(); writeSecond(time_now); writeMinute(time_now); writeHour(time_now); writeDay(time_now); writeWeekDay(time_now); writeMonth(time_now); writeYear(time_now); }
// reset the display void ClockMaster::resetDisplayTime(DateTime newTime, int currentXPos){ clearTextLine(secondXPos, secondYPos, secondSize, secondLength); writeSecond(newTime); clearTextLine(minuteXPos, minuteYPos, minuteSize, minuteLength); writeMinute(newTime); clearTextLine(hourXPos, hourYPos, hourSize, hourLength); writeHour(newTime); clearTextLine(dayXPos, dayYPos, daySize, dayLength); clearTextLine(weekdayXPos, weekdayYPos, weekdaySize, weekdayLength); writeDay(newTime); writeWeekDay(newTime); clearTextLine(monthXPos, monthYPos, monthSize, monthLength); writeMonth(newTime); clearTextLine(yearXPos, yearYPos, yearSize, yearLength); writeYear(newTime); clearTextLine(tempXPos, tempYPos, tempSize, tempLength); writeTemp(); // draw current x-position-indicator if(lastAlarmXPos != currentXPos){ // Center x-position-indicator under current number being changed clearTextLine(0, 85, 2, 160); if(currentXPos != -1){ int calculatedXPos = 45 * currentXPos + 30; tft.setCursor(calculatedXPos, 85); tft.print("-"); } } lastAlarmXPos = currentXPos; }
// Prints score to screen void Board::setScore(uint8_t score){ TFTscreen.setTextSize(3); TFTscreen.setCursor(0, 20); // Write old score as black, which erases it if(score != 0){ TFTscreen.stroke(0, 0, 0); TFTscreen.print(score - 1); } // Write new score in color of snake TFTscreen.setCursor(0, 20); TFTscreen.stroke(rgbColour[0], rgbColour[1], rgbColour[2]); TFTscreen.print(score); // Reset cursor size TFTscreen.setTextSize(1); }
PadConfiguration::PadConfiguration(const char* fileName): _valid(false), _picture(nullptr) { Serial.println("[PadConfiguration start]"); if(!TFTInit) { TFTscreen.begin(); TFTInit = true; } for(uint8_t index; index < BUTTON_ARRAY_SIZE; ++index) { _mapping[index] = nullptr; } File myFile; myFile = SD.open(fileName); if(!myFile) { Serial.println("[PadConfiguration done: could not open file]"); return; } String padConf = readLine(myFile); Serial.println(padConf.c_str()); Serial.println(padConf.length()); if(padConf.length() != BUTTON_ARRAY_SIZE*2+1) { Serial.println("[PadConfiguration done: parse error length]"); Serial.println(padConf.length()); myFile.close(); return; } for(uint8_t index = 0; index < BUTTON_ARRAY_SIZE; ++index) { switch(padConf.charAt(index*2)) { case 'k': { _mapping[index] = new InputHandlerKeyboard(padConf.charAt(index*2+1), false, false); if(!_mapping[index]->_valid) { Serial.println("[PadConfiguration done: parse error k]"); myFile.close(); return; } break; } case 'K': { _mapping[index] = new InputHandlerKeyboard(padConf.charAt(index*2+1), true, false); if(!_mapping[index]->_valid) { Serial.println("[PadConfiguration done: parse error K]"); myFile.close(); return; } break; } case 'F': { _mapping[index] = new InputHandlerKeyboard(padConf.charAt(index*2+1), false, true); if(!_mapping[index]->_valid) { Serial.println("[PadConfiguration done: parse error F]"); myFile.close(); return; } break; } case 'P': { _mapping[index] = new InputHandlerPad(padConf.charAt(index*2+1)); if(!_mapping[index]->_valid) { Serial.println("[PadConfiguration done: parse error P]"); myFile.close(); return; } break; } case 'X': /*{ _mapping[index] = new InputHandlerDualShock3(padConf.charAt(index*2+1)); if(!_mapping[index]->_valid) { Serial.println("[PadConfiguration done: parse error X]"); myFile.close(); return; } break; }*/ case '-': { _mapping[index] = nullptr; break; } default: {myFile.close(); Serial.println("[PadConfiguration done: parse error]"); return;} } } String imageFilePath = readLine(myFile); myFile.close(); myFile = SD.open(imageFilePath); if(myFile) { uint32_t len = myFile.size(); _picture = new uint8_t[len]; myFile.read(_picture, len); myFile.close(); Serial.println(imageFilePath); Serial.println("Image read"); } Serial.println(fileName); Serial.println("Valid"); _valid = true; Serial.println("[PadConfiguration done]"); }
void PadConfiguration::drawMapping() { TFTscreen.fillScreen(0); // Draw grid /*TFTscreen.drawFastHLine(0, 78, 160, 0xFFFF); TFTscreen.drawFastHLine(0, 103, 160, 0xFFFF); TFTscreen.drawFastVLine(40, 78, 50, 0xFFFF); TFTscreen.drawFastVLine(80, 78, 50, 0xFFFF); TFTscreen.drawFastVLine(120, 78, 50, 0xFFFF);*/ #define BUTTON_RADIUS 10 #define BUTTON1_X 60 #define BUTTON1_Y 90 #define BUTTON2_X (BUTTON1_X + 2 * BUTTON_RADIUS + 4) #define BUTTON2_Y (BUTTON1_Y - BUTTON_RADIUS) #define BUTTON3_X (BUTTON2_X + 2 * BUTTON_RADIUS + 4) #define BUTTON3_Y (BUTTON2_Y - 0.5 * BUTTON_RADIUS) #define BUTTON4_X (BUTTON3_X + 2 * BUTTON_RADIUS + 4) #define BUTTON4_Y BUTTON3_Y #define BUTTON5_X BUTTON1_X #define BUTTON5_Y (BUTTON1_Y - 2 * BUTTON_RADIUS - 4) #define BUTTON6_X BUTTON2_X #define BUTTON6_Y (BUTTON2_Y - 2 * BUTTON_RADIUS - 4) #define BUTTON7_X BUTTON3_X #define BUTTON7_Y (BUTTON3_Y - 2 * BUTTON_RADIUS - 4) #define BUTTON8_X BUTTON4_X #define BUTTON8_Y (BUTTON4_Y - 2 * BUTTON_RADIUS - 4) #define BUTTONSTART_X (BUTTON1_X - 3 * BUTTON_RADIUS) #define BUTTONSTART_Y (BUTTON1_Y + 2 * BUTTON_RADIUS) #define Y_OFFSET (-5) #define ALT_Y_OFFSET 5 #define GET_STRING(i) (_mapping[i] ? _mapping[i]->mappingString() : String("-")) #define GET_COLOR(i) (_mapping[i] ? _mapping[i]->mappingColor() : 0b1000010000010000) TFTscreen.drawCircle(BUTTON1_X, BUTTON1_Y, BUTTON_RADIUS, 0xFFFF); drawText(BUTTON1_X, BUTTON1_Y + Y_OFFSET, GET_STRING(BUTTON_1_INDEX), GET_COLOR(BUTTON_1_INDEX)); drawText(BUTTON1_X, BUTTON1_Y + ALT_Y_OFFSET, GET_STRING(ALT_BUTTON_1_INDEX), GET_COLOR(ALT_BUTTON_1_INDEX)); TFTscreen.drawCircle(BUTTON2_X, BUTTON2_Y, BUTTON_RADIUS, 0xFFFF); drawText(BUTTON2_X, BUTTON2_Y + Y_OFFSET, GET_STRING(BUTTON_2_INDEX), GET_COLOR(BUTTON_2_INDEX)); drawText(BUTTON2_X, BUTTON2_Y + ALT_Y_OFFSET, GET_STRING(ALT_BUTTON_2_INDEX), GET_COLOR(ALT_BUTTON_2_INDEX)); TFTscreen.drawCircle(BUTTON3_X, BUTTON3_Y, BUTTON_RADIUS, 0xFFFF); drawText(BUTTON3_X, BUTTON3_Y + Y_OFFSET, GET_STRING(BUTTON_3_INDEX), GET_COLOR(BUTTON_3_INDEX)); drawText(BUTTON3_X, BUTTON3_Y + ALT_Y_OFFSET, GET_STRING(ALT_BUTTON_3_INDEX), GET_COLOR(ALT_BUTTON_3_INDEX)); TFTscreen.drawCircle(BUTTON4_X, BUTTON4_Y, BUTTON_RADIUS, 0xFFFF); drawText(BUTTON4_X, BUTTON4_Y + Y_OFFSET, GET_STRING(BUTTON_4_INDEX), GET_COLOR(BUTTON_4_INDEX)); drawText(BUTTON4_X, BUTTON4_Y + ALT_Y_OFFSET, GET_STRING(ALT_BUTTON_4_INDEX), GET_COLOR(ALT_BUTTON_4_INDEX)); TFTscreen.drawCircle(BUTTON5_X, BUTTON5_Y, BUTTON_RADIUS, 0xFFFF); drawText(BUTTON5_X, BUTTON5_Y + Y_OFFSET, GET_STRING(BUTTON_5_INDEX), GET_COLOR(BUTTON_5_INDEX)); drawText(BUTTON5_X, BUTTON5_Y + ALT_Y_OFFSET, GET_STRING(ALT_BUTTON_5_INDEX), GET_COLOR(ALT_BUTTON_5_INDEX)); TFTscreen.drawCircle(BUTTON6_X, BUTTON6_Y, BUTTON_RADIUS, 0xFFFF); drawText(BUTTON6_X, BUTTON6_Y + Y_OFFSET, GET_STRING(BUTTON_6_INDEX), GET_COLOR(BUTTON_6_INDEX)); drawText(BUTTON6_X, BUTTON6_Y + ALT_Y_OFFSET, GET_STRING(ALT_BUTTON_6_INDEX), GET_COLOR(ALT_BUTTON_6_INDEX)); TFTscreen.drawCircle(BUTTON7_X, BUTTON7_Y, BUTTON_RADIUS, 0xFFFF); drawText(BUTTON7_X, BUTTON7_Y + Y_OFFSET, GET_STRING(BUTTON_7_INDEX), GET_COLOR(BUTTON_7_INDEX)); drawText(BUTTON7_X, BUTTON7_Y + ALT_Y_OFFSET, GET_STRING(ALT_BUTTON_7_INDEX), GET_COLOR(ALT_BUTTON_7_INDEX)); TFTscreen.drawCircle(BUTTON8_X, BUTTON8_Y, BUTTON_RADIUS, 0xFFFF); drawText(BUTTON8_X, BUTTON8_Y + Y_OFFSET, GET_STRING(BUTTON_8_INDEX), GET_COLOR(BUTTON_8_INDEX)); drawText(BUTTON8_X, BUTTON8_Y + ALT_Y_OFFSET, GET_STRING(ALT_BUTTON_8_INDEX), GET_COLOR(ALT_BUTTON_8_INDEX)); TFTscreen.drawCircle(BUTTONSTART_X, BUTTONSTART_Y, BUTTON_RADIUS, 0xFFFF); drawText(BUTTONSTART_X, BUTTONSTART_Y + Y_OFFSET, GET_STRING(BUTTON_START_INDEX), GET_COLOR(BUTTON_START_INDEX)); drawText(BUTTONSTART_X, BUTTONSTART_Y + ALT_Y_OFFSET, GET_STRING(ALT_BUTTON_START_INDEX), GET_COLOR(ALT_BUTTON_START_INDEX)); #define STICK_X 36 #define STICK_Y 36 #define STICK_WIDTH 6 #define STICK_LENGTH 12 TFTscreen.drawRect(STICK_X - STICK_LENGTH, STICK_Y - STICK_WIDTH/2, 2 * STICK_LENGTH, STICK_WIDTH, 0xFFFF); TFTscreen.drawRect(STICK_X - STICK_WIDTH/2, STICK_Y - STICK_LENGTH, STICK_WIDTH, 2 * STICK_LENGTH, 0xFFFF); drawText(STICK_X, STICK_Y - STICK_LENGTH - 5, _mapping[0]->mappingString(), _mapping[0]->mappingColor()); drawText(STICK_X, STICK_Y + STICK_LENGTH + 5, _mapping[1]->mappingString(), _mapping[1]->mappingColor()); drawText(STICK_X - STICK_LENGTH - 10, STICK_Y, _mapping[2]->mappingString(), _mapping[2]->mappingColor()); drawText(STICK_X + STICK_LENGTH + 10, STICK_Y, _mapping[3]->mappingString(), _mapping[3]->mappingColor()); }
void PadConfiguration::drawPicture() { if(_picture == nullptr) { drawMapping(); return; } uint8_t* pointer = _picture; uint16_t current(0); #define READ_8 current = *pointer; ++pointer; #define READ_16 current = *((uint16_t*)pointer); ++pointer; ++pointer; READ_16; TFTscreen.fillScreen(current); READ_8; uint16_t numColors(current); for(uint16_t currentColor = 0; currentColor < numColors; ++currentColor) { READ_16; uint16_t color = current; READ_16; uint32_t numRects(current); for(uint16_t currentRect = 0; currentRect < numRects; ++currentRect) { READ_8; int16_t x(current); READ_8; int16_t y(current); READ_8; int16_t w(current); READ_8; int16_t h(current); TFTscreen.fillRect(x, y, w, h, color); } READ_16; uint16_t numVLines(current); for(uint16_t currentVLine = 0; currentVLine < numVLines; ++currentVLine) { READ_8; int16_t x(current); READ_8; int16_t y(current); READ_8; int16_t l(current); TFTscreen.drawFastVLine(x, y, l, color); } READ_16; uint16_t numHLines(current); for(uint16_t currentHLine = 0; currentHLine < numHLines; ++currentHLine) { READ_8; int16_t x(current); READ_8; int16_t y(current); READ_8; int16_t l(current); TFTscreen.drawFastHLine(x, y, l, color); } READ_16; uint16_t numPix(current); for(uint16_t currentPix = 0; currentPix < numPix; ++currentPix) { READ_8; int16_t x(current); READ_8; int16_t y(current); TFTscreen.drawPixel(x, y, color); } } #undef READ_8 #undef READ_16 }
// Clear (draw black) single point (1 pixel) void Board::clearPointFast(int xPoint, int yPoint){ TFTscreen.stroke(0, 0, 0); TFTscreen.point(xPoint, yPoint); }
// Write month to screen void ClockMaster::writeMonth(DateTime time_now) { tft.stroke(255, 255, 255); tft.setTextSize(monthSize); tft.setCursor(monthXPos, monthYPos); tft.print(monthsOfTheYear[time_now.month() - 1]); }
// Clear text for text at x-position textX, yPosition textY that has size textSize and length textLength void ClockMaster::clearTextLine(int textX, int textY, int textSize, int textLength) { for (int i = 0; i < (8 * textSize); i++) { tft.drawFastHLine(textX, textY + i, textLength, defaultBackground); } }
// Write year to screen void ClockMaster::writeYear(DateTime time_now) { tft.stroke(255, 255, 255); tft.setTextSize(yearSize); tft.setCursor(yearYPos, yearXPos); tft.print(time_now.year()); }
// Write temperature and Humidity to screen void ClockMaster::writeTemp(){ tft.stroke(32, 177, 98); tft.setTextSize(2); tft.setCursor(tempXPos, tempYPos); if(-10 < lastTemp && 10 > lastTemp ){ tft.print("0"); } tft.print(lastTemp); tft.print("C "); if(-10 < lastTempF && 10 > lastTempF ){ tft.print("0"); } tft.print(lastTempF); tft.print("F "); if(-10 < lastHumidity && 10 > lastHumidity ){ tft.print("0"); } tft.print(lastHumidity); tft.print("h"); }
// Draws pellet as only the middle pixel void Board::drawPelletFast(Pellet pellet){ TFTscreen.stroke(56, 3, 200); TFTscreen.point(pellet.xPos, pellet.yPos); TFTscreen.stroke(0,0,0); }
void Board::winScreen(uint8_t highscore, bool isNewHighscore){ // Set background color to random TFTscreen.background(random(255), random(255), random(255)); // Print "game over" TFTscreen.setTextSize(3); TFTscreen.stroke(255, 255, 255); TFTscreen.setCursor(0, 40); TFTscreen.print(uwin_str); // Print "press joystick to restart" TFTscreen.setTextSize(1); TFTscreen.setCursor(0, 10); TFTscreen.print(pressagain_str); // Print score TFTscreen.setCursor(0, 80); TFTscreen.setTextSize(2); TFTscreen.print(score_str); TFTscreen.print(": "); TFTscreen.print(highscore); // If score is new highcore, print "new highscore!" if(isNewHighscore){ TFTscreen.setTextSize(1); TFTscreen.setCursor(0, 100); TFTscreen.print(newHighscore); } }
void Board::init(){ // Start screen TFTscreen.begin(); // make the background black TFTscreen.background(0, 0, 0); // Set starting colors rgbColour[0] = 255; rgbColour[1] = 0; rgbColour[2] = 0; // Set which color to be incremented and decremented decColour = 0; incColour = 1; TFTscreen.stroke(255, 255, 255); // Draw top border left->right TFTscreen.line(boardBorderXLeft, boardBorderYTop, boardBorderXRight, boardBorderYTop); // Draw right border top->bottom TFTscreen.line(boardBorderXLeft, boardBorderYTop, boardBorderXLeft, boardBoarderYBottom); // Draw bottom border left->right TFTscreen.line(boardBorderXLeft, boardBoarderYBottom, boardBorderXRight, boardBoarderYBottom); // Draw left border TFTscreen.line(boardBorderXRight, boardBorderYTop, boardBorderXRight, boardBoarderYBottom); // Write score TFTscreen.setCursor(10, 10); TFTscreen.setTextSize(1); TFTscreen.print(score_str); setScore(0); // Write highscore TFTscreen.setTextSize(1); TFTscreen.stroke(255, 255, 255); TFTscreen.setCursor(10, 50); TFTscreen.print(high_str); TFTscreen.setCursor(10, 60); TFTscreen.print(score_str); }