PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

roundGaugeWithBallistic.ino 3.1KB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. ROUND GAUGE EXAMPLE with ballistic!
  3. This example show how to create a round gauge that react like the real one with (almost) correct ballistic
  4. Created by S.U.M.O.T.O.Y - Max MC Costa
  5. If you modify or get better result please let me know
  6. */
  7. #include <SPI.h>
  8. #include <Adafruit_GFX.h>
  9. #include <TFT_ILI9163C.h>
  10. // Color definitions
  11. #define BLACK 0x0000
  12. #define BLUE 0x001F
  13. #define RED 0xF800
  14. #define GREEN 0x07E0
  15. #define CYAN 0x07FF
  16. #define MAGENTA 0xF81F
  17. #define YELLOW 0xFFE0
  18. #define WHITE 0xFFFF
  19. volatile int16_t curVal1 = 0;
  20. volatile int16_t oldVal1 = 0;
  21. /*
  22. Teensy3.x and Arduino's
  23. You are using 4 wire SPI here, so:
  24. MOSI: 11//Teensy3.x/Arduino UNO (for MEGA/DUE refere to arduino site)
  25. MISO: 12//Teensy3.x/Arduino UNO (for MEGA/DUE refere to arduino site)
  26. SCK: 13//Teensy3.x/Arduino UNO (for MEGA/DUE refere to arduino site)
  27. the rest of pin below:
  28. */
  29. #define __CS 10
  30. #define __DC 9
  31. /*
  32. Teensy 3.x can use: 2,6,9,10,15,20,21,22,23
  33. Arduino's 8 bit: any
  34. DUE: check arduino site
  35. If you do not use reset, tie it to +3V3
  36. */
  37. TFT_ILI9163C tft = TFT_ILI9163C(__CS, __DC);
  38. void setup() {
  39. Serial.begin(9600);
  40. tft.begin();
  41. drawGauge(63,63,63);
  42. }
  43. void loop(void) {
  44. curVal1 = random(1,254);
  45. if (oldVal1 != curVal1){
  46. drawNeedle(curVal1,63,63,63,GREEN,BLACK);
  47. oldVal1 = curVal1;
  48. }
  49. }
  50. void drawGauge(uint8_t x,uint8_t y,uint8_t r) {
  51. tft.drawCircle(x, y, r,WHITE);//draw instrument container
  52. faceHelper(x,y,r,150,390,1.3);//draw major ticks
  53. if (r > 15) faceHelper(x,y,r,165,375,1.1);//draw minor ticks
  54. }
  55. void faceHelper(uint8_t x,uint8_t y,uint8_t r,int from,int to,float dev){
  56. float dsec,fromSecX,fromSecY,toSecX,toSecY;
  57. int i;
  58. for (i = from;i <= to;i += 30) {
  59. dsec = i * (PI / 180);
  60. fromSecX = cos(dsec) * (r / dev);
  61. fromSecY = sin(dsec) * (r / dev);
  62. toSecX = cos(dsec) * r;
  63. toSecY = sin(dsec) * r;
  64. tft.drawLine(1 + x + fromSecX,1 + y + fromSecY,1 + x + toSecX,1 + y + toSecY,WHITE);
  65. }
  66. }
  67. void drawNeedle(int16_t val,uint8_t x,uint8_t y,uint8_t r,uint16_t color,uint16_t bcolor){
  68. uint8_t i;
  69. if (curVal1 > oldVal1){
  70. for (i = oldVal1; i <= curVal1; i++){
  71. drawPointerHelper(i-1,63,63,63,bcolor);
  72. drawPointerHelper(i,63,63,63,color);
  73. if ((curVal1 - oldVal1) < (128)) delay(1);//ballistic
  74. }
  75. }
  76. else {
  77. for (i = oldVal1; i >= curVal1; i--){
  78. drawPointerHelper(i+1,63,63,63,bcolor);
  79. drawPointerHelper(i,63,63,63,color);
  80. //ballistic
  81. if ((oldVal1 - curVal1) >= 128){
  82. delay(1);
  83. } else {
  84. delay(3);
  85. }
  86. }
  87. }
  88. }
  89. void drawPointerHelper(int16_t val,uint8_t x,uint8_t y,uint8_t r,uint16_t color) {
  90. float dsec, toSecX, toSecY;
  91. int16_t minValue = 0;
  92. int16_t maxValue = 255;
  93. int fromDegree = 150;//start
  94. int toDegree = 240;//end
  95. if (val > maxValue) val = maxValue;
  96. if (val < minValue) val = minValue;
  97. dsec = (((float)(uint16_t)(val - minValue) / (float)(uint16_t)(maxValue - minValue) * toDegree) + fromDegree) * (PI / 180);
  98. toSecX = cos(dsec) * (r / 1.35);
  99. toSecY = sin(dsec) * (r / 1.35);
  100. tft.drawLine(x, y, 1 + x + toSecX, 1 + y + toSecY, color);
  101. tft.fillCircle(x,y,2,color);
  102. }