onoffbutton.ino 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. //This example implements a simple sliding On/Off button. The example
  2. // demonstrates drawing and touch operations.
  3. //
  4. //Thanks to Adafruit forums member Asteroid for the original sketch!
  5. //
  6. #include <Adafruit_GFX.h>
  7. #include <SPI.h>
  8. #include <Wire.h>
  9. #include <Adafruit_ILI9341.h>
  10. #include <Adafruit_STMPE610.h>
  11. // This is calibration data for the raw touch data to the screen coordinates
  12. #define TS_MINX 150
  13. #define TS_MINY 130
  14. #define TS_MAXX 3800
  15. #define TS_MAXY 4000
  16. #define STMPE_CS 8
  17. Adafruit_STMPE610 ts = Adafruit_STMPE610(STMPE_CS);
  18. #define TFT_CS 10
  19. #define TFT_DC 9
  20. Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
  21. boolean RecordOn = false;
  22. #define FRAME_X 210
  23. #define FRAME_Y 180
  24. #define FRAME_W 100
  25. #define FRAME_H 50
  26. #define REDBUTTON_X FRAME_X
  27. #define REDBUTTON_Y FRAME_Y
  28. #define REDBUTTON_W (FRAME_W/2)
  29. #define REDBUTTON_H FRAME_H
  30. #define GREENBUTTON_X (REDBUTTON_X + REDBUTTON_W)
  31. #define GREENBUTTON_Y FRAME_Y
  32. #define GREENBUTTON_W (FRAME_W/2)
  33. #define GREENBUTTON_H FRAME_H
  34. void drawFrame()
  35. {
  36. tft.drawRect(FRAME_X, FRAME_Y, FRAME_W, FRAME_H, ILI9341_BLACK);
  37. }
  38. void redBtn()
  39. {
  40. tft.fillRect(REDBUTTON_X, REDBUTTON_Y, REDBUTTON_W, REDBUTTON_H, ILI9341_RED);
  41. tft.fillRect(GREENBUTTON_X, GREENBUTTON_Y, GREENBUTTON_W, GREENBUTTON_H, ILI9341_BLUE);
  42. drawFrame();
  43. tft.setCursor(GREENBUTTON_X + 6 , GREENBUTTON_Y + (GREENBUTTON_H/2));
  44. tft.setTextColor(ILI9341_WHITE);
  45. tft.setTextSize(2);
  46. tft.println("ON");
  47. RecordOn = false;
  48. }
  49. void greenBtn()
  50. {
  51. tft.fillRect(GREENBUTTON_X, GREENBUTTON_Y, GREENBUTTON_W, GREENBUTTON_H, ILI9341_GREEN);
  52. tft.fillRect(REDBUTTON_X, REDBUTTON_Y, REDBUTTON_W, REDBUTTON_H, ILI9341_BLUE);
  53. drawFrame();
  54. tft.setCursor(REDBUTTON_X + 6 , REDBUTTON_Y + (REDBUTTON_H/2));
  55. tft.setTextColor(ILI9341_WHITE);
  56. tft.setTextSize(2);
  57. tft.println("OFF");
  58. RecordOn = true;
  59. }
  60. void setup(void)
  61. {
  62. Serial.begin(9600);
  63. tft.begin();
  64. if (!ts.begin()) {
  65. Serial.println("Unable to start touchscreen.");
  66. }
  67. else {
  68. Serial.println("Touchscreen started.");
  69. }
  70. tft.fillScreen(ILI9341_BLUE);
  71. // origin = left,top landscape (USB left upper)
  72. tft.setRotation(1);
  73. redBtn();
  74. }
  75. void loop()
  76. {
  77. // See if there's any touch data for us
  78. if (!ts.bufferEmpty())
  79. {
  80. // Retrieve a point
  81. TS_Point p = ts.getPoint();
  82. // Scale using the calibration #'s
  83. // and rotate coordinate system
  84. p.x = map(p.x, TS_MINY, TS_MAXY, 0, tft.height());
  85. p.y = map(p.y, TS_MINX, TS_MAXX, 0, tft.width());
  86. int y = tft.height() - p.x;
  87. int x = p.y;
  88. if (RecordOn)
  89. {
  90. if((x > REDBUTTON_X) && (x < (REDBUTTON_X + REDBUTTON_W))) {
  91. if ((y > REDBUTTON_Y) && (y <= (REDBUTTON_Y + REDBUTTON_H))) {
  92. Serial.println("Red btn hit");
  93. redBtn();
  94. }
  95. }
  96. }
  97. else //Record is off (RecordOn == false)
  98. {
  99. if((x > GREENBUTTON_X) && (x < (GREENBUTTON_X + GREENBUTTON_W))) {
  100. if ((y > GREENBUTTON_Y) && (y <= (GREENBUTTON_Y + GREENBUTTON_H))) {
  101. Serial.println("Green btn hit");
  102. greenBtn();
  103. }
  104. }
  105. }
  106. Serial.println(RecordOn);
  107. }
  108. }