Advertisement
divx118

Update arduino uno display

Dec 31st, 2017
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.28 KB | None | 0 0
  1. //Display program for the google assistant on rpi
  2.  
  3. #include <TimedAction.h>
  4. #include <Adafruit_GFX.h>
  5. #include <UTFTGLUE.h>
  6. UTFTGLUE tft(0x0154,A2,A1,A3,A4,A0);
  7.  
  8. // Declare which fonts we will be using
  9. #if !defined(SmallFont)
  10. extern uint8_t SmallFont[];
  11. #endif
  12. String lcdtext;
  13. //String topString[4];
  14. //String bottomString[14];
  15. int h, w, maxscroll; //height width of the screen and maximum scroll of half the screen
  16. int left = 3; // left offset
  17. int top = 3; // top offset
  18. int s = 1; // font size
  19. int line = s*10; // line height
  20. String recieved;
  21. String rec;
  22. int i = 0;
  23. int c = 0;
  24. int A;
  25. #define BLACK   0x0000
  26. #define BLUE    0x001F
  27. #define RED     0xF800
  28. #define GREEN   0x07E0
  29. #define CYAN    0x07FF
  30. #define MAGENTA 0xF81F
  31. #define YELLOW  0xFFE0
  32. #define WHITE   0xFFFF
  33.  
  34. //TouchScreen_kbv myTouch(XP, YP, XM, YM, 300);
  35. #include <TouchScreen.h>         //Global Library
  36. #define XP 7
  37. #define XM A1
  38. #define YP A2
  39. #define YM 6
  40. TouchScreen myTouch(XP, YP, XM, YM, 300);
  41. TSPoint tp;                      //Touchscreen_due branch uses Point
  42.  
  43. //Touchscreen calibration results just for reminder.
  44. //PORTRAIT CALIBRATION     240 x 320
  45. //int x = map(tp.x, 837, 169, 0, 240);
  46. //int y = map(tp.y, 861, 285, 0, 320);
  47. //Touch Pin Wiring XP=7 XM=A1 YP=A2 YM=6
  48.  
  49. void readResistiveTouch(void)
  50. {
  51.     tp = myTouch.getPoint();
  52.     pinMode(YP, OUTPUT);      //restore shared pins
  53.     pinMode(XM, OUTPUT);
  54. }
  55.  
  56. bool ISPRESSED()
  57. {
  58.  
  59.     int count = 0;
  60.     bool state, oldstate;
  61.     while (count < 2) {
  62.         readResistiveTouch();
  63.         state = tp.z > 20 && tp.z < 1000;
  64.         if (state == oldstate) count++;
  65.         else count = 0;
  66.         oldstate = state;
  67.         //delay(2);
  68.     }
  69.     if (oldstate)
  70.     {
  71.       showpoint();
  72.     }
  73. }
  74.  
  75. void showpoint(void)
  76. {
  77.     //For now we only need 2 buttons top half of the screen and
  78.     //bottom half.
  79.     if (tp.y < 500)
  80.     {
  81.       Serial.println("Bottom");
  82.     }
  83.     else if (tp.y > 575)
  84.     {
  85.       Serial.println("Top");
  86.     }
  87.  
  88. }
  89.  
  90. void updateText()
  91. {
  92.  
  93.   //Serial.println("height: " + String(height) + " width: " + String(width));
  94.   tft.setColor(YELLOW);
  95.   //buildTopDisplay();
  96.   tft.setColor(WHITE);
  97.   tft.print("http://electronics.henningkarlsen.com", left, top + (h/2));
  98.  
  99. }
  100. TimedAction touchThread = TimedAction(10,ISPRESSED);
  101. TimedAction textThread = TimedAction(500,updateText);
  102.  
  103.  
  104. void setup()
  105. {
  106.   Serial.begin(115200);
  107.   //randomSeed(analogRead(5));   //.kbv Due does not like A0
  108.   //pinMode(A0, OUTPUT);       //.kbv mcufriend have RD on A0
  109.   //digitalWrite(A0, HIGH);
  110.  
  111. // Setup the LCD
  112.   tft.InitLCD();
  113.   tft.setFont(SmallFont);
  114.   tft.setRotation(2);
  115.   tft.clrScr();
  116.   lcdtext = "My lcd display for changing";
  117.   tft.setBackColor(BLACK);
  118.  
  119.   h = tft.height();
  120.   w = tft.width();
  121.   maxscroll = ((( h/2 ) - top ) / line) - 1;
  122.   tft.setColor(MAGENTA);
  123.   tft.drawRect(w - 1, 0, 1, (h/2) - 1);
  124.   tft.drawRect(w - 1, h/2 , 1, h - 1);
  125.   tft.setColor(WHITE);
  126.   tft.setTextSize(s);
  127.   textThread.check();
  128.  
  129. }
  130.  
  131. //void buildTopDisplay()
  132. //{
  133. //    tft.print(lcdtext, left, top + (i*line));
  134. //}
  135.  
  136. void loop()
  137. {
  138.  
  139.   touchThread.check();
  140.   //textThread.check();
  141.   if ( Serial.available() > 0 )
  142.   {
  143.     recieved = Serial.readString();
  144.     Serial.println("Received = " + recieved);
  145.     if ( recieved.length() > 2 )
  146.     {
  147.       recieved.trim();
  148.  
  149.       A = recieved.substring(0,1).toInt();
  150.       recieved = recieved.substring(1,recieved.length());
  151.       c = 0;
  152.       i = 0;
  153.       switch (A)
  154.       {
  155.         case 0:
  156.           //Serial.println("Yes !!! " + recieved.substring(3,recieved.length()));
  157.           while ( c != -1)
  158.             {
  159.             c = recieved.indexOf('|');
  160.             //Serial.println("c = "+ String(c) );
  161.            
  162.             rec = recieved.substring(0,c);
  163.             //Serial.println(rec);
  164.             recieved = recieved.substring(c+1,recieved.length());
  165.               if (rec.length() > 38)
  166.                 {
  167.                   rec = rec.substring(0,38);
  168.                 }
  169.               else
  170.                 {
  171.                   for (int j = rec.length();j <= 38;j++)
  172.                   {
  173.                      rec = rec + " ";
  174.                   }
  175.                 }
  176.  
  177.               tft.print(rec, left, top + (i*line));
  178.               i++;
  179.             }
  180.             A = -1;
  181.             Serial.println("TopDone");
  182.             recieved = "";
  183.             rec = "";
  184.           break;
  185.         case 1:
  186.           //Serial.println("Yes !!! " + recieved);
  187.           c = 0;
  188.           i = 0;
  189.           while ( c != -1)
  190.             {
  191.             c = recieved.indexOf('|');
  192.             rec = recieved.substring(0,c);
  193.             recieved = recieved.substring(c+1,recieved.length());
  194.               if (rec.length() > 38)
  195.                 {
  196.                   rec = rec.substring(0,38);
  197.                 }
  198.               else
  199.                 {
  200.                   for (int j = rec.length();j <= 38;j++)
  201.                   {
  202.                      rec = rec + " ";
  203.                   }
  204.                 }
  205.               tft.print(rec, left, top + (h/2) + (i*line));
  206.               i++;
  207.             }
  208.             A = -1;
  209.             Serial.println("BottomDone");
  210.             recieved = "";
  211.             rec = "";
  212.             break;
  213.           default:
  214.             break;
  215.       }
  216.     }
  217.   }
  218. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement