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.sensor.button.ITiButtonEventListener;
- import tijos.framework.sensor.button.TiButton;
- import tijos.framework.transducer.oled.TiOLED_UG2864;
- import tijos.framework.transducer.relay.TiRelay1CH;
- import tijos.framework.util.Delay;
- public class TouchLight {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- try {
- // GPIO资源分配,GPIO0的PIN2和PIN4脚
- TiGPIO gpio0 = TiGPIO.open(0,2,4);
- // GPIO资源分配,GPIO0的PIN4脚
- TiI2CMaster i2cm0 = TiI2CMaster.open(0);
- // I2C主机总线资源与屏幕对象绑定,屏幕地址:0x3C
- TiOLED_UG2864 oled = new TiOLED_UG2864(i2cm0,0x3c);
- // 屏幕开启并清屏
- oled.turnOn();
- oled.clear();
- oled.print(0, 0, "ControlLight");
-
- // GPIO总线资源与继电器对象绑定
- TiRelay1CH relay = new TiRelay1CH(gpio0,2);
- // GPIO总线资源与触摸按钮对象绑定,触发电平:高电平
- TiButton touch = new TiButton(gpio0,4,true);
- // 创建触摸按钮事件监听者
- TouchListener lc = new TouchListener();
- // 设置触摸按钮事件监听者
- touch.setEventListener(lc);
-
- // ADC资源分配,ADC0的CH0通道
- TiADC adc0 = TiADC.open(0, 0);
- // 设置ADC参考电压1.0V,外部2倍分压
- adc0.setRefVoltageValue(1.0, 2);
- // 定义采集电压比较阈值:目前为15.0毫伏,需要根据实际情况调整。
- double threshold = 30.0;
- double voiceValue = 0;
- // 循环检测
- while(true) {
- // 检测到按钮按下
- if(lc.isPressed()) {
- // 打开继电器
- relay.turnOn();
- oled.print(2, 0, "Open switch");
- System.out.println("Touch Preass Open Switch ");
- // 延时10秒
- Delay.msDelay(10*1000);
- System.out.println("Time Out ");
- }else if(voiceValue>threshold ){
- // 打开继电器
- relay.turnOn();
- oled.print(2, 0, "Open switch");
- System.out.println("Voice Open Switch "+voiceValue);
- // 延时10秒
- Delay.msDelay(10*1000);
- System.out.println("Time Out ");
- }else{
- // 关闭继电器
- relay.turnOff();
- oled.print(2, 0, "Close switch ");
-
- }
- // 检测间隔100ms
- voiceValue = adc0.getVoltageValue(0)*1000;
- Delay.msDelay(100);
- }
- }catch(IOException ie) {
- ie.printStackTrace();
- }
- }
- }
- class TouchListener implements ITiButtonEventListener{
- boolean _isOnPressed = false;
- @Override
- public void onPressed(TiButton button) {
- // TODO Auto-generated method stub
- synchronized(this) {
- // 置按下标志
- this._isOnPressed = true;
- }
-
- }
- @Override
- public void onReleased(TiButton button) {
- // TODO Auto-generated method stub
- synchronized(this) {
- // 置释放标志
- this._isOnPressed = false;
- }
- }
-
- public boolean isPressed() {
- boolean pressed = false;
- synchronized(this) {
- // 返回置按下标志
- pressed = this._isOnPressed;
- }
- return pressed;
- }
-
-
- }
复制代码
TiRelay1CH类源码
TiRelay1CH.rar
(689 Bytes, 下载次数: 0, 售价: 1 与非币)
|
|