查看: 2255|回复: 1

[原创] 【 钛极小龟】基础篇总结

[复制链接]
  • TA的每日心情
    奋斗
    2023-7-8 16:17
  • 签到天数: 971 天

    连续签到: 1 天

    [LV.10]以坛为家III

    发表于 2018-12-5 11:08:28 | 显示全部楼层 |阅读模式
    分享到:
    经过快二周的时间,终于把基本的外设备弄清楚啦!
    汇总的代码
    1. import java.io.IOException;

    2. import tijos.framework.devicecenter.TiADC;
    3. import tijos.framework.devicecenter.TiGPIO;
    4. import tijos.framework.devicecenter.TiI2CMaster;
    5. import tijos.framework.platform.peripheral.ITiKeyboardListener;
    6. import tijos.framework.platform.peripheral.TiKeyboard;
    7. import tijos.framework.platform.wlan.TiWiFi;
    8. import tijos.framework.sensor.button.ITiButtonEventListener;
    9. import tijos.framework.sensor.button.TiButton;
    10. import tijos.framework.sensor.dht.TiDHT;
    11. import tijos.framework.sensor.vs1838b.ITiVS1838BNECEventListener;
    12. import tijos.framework.sensor.vs1838b.TiVS1838BNEC;
    13. import tijos.framework.transducer.oled.TiOLED_UG2864;
    14. import tijos.framework.transducer.relay.TiRelay1CH;
    15. import tijos.framework.util.Delay;

    16. /**
    17. * WiFi SmartConfig线程类
    18. *
    19. */
    20. class WiFiScThread implements Runnable{
    21.         TiOLED_UG2864 _oOled_UG2864;
    22.        
    23.         //构造
    24.         public WiFiScThread(TiOLED_UG2864 oled_UG2864) {
    25.                 this._oOled_UG2864 = oled_UG2864;
    26.         }

    27.         @Override
    28.         public void run() {
    29.                 // TODO Auto-generated method stub
    30.                
    31.                 try {
    32.                         // 显示SmartConfig启动信息
    33.                         this._oOled_UG2864.print(3, 0, "Wi-Fi SC startup ");
    34.                         // 启动SmartConfig 30秒超时
    35.                         TiWiFi.getInstance().smartConfig(60);
    36.                         // 显示成功信息
    37.                         this._oOled_UG2864.print(3, 0, "Wi-Fi SC success");
    38.                        
    39.                 } catch (IOException ie) {
    40.                         // TODO: handle exception
    41.                         try {
    42.                                 // 显示失败信息
    43.                                 this._oOled_UG2864.print(3, 0, "Wi-Fi SC fail   ");
    44.                         } catch (IOException ie1) {
    45.                                 // TODO: handle exception
    46.                                 ie1.printStackTrace();
    47.                         }
    48.                 }
    49.         }
    50. }

    51. /**
    52. * MIC模拟信号采集线程类
    53. */
    54. class EMICThread implements Runnable{
    55.         TiADC _aAdc;
    56.         TiOLED_UG2864 _oOled_UG2864;
    57.        
    58.         // 构造
    59.         public EMICThread(TiADC adc,TiOLED_UG2864 oled_UG2864) {
    60.                 this._aAdc = adc;
    61.                 this._oOled_UG2864 = oled_UG2864;
    62.         }

    63.         @Override
    64.         public void run() {
    65.                 // TODO Auto-generated method stub
    66.                 try {
    67.                         while (true) {
    68.                                 // 获取咪头(MIC)电压
    69.                                 int val = this._aAdc.getRawValue(0);
    70.                                 //显示咪头(MIC)电压
    71.                                 this._oOled_UG2864.print(2, 0, "MIC: " + val + "     ");
    72.                                 Delay.msDelay(50);
    73.                                
    74.                         }
    75.                 } catch (IOException ie) {
    76.                         // TODO: handle exception
    77.                         ie.printStackTrace();
    78.                 }
    79.         }
    80. }

    81. /**
    82. * 光照度采集线程类
    83. */
    84. class BH1750Thread implements Runnable{
    85.         TiBH1750 _bBh1750;
    86.         TiOLED_UG2864 _oOled_UG2864;
    87.        
    88.         // 构造
    89.         public BH1750Thread(TiBH1750 bh1750,TiOLED_UG2864 oled ) {
    90.                 this._bBh1750 = bh1750;
    91.                 this._oOled_UG2864 = oled;
    92.         }

    93.         @Override
    94.         public void run() {
    95.                 // TODO Auto-generated method stub
    96.                 try {
    97.                         while (true) {
    98.                                 // 获取光照度值
    99.                                 int lux = this._bBh1750.readLightLevel();
    100.                                 // 显示温湿度及光照度
    101.                                 this._oOled_UG2864.print(1, 0, "LUX: " + lux + "    ");
    102.                                 Delay.msDelay(500);
    103.                         }
    104.                 } catch (IOException e) {
    105.                         // TODO: handle exception
    106.                         e.printStackTrace();
    107.                 }
    108.         }
    109. }

    110. /**
    111. * 键盘监听类
    112. */
    113. class KeyboadListener implements ITiKeyboardListener{
    114.         TiOLED_UG2864 _oOled_UG2864;
    115.         Thread _tThread;
    116.        
    117.         // 构造
    118.         public KeyboadListener(TiOLED_UG2864 oled_UG2864) {
    119.                 this._oOled_UG2864 = oled_UG2864;
    120.         }

    121.         @Override
    122.         public void onPressed(int id, long time) {
    123.                 // TODO Auto-generated method stub
    124.                
    125.         }

    126.         @Override
    127.         public void onReleased(int id, long time) {
    128.                 // TODO Auto-generated method stub
    129.                 // 确保SmartConfig线程还未创建
    130.                 if(this._tThread!=null && this._tThread.isAlive()) {
    131.                         return;
    132.                 }
    133.                
    134.                 // 创建SmartConfig线程并启动
    135.                 this._tThread = new Thread(new WiFiScThread(this._oOled_UG2864));
    136.                 this._tThread.start();
    137.                                
    138.         }
    139. }

    140. /**
    141. * 红外解码监听类
    142. */
    143. class IRDecodeLister implements ITiVS1838BNECEventListener{
    144.         TiOLED_UG2864 _oOled_UG2864;
    145.         // 构造
    146.         public IRDecodeLister(TiOLED_UG2864 oled_UG2864) {
    147.                 this._oOled_UG2864 = oled_UG2864;
    148.         }
    149.         @Override
    150.         public void cmdReceived(TiVS1838BNEC vs1838b) {
    151.                 // TODO Auto-generated method stub
    152.                 try {
    153.                         // 显示红外解码值
    154.                         this._oOled_UG2864.print(3, 0, "IR: " + vs1838b.getAddress() + " - " + vs1838b.getCommand() + "       ");
    155.                 } catch (IOException e) {
    156.                         // TODO: handle exception
    157.                         e.printStackTrace();
    158.                 }
    159.         }
    160.         @Override
    161.         public void cmdRepeat(TiVS1838BNEC vs1838b) {
    162.                 // TODO Auto-generated method stub
    163.                
    164.         }
    165. }

    166. /**
    167. * 触摸监听类
    168. */
    169. class TouchButtonListener implements ITiButtonEventListener{
    170.         TiRelay1CH _rRelay1ch;
    171.        
    172.         // 构造
    173.         public TouchButtonListener(TiRelay1CH relay1ch) {
    174.                 this._rRelay1ch = relay1ch;
    175.         }

    176.         @Override
    177.         public void onPressed(TiButton button) {
    178.                 // TODO Auto-generated method stub
    179.                 try {
    180.                         // 继电器打开
    181.                         this._rRelay1ch.turnOn();
    182.                 } catch (IOException e) {
    183.                         // TODO: handle exception
    184.                         e.printStackTrace();
    185.                 }
    186.         }

    187.         @Override
    188.         public void onReleased(TiButton button) {
    189.                 // TODO Auto-generated method stub
    190.                 try {
    191.                         // 继电器关闭
    192.                         this._rRelay1ch.turnOff();
    193.                 } catch (IOException e) {
    194.                         // TODO: handle exception
    195.                         e.printStackTrace();
    196.                 }
    197.         }
    198.        
    199. }

    200. /**
    201. * 开发板全功能测试
    202. */
    203. public class TiKitTest {

    204.         public static void main(String[] args) {
    205.                 // TODO Auto-generated method stub
    206.                 try {
    207.                         /* ---资源分配--- */
    208.                         // GPIO资源分配,GPIO0的PIN2/3/4/5脚
    209.                         TiGPIO gTiGPIO = TiGPIO.open(0, 2,3,4,5);
    210.                         // I2C主机总线资源分配,I2CM0
    211.                         TiI2CMaster i2cm0 = TiI2CMaster.open(0);
    212.                         // ADC资源分配,ADC0的CH0通道
    213.                         TiADC adc0 = TiADC.open(0, 0);
    214.                        
    215.                         /* ---资源绑定--- */
    216.                         // I2C主机总线资源PORT0与屏幕对象绑定,屏幕地址:0x3C
    217.                         TiOLED_UG2864 oled_UG2864 = new TiOLED_UG2864(i2cm0, 0x3c);
    218.                         // I2C主机总线资源PORT0与照度计BH1750对象绑定,默认地址:0x23
    219.                         TiBH1750 bh1750 = new TiBH1750(i2cm0);
    220.                         // GPIO总线资源PIN2与继电器对象绑定
    221.                         TiRelay1CH relay1ch = new TiRelay1CH(gTiGPIO, 2);
    222.                         // GPIO总线资源PIN3与温湿度传感器DHT11绑定
    223.                         TiDHT dht11 = new TiDHT(gTiGPIO, 3);
    224.                         // GPIO总线资源PIN4与触摸按钮对象绑定,触发电平:高电平
    225.                         TiButton touch = new TiButton(gTiGPIO, 4,true);
    226.                         touch.setEventListener(new TouchButtonListener(relay1ch));
    227.                         // GPIO总线资源PIN5与红外解码对象绑定 并设置红外解码事件监听对象
    228.                         TiVS1838BNEC vs1838bnec = new TiVS1838BNEC(gTiGPIO, 5);
    229.                         vs1838bnec.setEventListener(new IRDecodeLister(oled_UG2864));
    230.                         // 获取键盘实例并设置键盘事件监听对象 并设置触摸按钮事件监听对象
    231.                         TiKeyboard keyboard = TiKeyboard.getInstance();
    232.                         keyboard.setEventListener(new KeyboadListener(oled_UG2864));
    233.                        
    234.                         // 设置ADC参考电压及分压倍数
    235.                         adc0.setRefVoltageValue(1.0, 2);
    236.                        
    237.                         /* ---资源使用--- */
    238.                         // 屏幕开启并清屏
    239.                         oled_UG2864.turnOn();
    240.                         oled_UG2864.clear();
    241.                         oled_UG2864.print(3, 0, "Welcomes YOU ! ");
    242.                        
    243.                         // 创建MIC模拟电压采集线程对象并启动线程
    244.                         Thread thread1 = new Thread(new EMICThread(adc0, oled_UG2864));
    245.                         thread1.setDaemon(true);
    246.                         thread1.start();
    247.                        
    248.                         // 创建光照度采集线程对象并启动线程
    249.                         Thread thread2 = new Thread(new BH1750Thread(bh1750, oled_UG2864) );
    250.                         thread2.setDaemon(true);
    251.                         thread2.start();
    252.                        
    253.                         while (true) {
    254.                                 try {
    255.                                         // 启动温湿度测量
    256.                                         dht11.measure();
    257.                                         // 获取温湿度值
    258.                                         double temp = dht11.getTemperature();
    259.                                         double humi = dht11.getHumidity();
    260.                                        
    261.                                         // 显示温湿度
    262.                                         oled_UG2864.print(0, 0, "TH: " + temp + "C  " + humi + "%");
    263.                                         System.out.println("TH: " + temp + "C  " + humi + "%");
    264.                                         // 等待3秒
    265.                                         Delay.msDelay(3000);
    266.                                 }catch(Exception e) {
    267.                                         e.printStackTrace();
    268.                                 }
    269.                         }
    270.                        
    271.                 } catch (IOException e) {
    272.                         // TODO: handle exception
    273.                         e.printStackTrace();
    274.                 }
    275.         }

    276. }
    复制代码


    虽然是做完了,遇到不少的坑,只是后来发现,其实是与硬件电路相关,而且在GPIO的配置上真的是摸着石头过河

    提供一下硬件电路图,原来只有纸质的,后来在官网上找到的
    电路原理图(TiKit-T600-ESP8266B V1.3).pdf (512.89 KB, 下载次数: 5)
    回复

    使用道具 举报

  • TA的每日心情

    2023-7-25 22:49
  • 签到天数: 385 天

    连续签到: 1 天

    [LV.9]以坛为家II

    发表于 2018-12-5 13:14:47 | 显示全部楼层
    null9.png null8.png

    null7.png null6.png

    null5.png null4.png

    null3.png null2.png

    null1.png null0.png


    回复 支持 反对

    使用道具 举报

    您需要登录后才可以回帖 注册/登录

    本版积分规则

    关闭

    站长推荐上一条 /2 下一条



    手机版|小黑屋|与非网

    GMT+8, 2024-4-16 13:48 , Processed in 0.135268 second(s), 19 queries , MemCache On.

    ICP经营许可证 苏B2-20140176  苏ICP备14012660号-2   苏州灵动帧格网络科技有限公司 版权所有.

    苏公网安备 32059002001037号

    Powered by Discuz! X3.4

    Copyright © 2001-2020, Tencent Cloud.