]>
Commit | Line | Data |
---|---|---|
1 | package de.rmdir.ms2debounce; | |
2 | ||
3 | import android.app.Activity; | |
4 | import android.app.Dialog; | |
5 | import android.app.AlertDialog; | |
6 | import android.os.Bundle; | |
7 | import android.content.Intent; | |
8 | import android.content.DialogInterface; | |
9 | import android.widget.TextView; | |
10 | import android.widget.EditText; | |
11 | import android.widget.Button; | |
12 | import android.widget.CheckBox; | |
13 | import android.view.View; | |
14 | import android.view.Menu; | |
15 | import android.view.MenuInflater; | |
16 | import android.view.MenuItem; | |
17 | import android.text.TextWatcher; | |
18 | import android.text.Editable; | |
19 | ||
20 | public class MS2Debounce extends Activity | |
21 | { | |
22 | private DebounceModuleHelper module; | |
23 | ||
24 | // Calling these is expensive, so cache the result... | |
25 | private boolean loaded; | |
26 | private boolean safe_to_load; | |
27 | private int debounce_delay; | |
28 | private int settle_time; | |
29 | private int poll_time; | |
30 | private boolean hw_debounce_en; | |
31 | private int hw_debounce_time; | |
32 | private boolean drive_inactive_en; | |
33 | private boolean active_high_en; | |
34 | ||
35 | public MS2Debounce() | |
36 | { | |
37 | super(); | |
38 | module = new DebounceModuleHelper(this); | |
39 | } | |
40 | ||
41 | @Override | |
42 | public void onCreate(Bundle savedInstanceState) | |
43 | { | |
44 | super.onCreate(savedInstanceState); | |
45 | ||
46 | setContentView(R.layout.main); | |
47 | ||
48 | EditText textDelay = (EditText)findViewById(R.id.debounce_delay); | |
49 | textDelay.addTextChangedListener(new TextWatcher() { | |
50 | @Override | |
51 | public void afterTextChanged(Editable delay) { | |
52 | if (delay.toString().length() > 0) { | |
53 | module.setSavedDelay(Integer.parseInt(delay.toString())); | |
54 | } | |
55 | } | |
56 | ||
57 | @Override | |
58 | public void beforeTextChanged(CharSequence s, int start, int count, int after) { | |
59 | } | |
60 | ||
61 | @Override | |
62 | public void onTextChanged(CharSequence s, int start, int before, int count) { | |
63 | } | |
64 | }); | |
65 | ||
66 | EditText textSettle = (EditText)findViewById(R.id.settle_time); | |
67 | textSettle.addTextChangedListener(new TextWatcher() { | |
68 | @Override | |
69 | public void afterTextChanged(Editable settle_time) { | |
70 | if (settle_time.toString().length() > 0) { | |
71 | module.setSavedSettle(Integer.parseInt(settle_time.toString())); | |
72 | } | |
73 | } | |
74 | ||
75 | @Override | |
76 | public void beforeTextChanged(CharSequence s, int start, int count, int after) { | |
77 | } | |
78 | ||
79 | @Override | |
80 | public void onTextChanged(CharSequence s, int start, int before, int count) { | |
81 | } | |
82 | }); | |
83 | ||
84 | EditText textPoll = (EditText)findViewById(R.id.poll_time); | |
85 | textPoll.addTextChangedListener(new TextWatcher() { | |
86 | @Override | |
87 | public void afterTextChanged(Editable poll_time) { | |
88 | if (poll_time.toString().length() > 0) { | |
89 | module.setSavedPoll(Integer.parseInt(poll_time.toString())); | |
90 | } | |
91 | } | |
92 | ||
93 | @Override | |
94 | public void beforeTextChanged(CharSequence s, int start, int count, int after) { | |
95 | } | |
96 | ||
97 | @Override | |
98 | public void onTextChanged(CharSequence s, int start, int before, int count) { | |
99 | } | |
100 | }); | |
101 | ||
102 | EditText textHwDebounceTime = (EditText)findViewById(R.id.hw_debounce_time); | |
103 | textHwDebounceTime.addTextChangedListener(new TextWatcher() { | |
104 | @Override | |
105 | public void afterTextChanged(Editable hw_debounce_time) { | |
106 | if (hw_debounce_time.toString().length() > 0) { | |
107 | module.setSavedHwDebounceTime(Integer.parseInt(hw_debounce_time.toString())); | |
108 | } | |
109 | } | |
110 | ||
111 | @Override | |
112 | public void beforeTextChanged(CharSequence s, int start, int count, int after) { | |
113 | } | |
114 | ||
115 | @Override | |
116 | public void onTextChanged(CharSequence s, int start, int before, int count) { | |
117 | } | |
118 | }); | |
119 | ||
120 | updateUI(); | |
121 | } | |
122 | ||
123 | private void updateUI() { | |
124 | disableUI(); | |
125 | ||
126 | // Calling these is expensive, so cache the result... | |
127 | loaded = module.isLoaded(); | |
128 | safe_to_load = module.is_safe_to_load(); | |
129 | debounce_delay = module.getDelay(); | |
130 | settle_time = module.getSettle(); | |
131 | poll_time = module.getPoll(); | |
132 | hw_debounce_en = module.getHwDebounce(); | |
133 | hw_debounce_time = module.getHwDebounceTime(); | |
134 | //drive_inactive_en = module.getDriveInactive(); | |
135 | active_high_en = module.getActiveHigh(); | |
136 | ||
137 | TextView text = (TextView)findViewById(R.id.text); | |
138 | text.setText("Module loaded: " + loaded + "\n" + | |
139 | "debounce_delay: " + debounce_delay + "ms\n" + | |
140 | "settle_time: " + settle_time + "us\n" + | |
141 | "poll_time: " + poll_time + "ms\n" + | |
142 | "safe_to_load: " + safe_to_load + " (module loaded by this app)\n" + | |
143 | "hw_debounce: " + (hw_debounce_en?"en":"dis") + "abled, " + ((hw_debounce_time+1)*31) + "us (" + hw_debounce_time + "), active high: " + (active_high_en?"en":"dis") + "abled"); | |
144 | ||
145 | EditText textDelay = (EditText)findViewById(R.id.debounce_delay); | |
146 | textDelay.setText(Integer.toString(module.getSavedDelay())); | |
147 | textDelay.setEnabled(true); | |
148 | ||
149 | EditText textSettle = (EditText)findViewById(R.id.settle_time); | |
150 | textSettle.setText(Integer.toString(module.getSavedSettle())); | |
151 | textSettle.setEnabled(true); | |
152 | ||
153 | EditText textPoll = (EditText)findViewById(R.id.poll_time); | |
154 | textPoll.setText(Integer.toString(module.getSavedPoll())); | |
155 | textPoll.setEnabled(true); | |
156 | ||
157 | EditText textHwDebounceTime = (EditText)findViewById(R.id.hw_debounce_time); | |
158 | textHwDebounceTime.setText(Integer.toString(module.getSavedHwDebounceTime())); | |
159 | textHwDebounceTime.setEnabled(true); | |
160 | ||
161 | Button set = (Button)findViewById(R.id.set); | |
162 | if (loaded) { | |
163 | set.setEnabled(true); | |
164 | } else { | |
165 | set.setEnabled(false); | |
166 | } | |
167 | ||
168 | Button load = (Button)findViewById(R.id.load); | |
169 | if (loaded) { | |
170 | load.setEnabled(false); | |
171 | } else { | |
172 | load.setEnabled(true); | |
173 | } | |
174 | ||
175 | Button unload = (Button)findViewById(R.id.unload); | |
176 | if (loaded) { | |
177 | unload.setEnabled(true); | |
178 | } else { | |
179 | unload.setEnabled(false); | |
180 | } | |
181 | ||
182 | CheckBox on_boot = (CheckBox)findViewById(R.id.on_boot); | |
183 | on_boot.setChecked(module.get_on_boot()); | |
184 | if (safe_to_load) { | |
185 | on_boot.setEnabled(true); | |
186 | } else { | |
187 | on_boot.setEnabled(false); | |
188 | } | |
189 | ||
190 | CheckBox hw_debounce = (CheckBox)findViewById(R.id.hw_debounce); | |
191 | hw_debounce.setChecked(module.getSavedHwDebounce()); | |
192 | hw_debounce.setEnabled(true); | |
193 | ||
194 | //CheckBox drive_inactive = (CheckBox)findViewById(R.id.drive_inactive); | |
195 | //drive_inactive.setChecked(module.getSavedDriveInactive()); | |
196 | //drive_inactive.setEnabled(true); | |
197 | ||
198 | CheckBox active_high = (CheckBox)findViewById(R.id.active_high); | |
199 | active_high.setChecked(module.getSavedActiveHigh()); | |
200 | active_high.setEnabled(true); | |
201 | } | |
202 | ||
203 | private void disableUI() { | |
204 | EditText textDelay = (EditText)findViewById(R.id.debounce_delay); | |
205 | textDelay.setEnabled(false); | |
206 | ||
207 | EditText textSettle = (EditText)findViewById(R.id.settle_time); | |
208 | textSettle.setEnabled(false); | |
209 | ||
210 | EditText textPoll = (EditText)findViewById(R.id.poll_time); | |
211 | textPoll.setEnabled(false); | |
212 | ||
213 | EditText textHwDebounceTime = (EditText)findViewById(R.id.hw_debounce_time); | |
214 | textHwDebounceTime.setEnabled(false); | |
215 | ||
216 | Button set = (Button)findViewById(R.id.set); | |
217 | set.setEnabled(false); | |
218 | ||
219 | Button load = (Button)findViewById(R.id.load); | |
220 | load.setEnabled(false); | |
221 | ||
222 | Button unload = (Button)findViewById(R.id.unload); | |
223 | unload.setEnabled(false); | |
224 | ||
225 | CheckBox on_boot = (CheckBox)findViewById(R.id.on_boot); | |
226 | on_boot.setEnabled(false); | |
227 | ||
228 | CheckBox hw_debounce = (CheckBox)findViewById(R.id.hw_debounce); | |
229 | hw_debounce.setEnabled(false); | |
230 | ||
231 | //CheckBox drive_inactive = (CheckBox)findViewById(R.id.drive_inactive); | |
232 | //drive_inactive.setEnabled(false); | |
233 | ||
234 | CheckBox active_high = (CheckBox)findViewById(R.id.active_high); | |
235 | active_high.setEnabled(false); | |
236 | } | |
237 | ||
238 | public void loadModule(View view) { | |
239 | disableUI(); | |
240 | if (!module.isLoaded()) { | |
241 | module.loadModule(); | |
242 | } | |
243 | updateUI(); | |
244 | } | |
245 | ||
246 | public void unloadModule(View view) { | |
247 | disableUI(); | |
248 | if (module.isLoaded()) { | |
249 | module.unloadModule(); | |
250 | } | |
251 | updateUI(); | |
252 | } | |
253 | ||
254 | public void setValues(View view) { | |
255 | disableUI(); | |
256 | if (!module.isLoaded()) { | |
257 | module.loadModule(); | |
258 | } | |
259 | module.setAllValues(); | |
260 | updateUI(); | |
261 | } | |
262 | ||
263 | public void toggle_on_boot(View view) { | |
264 | CheckBox on_boot = (CheckBox)view; | |
265 | ||
266 | module.set_on_boot(on_boot.isChecked()); | |
267 | } | |
268 | ||
269 | public void toggle_hw_debounce(View view) { | |
270 | CheckBox hw_debounce = (CheckBox)view; | |
271 | ||
272 | module.setSavedHwDebounce(hw_debounce.isChecked()); | |
273 | } | |
274 | ||
275 | //public void toggle_drive_inactive(View view) { | |
276 | // CheckBox drive_inactive = (CheckBox)view; | |
277 | ||
278 | // module.setSavedDriveInactive(drive_inactive.isChecked()); | |
279 | //} | |
280 | ||
281 | public void toggle_active_high(View view) { | |
282 | CheckBox active_high = (CheckBox)view; | |
283 | ||
284 | module.setSavedActiveHigh(active_high.isChecked()); | |
285 | } | |
286 | ||
287 | @Override | |
288 | public boolean onCreateOptionsMenu(Menu menu) { | |
289 | MenuInflater inflater = getMenuInflater(); | |
290 | inflater.inflate(R.menu.main, menu); | |
291 | return true; | |
292 | } | |
293 | ||
294 | @Override | |
295 | public boolean onOptionsItemSelected(MenuItem item) { | |
296 | switch (item.getItemId()) { | |
297 | case R.id.about: | |
298 | showDialog(42); | |
299 | return true; | |
300 | default: | |
301 | return super.onOptionsItemSelected(item); | |
302 | } | |
303 | } | |
304 | ||
305 | protected Dialog onCreateDialog(int id) { | |
306 | Dialog dlg = null; | |
307 | ||
308 | AlertDialog.Builder about = new AlertDialog.Builder(this); | |
309 | about.setMessage("Milestone 2 Debounce\n\n(C) 2011 Michael Gernoth <michael@gernoth.net>\n\nThis program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation version 2 of the License.\n\nThis program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.\n\nYou should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA") | |
310 | .setCancelable(true) | |
311 | .setPositiveButton("Ok", new DialogInterface.OnClickListener() { | |
312 | public void onClick(DialogInterface dialog, int id) { | |
313 | dialog.cancel(); | |
314 | } | |
315 | }); | |
316 | dlg = about.create(); | |
317 | ||
318 | return dlg; | |
319 | } | |
320 | } |