TA的每日心情 | 奋斗 2023-7-8 16:17 |
---|
签到天数: 971 天 连续签到: 1 天 [LV.10]以坛为家III
|
钛极小龟的开发,要添加必需的Library
在工程属性中找到“Java Build Path" 中Libraries中选择"Add Library" 文件
LED的控制需要用到 以下两个库
import tijos.framework.devicecenter.TiGPIO;import tijos.framework.transducer.led.TiLED;
具体代码如下:
- import java.io.IOException;
- import tijos.framework.devicecenter.TiGPIO;
- import tijos.framework.transducer.led.TiLED;
- import tijos.framework.util.Delay;
- public class TiJOSLED {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- try {
- /*
- * 定义使用的TiGPIO port
- */
- int gpioPort0 = 0;
- /*
- * 定义使用的TiGPIO Pin
- */
- int gpioPin2 = 2;
- /*
- * 资源分配, 将gpioPort与gpioPin0分配给TiGPIO对象gpio2
- */
- TiGPIO gpio0 = TiGPIO.open(gpioPort0,gpioPin2);
- /*
- * 资源绑定, 创建TiLED对象red并将gpio0和gpioPin2与其绑定
- */
- TiLED redRelay = new TiLED(gpio0,gpioPin2,true);
- /*
- * 资源使用, 控制灯的亮与灭
- */
- while(true) {
- redRelay.turnOn();
- System.out.println("redRelay is turned on");
-
- Delay.msDelay(5000);
-
- redRelay.turnOff();
- System.out.println("redRelay is turned off");
- Delay.msDelay(5000);
- }
- }catch(IOException ex) {
- ex.printStackTrace();
- }
- }
- }
复制代码 我们更好奇的是
import tijos.framework.devicecenter.TiGPIO;import tijos.framework.transducer.led.TiLED;
这两个库是怎么实现的,
TiGPIO没有找到任任何资料,至于 TiLED 的代码如下:
- package tijos.framework.transducer.led;
- import java.io.IOException;
- import tijos.framework.devicecenter.TiGPIO;
- public class TiLED {
- /**
- * TiLED pin id
- */
- private int ledPin;
- /**
- * TiLED high level active flag
- */
- private boolean highActive;
- /**
- * TiLED turned on flag
- */
- private boolean ledOn;
- /**
- * TiGPIO object
- */
- private TiGPIO gpioObj;
- /**
- * TiLED initialization, default:highLevel=false
- *
- * @param gpio
- * TiGPIO object[IN]
- * @param signalPinID
- * signal pin id[IN]
- * @throws IOException
- */
- public TiLED(TiGPIO gpio, int signalPinID) throws IOException {
- this(gpio, signalPinID, false);
- }
- /**
- * TiLED initialization
- *
- * @param gpio
- * TiGPIO object[IN]
- * @param signalPinID
- * signal pin id[IN]
- * @param highLevel
- * true:high level active, false:low level active[IN]
- * @throws IOException
- */
- public TiLED(TiGPIO gpio, int signalPinID, boolean highLevel) throws IOException {
- gpio.setWorkMode(signalPinID, TiGPIO.OUTPUT_PP);
- gpio.writePin(signalPinID, highLevel ? 0 : 1);
- gpioObj = gpio;
- ledPin = signalPinID;
- highActive = highLevel;
- ledOn = false;
- }
- /**
- * Turn on
- *
- * @throws IOException
- *
- */
- public void turnOn() throws IOException {
- synchronized (this) {
- gpioObj.writePin(ledPin, highActive ? 1 : 0);
- ledOn = true;
- }
- }
- /**
- * Turn off
- *
- * @throws IOException
- *
- */
- public void turnOff() throws IOException {
- synchronized (this) {
- gpioObj.writePin(ledPin, highActive ? 0 : 1);
- ledOn = false;
- }
- }
- /**
- * Turn over
- *
- * @throws IOException
- *
- */
- public void turnOver() throws IOException {
- synchronized (this) {
- if (ledOn)
- gpioObj.writePin(ledPin, highActive ? 0 : 1);
- else
- gpioObj.writePin(ledPin, highActive ? 1 : 0);
- ledOn = !ledOn;
- }
- }
- /**
- * Check if turned on
- *
- * @return true or false
- */
- public boolean isTurnedOn() {
- synchronized (this) {
- return ledOn;
- }
- }
- /**
- * Gets the signal pin id
- *
- * @return pin id
- */
- public int getSignalPinID() {
- return ledPin;
- }
- }
复制代码 运行一下,看到继电器的灯5秒亮 5秒灭的循环。
整体的工程文件如下
HelloTiJOS.rar
(6.26 KB, 下载次数: 0, 售价: 1 与非币)
|
|