LaunchPad温度实验上位机改成摄氏度显示

[复制链接]
 楼主| cuter_2000 发表于 2012-7-30 18:08 | 显示全部楼层 |阅读模式
本帖最后由 cuter_2000 于 2012-7-31 11:33 编辑

LaunchPad温度实验上位机程序是用processing做的,显示的是华氏温度,看着不顺眼
改成摄氏度显示也很容易,加一行即可。
  1. /*
  2. * LaunchPad_Temp_GUI.pde
  3. *
  4. * LaunchPad Temperature GUI
  5. *
  6. * Copyright (C) {YEAR} Texas Instruments Incorporated - http://www.ti.com/
  7. *
  8. *
  9. *  Redistribution and use in source and binary forms, with or without
  10. *  modification, are permitted provided that the following conditions
  11. *  are met:
  12. *
  13. *    Redistributions of source code must retain the above copyright
  14. *    notice, this list of conditions and the following disclaimer.
  15. *
  16. *    Redistributions in binary form must reproduce the above copyright
  17. *    notice, this list of conditions and the following disclaimer in the
  18. *    documentation and/or other materials provided with the   
  19. *    distribution.
  20. *
  21. *    Neither the name of Texas Instruments Incorporated nor the names of
  22. *    its contributors may be used to endorse or promote products derived
  23. *    from this software without specific prior written permission.
  24. *
  25. *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  26. *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  27. *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  28. *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  29. *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  30. *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  31. *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  32. *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  33. *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  34. *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  35. *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36. *
  37. */


  38. /**
  39. * LaunchPad GUI
  40. *
  41. * Select COM port that LaunchPad is connected to
  42. * receive serial communication from LaunchPad for
  43. * instantaneous temperature readings.
  44. *
  45. **/
  46. PFont fontA;
  47. char instruct;
  48. color backColor;
  49. int dataRead;
  50. boolean portChosen = false;
  51. int COMPort;
  52. int [] keyIn = new int[3];
  53. int i, keyIndex=0;



  54. // The serial port:
  55. Serial myPort;

  56. void setup()
  57. {
  58.   //setup window

  59.   size(1000, 500);
  60.   smooth();
  61.   //load font

  62.   
  63.   // Set the font, its size (in units of pixels), and alignment
  64.   textAlign(CENTER);
  65.   
  66.   //Set background color
  67.   color baseColor = color(70);
  68.   backColor = baseColor;
  69.   //import serial library
  70.   import processing.serial.*;

  71.   // List all the available serial ports, then give prompt
  72.   println(Serial.list());
  73.   println("Please type in the serial COM port that your LaunchPad is connected to.");
  74.   
  75.   background(backColor);
  76.   stroke(255);
  77.   textSize(13);
  78.   text("Please, select the COM port that your LaunchPad is connected to.", 265, 20);
  79.   textAlign(LEFT);
  80.   for(i=0; i<Serial.list().length; i++){
  81.     text("[" + i + "] " + Serial.list()[i], 215, 38+13*i);
  82.   }
  83.   textSize(30);
  84.   text("# =",20, 350);
  85.   //wait for keypress --> Refer to void keyPressed()
  86. }

  87. void draw()
  88. {
  89.   if(portChosen == true){
  90.     dataRead= myPort.read();
  91.     if(dataRead != -1){
  92.       dataRead= (int)((dataRead - 32.0) * 5.0/9.0);//转成摄氏度
  93.       
  94.       //clear previous temperature reading from screen
  95.       background(backColor);
  96.       stroke(255);
  97.       //Update console
  98.       print("Temp: ");
  99.       print(dataRead);
  100.       println("°");
  101.       
  102.       //Update on screen GUI
  103.       text("Current Temperature: ", 420, 60);
  104.       text(dataRead, 835, 60);      
  105.       text("°", 900, 60);      
  106.       
  107.     }
  108.   }
  109. }

  110. //wait for key press. Once key is entered, initialize serial com port
  111. void keyPressed() {
  112.   if(portChosen == false){
  113.    
  114.     if (key != 10) //Enter
  115.     {
  116.       keyIn[keyIndex++] = key-48;
  117.       textSize(30);
  118.       text(keyIn[keyIndex-1],100 + (keyIndex*20), 350);
  119.     }
  120.     else
  121.     {
  122.       COMPort = 0;
  123.       for (i = 0; i < keyIndex; i++)
  124.         COMPort = COMPort * 10 + keyIn[i];      
  125.       println(COMPort);
  126.       myPort = new Serial(this, Serial.list()[COMPort], 2400);
  127.       portChosen = true;
  128.       textSize( 60); // change font size & alignment for temp readings
  129.       textAlign(CENTER);
  130.     }
  131.   }
  132. }


附件是编译好的

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

×

评分

参与人数 1威望 +2 收起 理由
tianm + 2

查看全部评分

figo20042005 发表于 2012-7-30 21:30 | 显示全部楼层
就是加了温度单位的转换公式么,可以尝试更大的改动
 楼主| cuter_2000 发表于 2012-7-31 12:42 | 显示全部楼层
还没想到有哪些可以改的
说说你的想法吧。给个意见
vivilzb1985 发表于 2012-8-1 17:47 | 显示全部楼层
谢谢分享,该例程很有针对性的啊
cgd 发表于 2012-8-2 08:59 | 显示全部楼层
向楼上学习
您需要登录后才可以回帖 登录 | 注册

本版积分规则

0

主题

12

帖子

0

粉丝

0

主题

12

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部