Advertisement
pleasedontcode

**Clock Display** rev_06

Dec 9th, 2024
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: **Clock Display**
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2024-12-09 11:30:56
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* print a clock on display and each number is within */
  21.     /* a circle. */
  22. /****** END SYSTEM REQUIREMENTS *****/
  23.  
  24. /* START CODE */
  25.  
  26. /****** DEFINITION OF LIBRARIES *****/
  27. #include <SPI.h>
  28. #include <ILI9488.h>    //https://github.com/jaretburkett/ILI9488
  29. #include <U8g2_for_Adafruit_GFX.h>  //https://github.com/olikraus/U8g2_for_Adafruit_GFX
  30.  
  31. /****** FUNCTION PROTOTYPES *****/
  32. void setup(void);
  33. void loop(void);
  34. void drawClock();
  35.  
  36. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  37. const uint8_t display_ILI9488_TFT_RST_PIN_D2        = 2;
  38. const uint8_t display_ILI9488_TFT_DC_PIN_D3     = 3;
  39.  
  40. /***** DEFINITION OF SPI PINS *****/
  41. const uint8_t display_ILI9488_TFT_SPI_PIN_MOSI_D11      = 11;
  42. const uint8_t display_ILI9488_TFT_SPI_PIN_MISO_D12      = 12;
  43. const uint8_t display_ILI9488_TFT_SPI_PIN_SCLK_D13      = 13;
  44. const uint8_t display_ILI9488_TFT_SPI_PIN_CS_D10        = 10;
  45.  
  46. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  47. /***** used to store raw data *****/
  48. bool    display_ILI9488_TFT_RST_PIN_D2_rawData      = 0;
  49. bool    display_ILI9488_TFT_DC_PIN_D3_rawData       = 0;
  50.  
  51. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  52. /***** used to store data after characteristic curve transformation *****/
  53. float   display_ILI9488_TFT_RST_PIN_D2_phyData      = 0.0;
  54. float   display_ILI9488_TFT_DC_PIN_D3_phyData       = 0.0;
  55.  
  56. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  57. // Create display object
  58. ILI9488 tft(display_ILI9488_TFT_SPI_PIN_CS_D10, display_ILI9488_TFT_DC_PIN_D3, display_ILI9488_TFT_RST_PIN_D2);
  59. U8G2_FOR_ADAFRUIT_GFX u8g2;
  60.  
  61. void setup(void)
  62. {
  63.     // put your setup code here, to run once:
  64.  
  65.     pinMode(display_ILI9488_TFT_RST_PIN_D2,  OUTPUT);
  66.     pinMode(display_ILI9488_TFT_DC_PIN_D3,   OUTPUT);
  67.     pinMode(display_ILI9488_TFT_SPI_PIN_CS_D10,  OUTPUT);
  68.    
  69.     // start the SPI library:
  70.     SPI.begin();
  71.    
  72.     // Initialize the display
  73.     tft.begin();
  74.     tft.setRotation(3); // Set rotation if needed
  75.     tft.fillScreen(ILI9488_BLACK); // Clear screen
  76.     u8g2.begin(tft); // Initialize U8g2 with the display
  77. }
  78.  
  79. void loop(void)
  80. {
  81.     // put your main code here, to run repeatedly:
  82.     drawClock(); // Call function to draw clock
  83.     delay(1000); // Update every second
  84. }
  85.  
  86. void drawClock() {
  87.     // Get current hour and minute
  88.     int hour = hour(); // Assuming you have a function to get the current hour
  89.     int minute = minute(); // Assuming you have a function to get the current minute
  90.  
  91.     // Clear the previous clock
  92.     tft.fillScreen(ILI9488_BLACK);
  93.  
  94.     // Draw clock face
  95.     int centerX = tft.width() / 2;
  96.     int centerY = tft.height() / 2;
  97.     int radius = 100;
  98.  
  99.     // Draw circles for each hour
  100.     for (int i = 0; i < 12; i++) {
  101.         float angle = (i * 30) * (PI / 180); // 30 degrees for each hour
  102.         int x = centerX + radius * cos(angle);
  103.         int y = centerY + radius * sin(angle);
  104.         tft.drawCircle(x, y, 20, ILI9488_WHITE); // Draw circle for hour
  105.         u8g2.setFont(u8g2_font_ncenB08_tr); // Set font for hour numbers
  106.         u8g2.setCursor(x - 5, y + 5); // Position cursor
  107.         u8g2.print(i + 1); // Print hour number
  108.     }
  109.  
  110.     // Draw hands for the current time
  111.     float hourAngle = (hour % 12 + minute / 60.0) * 30; // Hour hand
  112.     float minuteAngle = minute * 6; // Minute hand
  113.  
  114.     // Draw hour hand
  115.     int hourX = centerX + (radius - 40) * cos(hourAngle * (PI / 180));
  116.     int hourY = centerY + (radius - 40) * sin(hourAngle * (PI / 180));
  117.     tft.drawLine(centerX, centerY, hourX, hourY, ILI9488_WHITE);
  118.  
  119.     // Draw minute hand
  120.     int minuteX = centerX + (radius - 20) * cos(minuteAngle * (PI / 180));
  121.     int minuteY = centerY + (radius - 20) * sin(minuteAngle * (PI / 180));
  122.     tft.drawLine(centerX, centerY, minuteX, minuteY, ILI9488_WHITE);
  123. }
  124.  
  125. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement