TA的每日心情 | 奋斗 2023-7-8 16:17 |
---|
签到天数: 971 天 连续签到: 1 天 [LV.10]以坛为家III
|
经过快二周的时间,终于把基本的外设备弄清楚啦!
汇总的代码
- import java.io.IOException;
- import tijos.framework.devicecenter.TiADC;
- import tijos.framework.devicecenter.TiGPIO;
- import tijos.framework.devicecenter.TiI2CMaster;
- import tijos.framework.platform.peripheral.ITiKeyboardListener;
- import tijos.framework.platform.peripheral.TiKeyboard;
- import tijos.framework.platform.wlan.TiWiFi;
- import tijos.framework.sensor.button.ITiButtonEventListener;
- import tijos.framework.sensor.button.TiButton;
- import tijos.framework.sensor.dht.TiDHT;
- import tijos.framework.sensor.vs1838b.ITiVS1838BNECEventListener;
- import tijos.framework.sensor.vs1838b.TiVS1838BNEC;
- import tijos.framework.transducer.oled.TiOLED_UG2864;
- import tijos.framework.transducer.relay.TiRelay1CH;
- import tijos.framework.util.Delay;
- /**
- * WiFi SmartConfig线程类
- *
- */
- class WiFiScThread implements Runnable{
- TiOLED_UG2864 _oOled_UG2864;
-
- //构造
- public WiFiScThread(TiOLED_UG2864 oled_UG2864) {
- this._oOled_UG2864 = oled_UG2864;
- }
- @Override
- public void run() {
- // TODO Auto-generated method stub
-
- try {
- // 显示SmartConfig启动信息
- this._oOled_UG2864.print(3, 0, "Wi-Fi SC startup ");
- // 启动SmartConfig 30秒超时
- TiWiFi.getInstance().smartConfig(60);
- // 显示成功信息
- this._oOled_UG2864.print(3, 0, "Wi-Fi SC success");
-
- } catch (IOException ie) {
- // TODO: handle exception
- try {
- // 显示失败信息
- this._oOled_UG2864.print(3, 0, "Wi-Fi SC fail ");
- } catch (IOException ie1) {
- // TODO: handle exception
- ie1.printStackTrace();
- }
- }
- }
- }
- /**
- * MIC模拟信号采集线程类
- */
- class EMICThread implements Runnable{
- TiADC _aAdc;
- TiOLED_UG2864 _oOled_UG2864;
-
- // 构造
- public EMICThread(TiADC adc,TiOLED_UG2864 oled_UG2864) {
- this._aAdc = adc;
- this._oOled_UG2864 = oled_UG2864;
- }
- @Override
- public void run() {
- // TODO Auto-generated method stub
- try {
- while (true) {
- // 获取咪头(MIC)电压
- int val = this._aAdc.getRawValue(0);
- //显示咪头(MIC)电压
- this._oOled_UG2864.print(2, 0, "MIC: " + val + " ");
- Delay.msDelay(50);
-
- }
- } catch (IOException ie) {
- // TODO: handle exception
- ie.printStackTrace();
- }
- }
- }
- /**
- * 光照度采集线程类
- */
- class BH1750Thread implements Runnable{
- TiBH1750 _bBh1750;
- TiOLED_UG2864 _oOled_UG2864;
-
- // 构造
- public BH1750Thread(TiBH1750 bh1750,TiOLED_UG2864 oled ) {
- this._bBh1750 = bh1750;
- this._oOled_UG2864 = oled;
- }
- @Override
- public void run() {
- // TODO Auto-generated method stub
- try {
- while (true) {
- // 获取光照度值
- int lux = this._bBh1750.readLightLevel();
- // 显示温湿度及光照度
- this._oOled_UG2864.print(1, 0, "LUX: " + lux + " ");
- Delay.msDelay(500);
- }
- } catch (IOException e) {
- // TODO: handle exception
- e.printStackTrace();
- }
- }
- }
- /**
- * 键盘监听类
- */
- class KeyboadListener implements ITiKeyboardListener{
- TiOLED_UG2864 _oOled_UG2864;
- Thread _tThread;
-
- // 构造
- public KeyboadListener(TiOLED_UG2864 oled_UG2864) {
- this._oOled_UG2864 = oled_UG2864;
- }
- @Override
- public void onPressed(int id, long time) {
- // TODO Auto-generated method stub
-
- }
- @Override
- public void onReleased(int id, long time) {
- // TODO Auto-generated method stub
- // 确保SmartConfig线程还未创建
- if(this._tThread!=null && this._tThread.isAlive()) {
- return;
- }
-
- // 创建SmartConfig线程并启动
- this._tThread = new Thread(new WiFiScThread(this._oOled_UG2864));
- this._tThread.start();
-
- }
- }
- /**
- * 红外解码监听类
- */
- class IRDecodeLister implements ITiVS1838BNECEventListener{
- TiOLED_UG2864 _oOled_UG2864;
- // 构造
- public IRDecodeLister(TiOLED_UG2864 oled_UG2864) {
- this._oOled_UG2864 = oled_UG2864;
- }
- @Override
- public void cmdReceived(TiVS1838BNEC vs1838b) {
- // TODO Auto-generated method stub
- try {
- // 显示红外解码值
- this._oOled_UG2864.print(3, 0, "IR: " + vs1838b.getAddress() + " - " + vs1838b.getCommand() + " ");
- } catch (IOException e) {
- // TODO: handle exception
- e.printStackTrace();
- }
- }
- @Override
- public void cmdRepeat(TiVS1838BNEC vs1838b) {
- // TODO Auto-generated method stub
-
- }
- }
- /**
- * 触摸监听类
- */
- class TouchButtonListener implements ITiButtonEventListener{
- TiRelay1CH _rRelay1ch;
-
- // 构造
- public TouchButtonListener(TiRelay1CH relay1ch) {
- this._rRelay1ch = relay1ch;
- }
- @Override
- public void onPressed(TiButton button) {
- // TODO Auto-generated method stub
- try {
- // 继电器打开
- this._rRelay1ch.turnOn();
- } catch (IOException e) {
- // TODO: handle exception
- e.printStackTrace();
- }
- }
- @Override
- public void onReleased(TiButton button) {
- // TODO Auto-generated method stub
- try {
- // 继电器关闭
- this._rRelay1ch.turnOff();
- } catch (IOException e) {
- // TODO: handle exception
- e.printStackTrace();
- }
- }
-
- }
- /**
- * 开发板全功能测试
- */
- public class TiKitTest {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- try {
- /* ---资源分配--- */
- // GPIO资源分配,GPIO0的PIN2/3/4/5脚
- TiGPIO gTiGPIO = TiGPIO.open(0, 2,3,4,5);
- // I2C主机总线资源分配,I2CM0
- TiI2CMaster i2cm0 = TiI2CMaster.open(0);
- // ADC资源分配,ADC0的CH0通道
- TiADC adc0 = TiADC.open(0, 0);
-
- /* ---资源绑定--- */
- // I2C主机总线资源PORT0与屏幕对象绑定,屏幕地址:0x3C
- TiOLED_UG2864 oled_UG2864 = new TiOLED_UG2864(i2cm0, 0x3c);
- // I2C主机总线资源PORT0与照度计BH1750对象绑定,默认地址:0x23
- TiBH1750 bh1750 = new TiBH1750(i2cm0);
- // GPIO总线资源PIN2与继电器对象绑定
- TiRelay1CH relay1ch = new TiRelay1CH(gTiGPIO, 2);
- // GPIO总线资源PIN3与温湿度传感器DHT11绑定
- TiDHT dht11 = new TiDHT(gTiGPIO, 3);
- // GPIO总线资源PIN4与触摸按钮对象绑定,触发电平:高电平
- TiButton touch = new TiButton(gTiGPIO, 4,true);
- touch.setEventListener(new TouchButtonListener(relay1ch));
- // GPIO总线资源PIN5与红外解码对象绑定 并设置红外解码事件监听对象
- TiVS1838BNEC vs1838bnec = new TiVS1838BNEC(gTiGPIO, 5);
- vs1838bnec.setEventListener(new IRDecodeLister(oled_UG2864));
- // 获取键盘实例并设置键盘事件监听对象 并设置触摸按钮事件监听对象
- TiKeyboard keyboard = TiKeyboard.getInstance();
- keyboard.setEventListener(new KeyboadListener(oled_UG2864));
-
- // 设置ADC参考电压及分压倍数
- adc0.setRefVoltageValue(1.0, 2);
-
- /* ---资源使用--- */
- // 屏幕开启并清屏
- oled_UG2864.turnOn();
- oled_UG2864.clear();
- oled_UG2864.print(3, 0, "Welcomes YOU ! ");
-
- // 创建MIC模拟电压采集线程对象并启动线程
- Thread thread1 = new Thread(new EMICThread(adc0, oled_UG2864));
- thread1.setDaemon(true);
- thread1.start();
-
- // 创建光照度采集线程对象并启动线程
- Thread thread2 = new Thread(new BH1750Thread(bh1750, oled_UG2864) );
- thread2.setDaemon(true);
- thread2.start();
-
- while (true) {
- try {
- // 启动温湿度测量
- dht11.measure();
- // 获取温湿度值
- double temp = dht11.getTemperature();
- double humi = dht11.getHumidity();
-
- // 显示温湿度
- oled_UG2864.print(0, 0, "TH: " + temp + "C " + humi + "%");
- System.out.println("TH: " + temp + "C " + humi + "%");
- // 等待3秒
- Delay.msDelay(3000);
- }catch(Exception e) {
- e.printStackTrace();
- }
- }
-
- } catch (IOException e) {
- // TODO: handle exception
- e.printStackTrace();
- }
- }
- }
复制代码
虽然是做完了,遇到不少的坑,只是后来发现,其实是与硬件电路相关,而且在GPIO的配置上真的是摸着石头过河
提供一下硬件电路图,原来只有纸质的,后来在官网上找到的
电路原理图(TiKit-T600-ESP8266B V1.3).pdf
(512.89 KB, 下载次数: 5)
|
|