huangcm
2025-04-09 02d4ce54b909bd733f12e9f3fa4c1b03cf2d6f45
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
 
package jp.co.ntt.east.hardware;
 
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;
 
import android.view.KeyEvent;
 
public class IrRemoteController {
   
   static {
       System.loadLibrary("rk29_cir");
   }
   
   private static final int SEND_WAIT_TIME = 250; 
   private static IrRemoteController instance;
   private volatile boolean isRunning;
   private IrRemoteController.Data[] data;
   private int timeout;
   private int count;
   private Timer timer;
   
   public static synchronized IrRemoteController getInstance() {
       if (instance == null) {
           instance = new IrRemoteController();
       }
       return instance;
   }
   
   public void send(IrRemoteController.Data[] data, int timeout) throws IOException {
       send(data, timeout, -1);
   }
   
   public void send(IrRemoteController.Data[] data, int timeout, int count) throws IOException {
       if (isRunning) {
           throw new IllegalStateException("IrRemote is running.");
       }
       this.data = data;
       this.timeout = timeout;
       this.count = count;
       
       isRunning = true;
       new SendThread().start();
       timer.schedule(new TimerThread(), (long)timeout * 1000L);
   }
   
   public void stop() throws IOException {
       isRunning = false;
       native_hal_stop();
       timer.cancel();
   }
   
   private IrRemoteController() {
       timer = new Timer();
   }
   
   private static native int native_hal_init();
   private static native int native_hal_deinit();
   private static native int native_hal_send();
   private static native int native_hal_stop();
   private static native int native_hal_set_formate(int high, int low, byte[] data,
           int length, int duration, int startHigh, int startLow, int stopHigh,
           int data0Pattern, int data0High, int data0Low, int data1Pattern, 
           int data1High, int data1Low, int count);
   
   public static class Data {
       public static final int HIGH_LOW = 1;
       public static final int INFINITE = 0;
       public static final int LOW_HIGH = 2;
       
       int carry_high;
       int carry_low;
       int data0Pattern;
       int data0High;
       int data0Low;
       int data1Pattern;
       int data1High;
       int data1Low;
       int startHigh;
       int startLow;
       int stopHigh;
       byte[] data;
       int length;
       int count;
       int duration;
       
       public void setCarrier(int high, int low) {
           this.carry_high = (int)(high / 10.0);
           this.carry_low = (int)(low / 10.0);
       }
       
       public void setPulse(int data0Pattern, int data0High, int data0Low,
               int data1Pattern, int data1High, int data1Low) {
           this.data0Pattern = (data0Pattern == HIGH_LOW) ? 0 : 1;
           this.data0High = data0High;
           this.data0Low = data0Low;
           this.data1Pattern = (data1Pattern  == HIGH_LOW) ? 0 : 1;
           this.data1High = data1High;
           this.data1Low = data1Low;            
       }
       
       public void setParameter(int startHigh, int startLow, int stopHigh) {
           this.startHigh = startHigh;
           this.startLow = startLow;
           this.stopHigh = stopHigh;
       }
       
       public void setData(byte[] data, int length) {
           this.data = data;
           this.length = length;
       }
       
       public void setRepeatCount(int count) {
           this.count = (count == INFINITE) ? -1 : count;
       }
       
       public void setDuration(int duration) {
           this.duration = duration;
       }
   }
   
   private class SendThread extends Thread {
       
       public void run() {
           native_hal_init();
           for (int i = 0; i < data.length && isRunning; i++) {
               Data d = data[i];
               native_hal_set_formate(d.carry_high, d.carry_low, d.data, d.length, d.duration,
                       d.startHigh, d.startLow, d.stopHigh, d.data0Pattern, d.data0High, 
                       d.data0Low, d.data1Pattern, d.data1High, d.data1Low, d.count);
               native_hal_send();
           }
           
           try {
               Thread.sleep(SEND_WAIT_TIME);
           }
           catch (InterruptedException e) {
               ;
           }
           isRunning = false;
           timer.cancel();
           native_hal_deinit();
       }
   }
   
   private class TimerThread extends TimerTask {
       
       public void run() {
           if (isRunning) {
               isRunning = false;
               native_hal_stop();
           }
       }
   }
}