Advertisement
KRITSADA

IPST-SE PS2Controller Tester by Ake Exorcist

Oct 31st, 2016
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 38.19 KB | None | 0 0
  1. /*
  2.  * Copyright (c) 2014 Innovative Experiment Co.,Ltd.
  3.  *
  4.  * Permission is hereby granted, free of charge, to any person obtaining a copy
  5.  * of this software and associated documentation files (the "Software"), to deal
  6.  * in the Software without restriction, including without limitation the rights
  7.  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8.  * copies of the Software, and to permit persons to whom the Software is
  9.  * furnished to do so, subject to the following conditions:
  10.  *
  11.  * The above copyright notice and this permission notice shall be included in
  12.  * all copies or substantial portions of the Software.
  13.  *
  14.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15.  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17.  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18.  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19.  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20.  * THE SOFTWARE.
  21.  */
  22.  
  23. /*
  24.  * โปรแกรมทดสอบการทำงานของจอยสติ๊กไร้สายกับบอร์ด IPST-SE
  25.  *
  26.  * ใช้ทดสอบการติดต่อกับจอยสติ๊กไร้สายที่ทำงานเข้ากันได้กับจอยสติ๊ก PS2
  27.  * โดยมีหน้าจอแสดงผลตรงกับปุ่มต่างๆ บนตัวจอยสติ๊ก หากไม่มีการกดปุ่ม สัญลักษณ์ทั้งหมดจะเป็นสีเทา
  28.  * เมื่อกดปุ่มใดๆ บนจอยสติ๊ก สัญลักษณ์ของปุ่มๆ นั้นๆ บนจอแสดงผลจะแสดงสีออกมา (สีแตกต่างกันไปในแต่ละปุ่ม)
  29.  * หากโยกปุ่มอะนาลอก จะมีการแสดงพิกัดของปุ่มอะนาลอกด้วยจุดสีแดง
  30.  */
  31.  
  32. #include <ipst.h>                                // เรียกใช้งานไลบรารีสำหรับ IPST-SE
  33. #include <PS2X_lib.h>                            // เรียกใช้งานไลบรารีสำหรับ PS2 Controller
  34.  
  35. #define PS2_DAT        16                        // ต่อขา DATA    เข้ากับขาพอร์ต 16
  36. #define PS2_CMD        17                        // ต่อขา COMMAND เข้ากับขาพอร์ต 17
  37. #define PS2_SEL        18                        // ต่อขา SELECT  เข้ากับขาพอร์ต 18
  38. #define PS2_CLK        19                        // ต่อขา CLOCK   เข้ากับขาพอร์ต 19
  39.  
  40. #define GLCD_DKGRAY    colorRGB(10, 20, 10)      // กำหนดค่าสีเทาเข้ม
  41. #define GLCD_GRAY      colorRGB(27, 54, 27)      // กำหนดค่าสีเทา
  42. #define GLCD_DKGREEN   colorRGB(0, 50, 0)        // กำหนดค่าสีเขียวเข้ม
  43. #define GLCD_ORANGE    colorRGB(31, 40, 0)       // กำหนดค่าสีส้ม
  44.  
  45. PS2X ps2x;                                       // ประกาศตัวแปรสำหรับจอยสติ๊ก
  46.  
  47. int oldLX = 0;                                   // เก็บค่าตำแหน่งพิกัด X ก่อนหน้าของปุ่มอะนาลอกซ้าย
  48. int oldLY = 0;                                   // เก็บค่าตำแหน่งพิกัด Y ก่อนหน้าของปุ่มอะนาลอกซ้าย
  49. int oldRX = 0;                                   // เก็บค่าตำแหน่งพิกัด X ก่อนหน้าของปุ่มอะนาลอกขวา
  50. int oldRY = 0;                                   // เก็บค่าตำแหน่งพิกัด Y ก่อนหน้าของปุ่มอะนาลอกขวา
  51.  
  52. void setup()
  53. {
  54.   delay(1000);                                   // หน่วงเวลา 1 วินาทีเพื่อรอให้บอร์ดพร้อมทำงาน
  55.   glcdClear();                                   // เคลียร์ภาพทั้งหมดบนหน้าจอแสดงผล
  56.   glcdMode(1);                                   // กำหนดให้หน้าจอแสดงผลเป็นแนวนอน
  57.   glcdFillScreen(GLCD_WHITE);                    // กำหนดพื้นหลังของหน้าจอเป็นสีขาว
  58.   setTextBackgroundColor(GLCD_WHITE);            // กำหนดพื้นหลังของตัวหนังสือเป็นสีขาว
  59.  
  60.   setTextColor(GLCD_BLACK);                      // กำหนดตัวหนังสือเป็นสีดำ
  61.   glcd(0, 0, "Connecting");                      // แสดงข้อความเพื่อให้รู้ว่ากำลังทำการเชื่อมต่อกับจอยสติ๊ก
  62.      
  63.   initController();                              // เรียกฟังก์ชั่นเชื่อมต่อกับจอยสติ๊ก
  64. }
  65.  
  66. void loop()
  67. {
  68.   ps2x.read_gamepad(false, false);               // อ่านข้อมูลจากจอยสติ๊ก PS2
  69.  
  70.   checkButtonCircle();                           // เรียกฟังก์ชั่นตรวจสอบการกดปุ่มวงกลม
  71.   checkButtonCross();                            // เรียกฟังก์ชั่นตรวจสอบการกดปุ่มกากบาท
  72.   checkButtonSquare();                           // เรียกฟังก์ชั่นตรวจสอบการกดปุ่มสี่เหลี่ยม
  73.   checkButtonTriangle();                         // เรียกฟังก์ชั่นตรวจสอบการกดปุ่มสามเหลี่ยม
  74.  
  75.   checkButtonUp();                               // เรียกฟังก์ชั่นตรวจสอบการกดปุ่มขึ้น
  76.   checkButtonDown();                             // เรียกฟังก์ชั่นตรวจสอบการกดปุ่มลง
  77.   checkButtonLeft();                             // เรียกฟังก์ชั่นตรวจสอบการกดปุ่มซ้าย
  78.   checkButtonRight();                            // เรียกฟังก์ชั่นตรวจสอบการกดปุ่มขวา  
  79.  
  80.   checkButtonL1();                               // เรียกฟังก์ชั่นตรวจสอบการกดปุ่ม L1  
  81.   checkButtonL2();                               // เรียกฟังก์ชั่นตรวจสอบการกดปุ่ม L2
  82.   checkButtonL3();                               // เรียกฟังก์ชั่นตรวจสอบการกดปุ่ม L3
  83.   checkButtonR1();                               // เรียกฟังก์ชั่นตรวจสอบการกดปุ่ม R1
  84.   checkButtonR2();                               // เรียกฟังก์ชั่นตรวจสอบการกดปุ่ม R2
  85.   checkButtonR3();                               // เรียกฟังก์ชั่นตรวจสอบการกดปุ่ม R3
  86.  
  87.   checkButtonSelect();                           // เรียกฟังก์ชั่นตรวจสอบการกดปุ่ม SELECT
  88.   checkButtonStart();                            // เรียกฟังก์ชั่นตรวจสอบการกดปุ่ม START
  89.  
  90.   checkAnalogL();                                // เรียกฟังก์ชั่นตรวจสอบตำแหน่ง XY ของปุ่มอะนาลอกซ้าย
  91.   checkAnalogR();                                // เรียกฟังก์ชั่นตรวจสอบตำแหน่ง XY ของปุ่มอะนาลอกขวา
  92.  
  93.   delay(50);                                     // หน่วงเวลา 50 มิลลิวินาที
  94. }
  95.  
  96. void initController() {                          // ฟังก์ชั่นสำหรับเชื่อมต่อกับจอยสติ๊ก PS2
  97.   while(true)                                    // วนการทำงานเพื่อรอการเชื่อมต่อกับจอยสติ๊ก
  98.   {
  99.     // กำหนดขาเชื่อมต่กับจอยสติ๊ก โดยมีการเก็บค่าที่ส่งกลับมาเป็น Integer เพื่อรู้ว่าเชื่อมต่อได้หรือไม่
  100.     int error = ps2x.config_gamepad(PS2_CLK, PS2_CMD, PS2_SEL, PS2_DAT, false, false);
  101.  
  102.     if(error == 0)                               // กรณีที่เชื่อมต่อได้ (Error = 0)
  103.     {
  104.       initGlcd();                                // เตรียมภาพบนหน้าจอ
  105.       break;                                     // ออกจาก while(true)
  106.     }
  107.     delay(500);                                  // หน่วงเวลา 500 มิลลิวินาทีเพื่อรอการเชื่อมต่อครั้งต่อไปในกรณีที่เชื่อมต่อไม่สำเร็จ
  108.   }
  109. }
  110.  
  111. void initGlcd()                                  // ฟังก์ชั่นเตรียมภาพบนหน้าจอเมื่อเริ่มต้นทำงาน
  112. {
  113.   glcdClear();                                   // เคลียร์ภาพบนหน้าจอ
  114.   setTextColor(GLCD_RED);                        // กำหนดตัวอักษรเป็นสีแดง
  115.   glcd(0, 7, "PlayStation 2");                   // แสดงคำว่า PlayStation 2 ที่แถวแรกสุดตรงกลาง
  116.   setTextColor(GLCD_BLUE);                       // กำหนดตัวอักษรเป็นสีแดง
  117.   glcd(1, 9, "Controller");                      // แสดงคำว่า Controller ที่แถวที่สองตรงกลาง
  118.   setTextColor(GLCD_BLACK);                      // กำหนดตัวอักษรเป็นสีแดง
  119.   glcd(2, 13, "OK");                             // แสดงคำว่า OK ที่แถวที่สามตรงกลาง
  120.    
  121.   drawButtonUpOff();                             // เรียกฟังก์ชั่นวาดปุ่มขึ้นที่ยังไม่ถูกกด (เป็นภาพสีเทา)
  122.   drawButtonDownOff();                           // เรียกฟังก์ชั่นวาดปุ่มลงที่ยังไม่ถูกกด (เป็นภาพสีเทา)
  123.   drawButtonLeftOff();                           // เรียกฟังก์ชั่นวาดปุ่มซ้ายที่ยังไม่ถูกกด (เป็นภาพสีเทา)
  124.   drawButtonRightOff();                          // เรียกฟังก์ชั่นวาดปุ่มขวาที่ยังไม่ถูกกด (เป็นภาพสีเทา)
  125.  
  126.   drawButtonCircleOff();                         // เรียกฟังก์ชั่นวาดปุ่มวงกลมที่ยังไม่ถูกกด (เป็นภาพสีเทา)
  127.   drawButtonSquareOff();                         // เรียกฟังก์ชั่นวาดปุ่มสี่เหลี่ยมที่ยังไม่ถูกกด (เป็นภาพสีเทา)
  128.   drawButtonCrossOff();                          // เรียกฟังก์ชั่นวาดปุ่มกากบาทที่ยังไม่ถูกกด (เป็นภาพสีเทา)
  129.   drawButtonTriangleOff();                       // เรียกฟังก์ชั่นวาดปุ่มสามเหลี่ยมที่ยังไม่ถูกกด (เป็นภาพสีเทา)
  130.  
  131.   drawButtonR1Off();                             // เรียกฟังก์ชั่นวาดปุ่ม R1 ที่ยังไม่ถูกกด (เป็นภาพสีเทา)
  132.   drawButtonR2Off();                             // เรียกฟังก์ชั่นวาดปุ่ม R2 ที่ยังไม่ถูกกด (เป็นภาพสีเทา)
  133.   drawButtonR3Off();                             // เรียกฟังก์ชั่นวาดปุ่ม R3 ที่ยังไม่ถูกกด (เป็นภาพสีเทา)
  134.   drawButtonL1Off();                             // เรียกฟังก์ชั่นวาดปุ่ม L1 ที่ยังไม่ถูกกด (เป็นภาพสีเทา)
  135.   drawButtonL2Off();                             // เรียกฟังก์ชั่นวาดปุ่ม L2 ที่ยังไม่ถูกกด (เป็นภาพสีเทา)
  136.   drawButtonL3Off();                             // เรียกฟังก์ชันวาดปุ่ม L3 ที่ยังไม่ถูกกด (เป็นภาพสีเทา)
  137.  
  138.   drawButtonSelectOff();                         // เรียกฟังก์ชั่นวาดปุ่ม SELECT ที่ยังไม่ถูกกด (เป็นภาพสีเทา)
  139.   drawButtonStartOff();                          // เรียกฟังก์ชั่นวาดปุ่ม START ที่ยังไม่ถูกกด (เป็นภาพสีเทา)
  140.  
  141.   drawAnalogBorder();                            // เรียกฟังก์ชั่นวาดกรอบสี่เหลี่ยมสำหรับแสดงพิกัด XY ของปุ่มอะนาลอก
  142. }
  143.  
  144. void checkButtonUp()                             // ฟังก์ชั่นตรวจสอบการกดปุ่มขึ้น (UP)
  145. {
  146.   if(ps2x.ButtonPressed(PSB_PAD_UP))             // ถ้าปุ่มขึ้นถูกกด  
  147.   {
  148.     drawButtonUpOn();                            // เรียกฟังก์ชั่นวาดปุ่มขึ้นเมื่อถูกกด
  149.   }                                          
  150.   else if(ps2x.ButtonReleased(PSB_PAD_UP))       // ถ้าปุ่มขึ้นยังไม่ถูกกด
  151.   {
  152.     drawButtonUpOff();                           // เรียกฟังก์ชั่นวาดปุ่มขึ้นเมื่อไม่ถูกกด
  153.   }
  154. }
  155.  
  156. void drawButtonUpOn()                            // ฟังก์ชั่นวาดภาพปุ่มขึ้นเมื่อถูกกด
  157. {
  158.   glcdFillRect(25, 29, 10, 12, GLCD_DKGRAY);
  159.   glcdPixel(26, 40, GLCD_WHITE);
  160.   glcdPixel(27, 40, GLCD_WHITE);
  161.   glcdPixel(26, 39, GLCD_WHITE);
  162.   glcdPixel(35, 40, GLCD_WHITE);
  163.   glcdPixel(34, 40, GLCD_WHITE);
  164.   glcdPixel(35, 39, GLCD_WHITE);
  165. }
  166.  
  167. void drawButtonUpOff()                           // ฟังก์ชั่นวาดภาพปุ่มขึ้นที่ไม่ถูกกด
  168. {
  169.   glcdFillRect(25, 29, 10, 12, GLCD_GRAY);
  170.   glcdPixel(26, 40, GLCD_WHITE);
  171.   glcdPixel(27, 40, GLCD_WHITE);
  172.   glcdPixel(26, 39, GLCD_WHITE);
  173.   glcdPixel(35, 40, GLCD_WHITE);
  174.   glcdPixel(34, 40, GLCD_WHITE);
  175.   glcdPixel(35, 39, GLCD_WHITE);
  176. }
  177.  
  178. void checkButtonDown()                           // ฟังก์ชั่นตรวจสอบการกดปุ่มลง            
  179. {
  180.   if(ps2x.ButtonPressed(PSB_PAD_DOWN))           // ถ้าปุ่มลงถูกกด  
  181.   {
  182.     drawButtonDownOn();                          // เรียกฟังก์ชั่นวาดปุ่มลงเมื่อถูกกด
  183.   }
  184.   else if(ps2x.ButtonReleased(PSB_PAD_DOWN))     // ถ้าปุ่มลงไม่ถูกกด  
  185.   {
  186.     drawButtonDownOff();                         // เรียกฟังก์ชั่นวาดปุ่มลงเมื่อไม่ถูกกด
  187.   }
  188. }
  189.  
  190. void drawButtonDownOn()                          // ฟังก์ชั่นวาดปุ่มลงเมื่อถูกกด
  191. {
  192.   glcdFillRect(25, 60, 10, 12, GLCD_DKGRAY);
  193.   glcdPixel(26, 60, GLCD_WHITE);
  194.   glcdPixel(27, 60, GLCD_WHITE);
  195.   glcdPixel(26, 61, GLCD_WHITE);
  196.   glcdPixel(35, 60, GLCD_WHITE);
  197.   glcdPixel(34, 60, GLCD_WHITE);
  198.   glcdPixel(35, 61, GLCD_WHITE);
  199. }
  200.  
  201. void drawButtonDownOff()                         // ฟังก์ชั่นวาดปุ่มลงเมื่อไม่ถูกกด
  202. {
  203.   glcdFillRect(25, 60, 10, 12, GLCD_GRAY);
  204.   glcdPixel(26, 60, GLCD_WHITE);
  205.   glcdPixel(27, 60, GLCD_WHITE);
  206.   glcdPixel(26, 61, GLCD_WHITE);
  207.   glcdPixel(35, 60, GLCD_WHITE);
  208.   glcdPixel(34, 60, GLCD_WHITE);
  209.   glcdPixel(35, 61, GLCD_WHITE);
  210. }
  211.  
  212. void checkButtonLeft()                           // ฟังก์ชั่นตรวจสอบการกดปุ่มซ้าย  
  213. {
  214.   if(ps2x.ButtonPressed(PSB_PAD_LEFT))           // ถ้าปุ่มซ้ายถูกกด  
  215.   {
  216.     drawButtonLeftOn();                          // เรียกฟังก์ชั่นวาดปุ่มซ้ายเมื่อถูกกด
  217.   }
  218.   else if(ps2x.ButtonReleased(PSB_PAD_LEFT))     // ถ้าปุ่มซ้ายไม่ถูกกด  
  219.   {
  220.     drawButtonLeftOff();                         // เรียกฟังก์ชั่นวาดปุ่มซ้ายเมื่อไม่ถูกกด
  221.   }
  222. }
  223.  
  224. void drawButtonLeftOn()                          // ฟังก์ชั่นวาดปุ่มซ้ายเมื่อถูกกด
  225. {
  226.   glcdFillRect(10, 45, 12, 10, GLCD_DKGRAY);
  227.   glcdPixel(22, 45, GLCD_WHITE);
  228.   glcdPixel(21, 45, GLCD_WHITE);
  229.   glcdPixel(22, 46, GLCD_WHITE);
  230.   glcdPixel(22, 54, GLCD_WHITE);
  231.   glcdPixel(21, 54, GLCD_WHITE);
  232.   glcdPixel(22, 53, GLCD_WHITE);
  233. }
  234.  
  235. void drawButtonLeftOff()                         // ฟังก์ชั่นวาดปุ่มซ้ายเมื่อไม่ถูกกด
  236. {
  237.   glcdFillRect(10, 45, 12, 10, GLCD_GRAY);
  238.   glcdPixel(22, 45, GLCD_WHITE);
  239.   glcdPixel(21, 45, GLCD_WHITE);
  240.   glcdPixel(22, 46, GLCD_WHITE);
  241.   glcdPixel(22, 54, GLCD_WHITE);
  242.   glcdPixel(21, 54, GLCD_WHITE);
  243.   glcdPixel(22, 53, GLCD_WHITE);
  244. }
  245.  
  246.  
  247. void checkButtonRight()                          // ฟังก์ชั่นตรวจสอบการกดปุ่มขวา    
  248. {
  249.   if(ps2x.ButtonPressed(PSB_PAD_RIGHT))          // ถ้าปุ่มขวาถูกกด
  250.   {
  251.     drawButtonRightOn();                         // เรียกฟังก์ชั่นวาดปุ่มขวาเมื่อถูกกด
  252.   }
  253.   else if(ps2x.ButtonReleased(PSB_PAD_RIGHT))    // ถ้าปุ่มขวาไม่ถูกกด  
  254.   {
  255.     drawButtonRightOff();                        // เรียกฟังก์ชั่นวาดปุ่มขวาเมื่อไม่ถูกกด
  256.   }
  257. }
  258.  
  259. void drawButtonRightOn()                         // ฟังก์ชั่นวาดปุ่มขวาเมื่อถูกกด
  260. {
  261.   glcdFillRect(38, 45, 12, 10, GLCD_DKGRAY);
  262.   glcdPixel(39, 45, GLCD_WHITE);
  263.   glcdPixel(40, 45, GLCD_WHITE);
  264.   glcdPixel(39, 46, GLCD_WHITE);
  265.   glcdPixel(39, 54, GLCD_WHITE);
  266.   glcdPixel(40, 54, GLCD_WHITE);
  267.   glcdPixel(39, 53, GLCD_WHITE);
  268. }
  269.  
  270. void drawButtonRightOff()                        // ฟังก์ชั่นวาดปุ่มขวาเมื่อไม่ถูกกด
  271. {
  272.   glcdFillRect(38, 45, 12, 10, GLCD_GRAY);
  273.   glcdPixel(39, 45, GLCD_WHITE);
  274.   glcdPixel(40, 45, GLCD_WHITE);
  275.   glcdPixel(39, 46, GLCD_WHITE);
  276.   glcdPixel(39, 54, GLCD_WHITE);
  277.   glcdPixel(40, 54, GLCD_WHITE);
  278.   glcdPixel(39, 53, GLCD_WHITE);
  279. }
  280.  
  281. void checkButtonR1()                             // ฟังก์ชั่นตรวจสอบการกดปุ่ม R1    
  282. {
  283.   if(ps2x.ButtonPressed(PSB_R1))                 // ถ้าปุ่ม R1 ถูกกด
  284.   {
  285.     drawButtonR1On();                            // เรียกฟังก์ชั่นวาดปุ่ม R1 เมื่อถูกกด
  286.   }
  287.   else if(ps2x.ButtonReleased(PSB_R1))           // ถ้าปุ่ม R1 ไม่ถูกกด
  288.   {
  289.     drawButtonR1Off();                           // เรียกฟังก์ชั่นวาดปุ่ม R1 เมื่อไม่ถูกกด
  290.   }
  291. }
  292.  
  293. void drawButtonR1On()                            // ฟังก์ชั่นวาดปุ่ม R1 เมื่อถูกกด
  294. {  
  295.   setTextBackgroundColor(GLCD_DKGRAY);
  296.   glcdFillRect(149, 10, 10, 8, GLCD_DKGRAY);
  297.   setTextColor(GLCD_WHITE);
  298.   glcd(1, 21, "  R1");
  299. }
  300.  
  301. void drawButtonR1Off()                           // ฟังก์ชั่นวาดปุ่ม R1 เมื่อไม่ถูกกด
  302. {
  303.   setTextBackgroundColor(GLCD_GRAY);
  304.   setTextColor(GLCD_WHITE);
  305.   glcdFillRect(149, 10, 10, 8, GLCD_GRAY);
  306.   glcd(1, 21, "  R1");
  307. }
  308.  
  309. void checkButtonR2()                             // ฟังก์ชั่นตรวจสอบการกดปุ่ม R2    
  310. {
  311.   if(ps2x.ButtonPressed(PSB_R2))                 // ถ้าปุ่ม R2 ถูกกด
  312.   {
  313.     drawButtonR2On();                            // เรียกฟังก์ชั่นวาดปุ่ม R2 เมื่อถูกกด
  314.   }
  315.   else if(ps2x.ButtonReleased(PSB_R2))           // ถ้าปุ่ม R2 ไม่ถูกกด
  316.   {
  317.     drawButtonR2Off();                           // เรียกฟังก์ชั่นวาดปุ่ม R2 เมื่อไม่ถูกกด
  318.   }
  319. }
  320.  
  321. void drawButtonR2On()                            // ฟังก์ชั่นวาดปุ่ม R2 เมื่อถูกกด
  322. {
  323.   setTextBackgroundColor(GLCD_DKGRAY);
  324.   setTextColor(GLCD_WHITE);
  325.   glcdFillRect(149, 0, 10, 8, GLCD_DKGRAY);
  326.   glcd(0, 21, "  R2");
  327. }
  328.  
  329. void drawButtonR2Off()                           // ฟังก์ชั่นวาดปุ่ม R2 เมื่อไม่ถูกกด
  330. {
  331.   setTextBackgroundColor(GLCD_GRAY);
  332.   setTextColor(GLCD_WHITE);
  333.   glcdFillRect(149, 0, 10, 8, GLCD_GRAY);
  334.   glcd(0, 21, "  R2");
  335. }
  336.  
  337. void checkButtonR3()                             // ฟังก์ชั่นตรวจสอบการกดปุ่ม R3  
  338. {
  339.   if(ps2x.ButtonPressed(PSB_R3))                 // ถ้าปุ่ม R3 ถูกกด
  340.   {
  341.     drawButtonR3On();                            // เรียกฟังก์ชั่นวาดปุ่ม R3 เมื่อถูกกด
  342.   }
  343.   else if(ps2x.ButtonReleased(PSB_R3))           // ถ้าปุ่ม R3 ไม่ถูกกด
  344.   {
  345.     drawButtonR3Off();                           // เรียกฟังก์ชั่นวาดปุ่ม R3 เมื่อไม่ถูกกด
  346.   }
  347. }
  348.  
  349. void drawButtonR3On()                            // ฟังก์ชั่นวาดปุ่ม R3 เมื่อถูกกด  
  350. {
  351.   setTextBackgroundColor(GLCD_DKGRAY);
  352.   setTextColor(GLCD_WHITE);
  353.   glcdFillRect(149, 120, 10, 8, GLCD_DKGRAY);
  354.   glcd(12, 21, "  R3");
  355. }
  356.  
  357. void drawButtonR3Off()                           // ฟังก์ชั่นวาดปุ่ม R3 เมื่อไม่ถูกกด  
  358. {
  359.   setTextBackgroundColor(GLCD_GRAY);
  360.   setTextColor(GLCD_WHITE);
  361.   glcdFillRect(149, 120, 10, 8, GLCD_GRAY);
  362.   glcd(12, 21, "  R3");
  363. }
  364.  
  365. void checkButtonL1()                             // ฟังก์ชั่นตรวจสอบการกดปุ่ม L1    
  366. {
  367.   if(ps2x.ButtonPressed(PSB_L1))                 // ถ้าปุ่ม L1 ถูกกด
  368.   {
  369.     drawButtonL1On();                            // เรียกฟังก์ชั่นวาดปุ่ม L1 เมื่อถูกกด
  370.   }
  371.   else if(ps2x.ButtonReleased(PSB_L1))           // ถ้าปุ่ม L1 ไม่ถูกกด  
  372.   {                                              
  373.     drawButtonL1Off();                           // เรียกฟังก์ชั่นวาดปุ่ม L1 เมื่อไม่ถูกกด
  374.   }
  375. }
  376.  
  377. void drawButtonL1On()                            // ฟังก์ชั่นวาดปุ่ม L1 เมื่อถูกกด  
  378. {
  379.   setTextBackgroundColor(GLCD_DKGRAY);
  380.   setTextColor(GLCD_WHITE);
  381.   glcd(1, 0, "  L1  ");
  382. }
  383.  
  384. void drawButtonL1Off()                           // ฟังก์ชั่นวาดปุ่ม L1 เมื่อไม่ถูกกด
  385. {
  386.   setTextBackgroundColor(GLCD_GRAY);
  387.   setTextColor(GLCD_WHITE);
  388.   glcd(1, 0, "  L1  ");
  389. }
  390.  
  391. void checkButtonL2()                             // ฟังก์ชั่นตรวจสอบการกดปุ่ม L2  
  392. {
  393.   if(ps2x.ButtonPressed(PSB_L2))                 // ถ้าปุ่ม L2 ถูกกด  
  394.   {
  395.     drawButtonL2On();                            // เรียกฟังก์ชั่นวาดปุ่ม L2 เมื่อถูกกด
  396.   }
  397.   else if(ps2x.ButtonReleased(PSB_L2))           // ถ้าปุ่ม L2 ไม่ถูกกด  
  398.   {
  399.     drawButtonL2Off();                           // เรียกฟังก์ชั่นวาดปุ่ม L2 เมื่อไม่ถูกกด
  400.   }
  401. }
  402.  
  403. void drawButtonL2On()                            // ฟังก์ชั่นวาดปุ่ม L2 เมื่อถูกกด  
  404. {
  405.   setTextBackgroundColor(GLCD_DKGRAY);
  406.   setTextColor(GLCD_WHITE);
  407.   glcd(0, 0, "  L2  ");
  408. }
  409.  
  410. void drawButtonL2Off()                           // ฟังก์ชั่นวาดปุ่ม L2 เมื่อไม่ถูกกด  
  411. {
  412.   setTextBackgroundColor(GLCD_GRAY);
  413.   setTextColor(GLCD_WHITE);
  414.   glcd(0, 0, "  L2  ");
  415. }
  416.  
  417. void checkButtonL3()                             // ฟังก์ชั่นตรวจสอบการกดปุ่ม L3    
  418. {
  419.   if(ps2x.ButtonPressed(PSB_L3))                 // ถ้าปุ่ม L3 ถูกกด  
  420.   {
  421.     drawButtonL3On();                            // เรียกฟังก์ชั่นวาดปุ่ม L3 เมื่อถูกกด
  422.   }
  423.   else if(ps2x.ButtonReleased(PSB_L3))           // ถ้าปุ่ม L3 ไม่ถูกกด    
  424.   {
  425.     drawButtonL3Off();                           // เรียกฟังก์ชั่นวาดปุ่ม L3 เมื่อไม่ถูกกด
  426.   }
  427. }
  428.  
  429. void drawButtonL3On()                            // ฟังก์ชั่นวาดปุ่ม L3 เมื่อถูกกด  
  430. {
  431.   setTextBackgroundColor(GLCD_DKGRAY);
  432.   setTextColor(GLCD_WHITE);
  433.   glcd(12, 0, "  L3  ");
  434. }
  435.  
  436. void drawButtonL3Off()                           // ฟังก์ชั่นวาดปุ่ม L3 เมื่อไม่ถูกกด  
  437. {
  438.   setTextBackgroundColor(GLCD_GRAY);
  439.   setTextColor(GLCD_WHITE);
  440.   glcd(12, 0, "  L3  ");
  441. }
  442.  
  443. void checkButtonCircle()                         // ฟังก์ชั่นตรวจสอบการกดปุ่มวงกลม    
  444. {
  445.   if(ps2x.ButtonPressed(PSB_CIRCLE))             // ถ้าปุ่มวงกลมถูกกด    
  446.   {
  447.     drawButtonCircleOn();                        // เรียกฟังก์ชั่นวาดปุ่มวงกลมเมื่อถูกกด
  448.   }
  449.   else if(ps2x.ButtonReleased(PSB_CIRCLE))       // ถ้าปุ่มวงกลมไม่ถูกกด  
  450.   {
  451.     drawButtonCircleOff();                       // เรียกฟังก์ชั่นวาดปุ่มวงกลมเมื่อไม่ถูกกด
  452.   }
  453. }
  454.  
  455. void drawButtonCircleOn()                        // ฟังก์ชั่นวาดปุ่มวงกลมเมื่อถูกกด
  456. {
  457.   glcdCircle(140, 48, 6, GLCD_RED);
  458. }
  459.  
  460. void drawButtonCircleOff()                       // ฟังก์ชั่นวาดปุ่มวงกลมเมื่อไม่ถูกกด    
  461. {
  462.   glcdCircle(140, 48, 6, GLCD_GRAY);
  463. }
  464.  
  465. void checkButtonCross()                          // ฟังก์ชั่นตรวจสอบการกดปุ่มกากบาท  
  466. {
  467.   if(ps2x.ButtonPressed(PSB_CROSS))              // ถ้าปุ่มกากบาทถูกกด    
  468.   {
  469.     drawButtonCrossOn();                         // เรียกฟังก์ชั่นวาดปุ่มกากบาทเมื่อถูกกด
  470.   }
  471.   else if(ps2x.ButtonReleased(PSB_CROSS))        // ถ้าปุ่มกากบาทไม่ถูกกด  
  472.   {
  473.     drawButtonCrossOff();                        // เรียกฟังก์ชั่นวาดปุ่มกากบาทเมื่อไม่ถูกกด
  474.   }
  475. }
  476.  
  477. void drawButtonCrossOn()                         // ฟังก์ชั่นวาดปุ่มกากบาทเมื่อถูกกด
  478. {
  479.   glcdLine(118, 58, 130, 70, GLCD_BLUE);
  480.   glcdLine(130, 58, 118, 70, GLCD_BLUE);
  481. }
  482.  
  483. void drawButtonCrossOff()                        // ฟังก์ชั่นวาดปุ่มกากบาทเมื่อไม่ถูกกด    
  484. {
  485.   glcdLine(118, 58, 130, 70, GLCD_GRAY);
  486.   glcdLine(130, 58, 118, 70, GLCD_GRAY);
  487. }
  488.  
  489. void checkButtonSquare()                         // ฟังก์ชั่นตรวจสอบการกดปุ่มสี่เหลี่ยม
  490. {
  491.   if(ps2x.ButtonPressed(PSB_SQUARE))             // ถ้าปุ่มสี่เหลี่ยมถูกกด    
  492.   {
  493.     drawButtonSquareOn();                        // เรียกฟังก์ชั่นวาดปุ่มสี่เหลี่ยมเมื่อถูกกด
  494.   }
  495.   else if(ps2x.ButtonReleased(PSB_SQUARE))       // ถ้าปุ่มสี่เหลี่ยมไม่ถูกกด  
  496.   {
  497.     drawButtonSquareOff();                       // เรียกฟังก์ชั่นวาดปุ่มสี่เหลี่ยมเมื่อไม่ถูกกด
  498.   }
  499. }
  500.  
  501. void drawButtonSquareOn()                        // ฟังก์ชั่นวาดปุ่มสี่เหลี่ยมเมื่อถูกกด
  502. {
  503.   glcdRect(102, 43, 12, 12, GLCD_MAGENTA);
  504. }
  505.  
  506. void drawButtonSquareOff()                       // ฟังก์ชั่นวาดปุ่มสี่เหลี่ยมเมื่อไม่ถูกกด  
  507. {
  508.   glcdRect(102, 43, 12, 12, GLCD_GRAY);
  509. }
  510.  
  511. void checkButtonTriangle()                       // ฟังก์ชั่นตรวจสอบการกดปุ่มสามเหลี่ยม
  512. {
  513.   if(ps2x.ButtonPressed(PSB_TRIANGLE))           // ถ้าปุ่มสามเหลี่ยมถูกกด
  514.   {
  515.     drawButtonTriangleOn();                      // เรียกฟังก์ชั่นวาดปุ่มสามเหลี่ยมเมื่อถูกกด
  516.   }
  517.   else if(ps2x.ButtonReleased(PSB_TRIANGLE))     // ถ้าปุ่มสามเหลี่ยมไม่ถูกกด
  518.   {
  519.     drawButtonTriangleOff();                     // เรียกฟังก์ชั่นวาดปุ่มสามเหลี่ยมเมื่อไม่ถูกกด
  520.   }  
  521. }
  522.  
  523. void drawButtonTriangleOn()                      // ฟังก์ชั่นวาดปุ่มสามเหลี่ยมเมื่อถูกกด  
  524. {
  525.   glcdLine(117, 40, 131, 40, GLCD_DKGREEN);
  526.   glcdLine(117, 40, 124, 28, GLCD_DKGREEN);
  527.   glcdLine(131, 40, 124, 28, GLCD_DKGREEN);
  528. }
  529.  
  530. void drawButtonTriangleOff()                     // ฟังก์ชั่นวาดปุ่มสามเหลี่ยมเมื่อไม่ถูกกด  
  531. {
  532.   glcdLine(117, 40, 131, 40, GLCD_GRAY);
  533.   glcdLine(117, 40, 124, 28, GLCD_GRAY);
  534.   glcdLine(131, 40, 124, 28, GLCD_GRAY);
  535. }
  536.  
  537. void checkButtonSelect()                         // ฟังก์ชั่นตรวจสอบการกดปุ่ม SELECT
  538. {
  539.   if(ps2x.ButtonPressed(PSB_SELECT))             // ถ้าปุ่ม SELECT ถูกกด
  540.   {
  541.     drawButtonSelectOn();                        // เรียกฟังก์ชั่นวาดปุ่ม SELECT เมื่อถูกกด
  542.   }
  543.   else if(ps2x.ButtonReleased(PSB_SELECT))       // ถ้าปุ่ม SELECT ไม่ถูกกด  
  544.   {
  545.     drawButtonSelectOff();                       // เรียกฟังก์ชั่นวาดปุ่ม SELECT เมื่อไม่ถูกกด
  546.   }  
  547. }
  548.  
  549. void drawButtonSelectOn()                        // ฟังก์ชั่นวาดปุ่ม SELECT เมื่อถูกกด  
  550. {
  551.   glcdFillRect(62, 65, 12, 9, GLCD_ORANGE);
  552. }
  553.  
  554. void drawButtonSelectOff()                       // ฟังก์ชั่นวาดปุ่ม SELECT เมื่อไม่ถูกกด  
  555. {
  556.   glcdFillRect(62, 65, 12, 9, GLCD_GRAY);
  557. }
  558.  
  559. void checkButtonStart()                          // ฟังก์ชั่นตรวจสอบการกดปุ่ม START  
  560. {
  561.   if(ps2x.ButtonPressed(PSB_START))              // ถ้าปุ่ม START ถูกกด
  562.   {
  563.     drawButtonStartOn();                         // เรียกฟังก์ชั่นวาดปุ่ม START เมื่อถูกกด
  564.   }
  565.   else if(ps2x.ButtonReleased(PSB_START))        // ถ้าปุ่ม START ไม่ถูกกด  
  566.   {
  567.     drawButtonStartOff();                        // เรียกฟังก์ชั่นวาดปุ่ม START เมื่อไม่ถูกกด
  568.   }  
  569. }
  570.  
  571. void drawButtonStartOn()                         // ฟังก์ชั่นวาดปุ่ม START เมื่อถูกกด
  572. {
  573.   glcdLine(84, 65, 84, 73, GLCD_ORANGE);
  574.   glcdLine(85, 65, 85, 73, GLCD_ORANGE);
  575.   glcdLine(86, 65, 86, 73, GLCD_ORANGE);
  576.   glcdLine(87, 66, 87, 72, GLCD_ORANGE);
  577.   glcdLine(88, 66, 88, 72, GLCD_ORANGE);
  578.   glcdLine(89, 66, 89, 72, GLCD_ORANGE);
  579.   glcdLine(90, 67, 90, 71, GLCD_ORANGE);
  580.   glcdLine(91, 67, 91, 71, GLCD_ORANGE);
  581.   glcdLine(92, 67, 92, 71, GLCD_ORANGE);
  582.   glcdLine(93, 68, 93, 70, GLCD_ORANGE);
  583.   glcdLine(94, 68, 94, 70, GLCD_ORANGE);
  584.   glcdLine(95, 68, 95, 70, GLCD_ORANGE);
  585.   glcdPixel(96, 69, GLCD_ORANGE);
  586. }
  587.  
  588. void drawButtonStartOff()                        // ฟังก์ชั่นวาดปุ่ม START เมื่อไม่ถูกกด
  589. {
  590.   glcdLine(84, 65, 84, 73, GLCD_GRAY);
  591.   glcdLine(85, 65, 85, 73, GLCD_GRAY);
  592.   glcdLine(86, 65, 86, 73, GLCD_GRAY);
  593.   glcdLine(87, 66, 87, 72, GLCD_GRAY);
  594.   glcdLine(88, 66, 88, 72, GLCD_GRAY);
  595.   glcdLine(89, 66, 89, 72, GLCD_GRAY);
  596.   glcdLine(90, 67, 90, 71, GLCD_GRAY);
  597.   glcdLine(91, 67, 91, 71, GLCD_GRAY);
  598.   glcdLine(92, 67, 92, 71, GLCD_GRAY);
  599.   glcdLine(93, 68, 93, 70, GLCD_GRAY);
  600.   glcdLine(94, 68, 94, 70, GLCD_GRAY);
  601.   glcdLine(95, 68, 95, 70, GLCD_GRAY);
  602.   glcdPixel(96, 69, GLCD_GRAY);
  603. }
  604.  
  605. void checkAnalogL()                              // ฟังก์ชั่นตรวจสอบค่าจากปุ่มอะนาลอกซ้าย
  606. {
  607.   int x = ps2x.Analog(PSS_LX);                   // อ่านค่าแกน X ของปุ่มอะนาลอก
  608.    int y = ps2x.Analog(PSS_LY);                  // อ่านค่าแกน Y ของปุ่มอะนาลอก
  609.  
  610.   if(x != oldLX || y != oldLY)                   // ถ้า X หรือ Y มีค่าไม่เท่ากับของเดิมที่เก็บไว้ในตัวแปร oldLX และ oldLY
  611.   {
  612.     oldLX = map(oldLX, 0, 255, 26, 54);          // ให้ลบภาพเดิมออก แล้ววาดใหม่ให้เป็นพิกัด XY ล่าสุดของปุ่มอะนาลอกซ้าย
  613.     oldLY = map(oldLY, 0, 255, 82, 110);
  614.     glcdPixel(oldLX, oldLY, GLCD_WHITE);
  615.     glcdCircle(oldLX, oldLY, 1, GLCD_WHITE);
  616.     oldLX = x;
  617.     oldLY = y;
  618.     x = map(x, 0, 255, 26, 54);
  619.     y = map(y, 0, 255, 82, 110);
  620.     glcdPixel(x, y, GLCD_RED);
  621.     glcdCircle(x, y, 1, GLCD_RED);
  622.   }
  623. }
  624.  
  625. void checkAnalogR()                              // ฟังก์ชั่นตรวจสอบค่าจากปุ่มอะนาลอกขวา
  626. {
  627.   int x = ps2x.Analog(PSS_RX);                   // อ่านค่าแกน X ของปุ่มอะนาลอก
  628.   int y = ps2x.Analog(PSS_RY);                   // อ่านค่าแกน Y ของปุ่มอะนาลอก
  629.  
  630.   if(x != oldRX || y != oldRY)                  
  631.   {
  632.     oldRX = map(oldRX, 0, 255, 106, 134);        // ลบภาพเดิมออก แล้ววาดใหม่ให้เป็นพิกัด XY ล่าสุดของปุ่มอะนาลอกขวา
  633.     oldRY = map(oldRY, 0, 255, 82, 110);
  634.     glcdPixel(oldRX, oldRY, GLCD_WHITE);
  635.     glcdCircle(oldRX, oldRY, 1, GLCD_WHITE);
  636.     oldRX = x;
  637.     oldRY = y;
  638.     x = map(x, 0, 255, 106, 134);
  639.     y = map(y, 0, 255, 82, 110);
  640.     glcdPixel(x, y, GLCD_RED);
  641.     glcdCircle(x, y, 1, GLCD_RED);
  642.   }
  643. }
  644.  
  645. void drawAnalogBorder()                          // ฟังก์ชั่นวาดกรอบแสดงค่าของปุ่มอะนาลอก
  646. {
  647.   glcdRect(24, 80, 33, 33, GLCD_GRAY);           // วาดกรอบสำหรับปุ่มอะนาลอกซ้าย
  648.   glcdRect(104, 80, 33, 33, GLCD_GRAY);          // วาดกรอบสำหรับปุ่มอะนาลอกขวา
  649. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement