1 package de
.rmdir
.ms2debounce
;
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
;
20 public class MS2Debounce
extends Activity
22 private DebounceModuleHelper module
;
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
;
36 module
= new DebounceModuleHelper(this);
40 public void onCreate(Bundle savedInstanceState
)
42 super.onCreate(savedInstanceState
);
44 setContentView(R
.layout
.main
);
46 EditText textDelay
= (EditText
)findViewById(R
.id
.debounce_delay
);
47 textDelay
.addTextChangedListener(new TextWatcher() {
49 public void afterTextChanged(Editable delay
) {
50 if (delay
.toString().length() > 0) {
51 module
.setSavedDelay(Integer
.parseInt(delay
.toString()));
56 public void beforeTextChanged(CharSequence s
, int start
, int count
, int after
) {
60 public void onTextChanged(CharSequence s
, int start
, int before
, int count
) {
64 EditText textSettle
= (EditText
)findViewById(R
.id
.settle_time
);
65 textSettle
.addTextChangedListener(new TextWatcher() {
67 public void afterTextChanged(Editable settle_time
) {
68 if (settle_time
.toString().length() > 0) {
69 module
.setSavedSettle(Integer
.parseInt(settle_time
.toString()));
74 public void beforeTextChanged(CharSequence s
, int start
, int count
, int after
) {
78 public void onTextChanged(CharSequence s
, int start
, int before
, int count
) {
82 EditText textPoll
= (EditText
)findViewById(R
.id
.poll_time
);
83 textPoll
.addTextChangedListener(new TextWatcher() {
85 public void afterTextChanged(Editable poll_time
) {
86 if (poll_time
.toString().length() > 0) {
87 module
.setSavedPoll(Integer
.parseInt(poll_time
.toString()));
92 public void beforeTextChanged(CharSequence s
, int start
, int count
, int after
) {
96 public void onTextChanged(CharSequence s
, int start
, int before
, int count
) {
100 EditText textHwDebounceTime
= (EditText
)findViewById(R
.id
.hw_debounce_time
);
101 textHwDebounceTime
.addTextChangedListener(new TextWatcher() {
103 public void afterTextChanged(Editable hw_debounce_time
) {
104 if (hw_debounce_time
.toString().length() > 0) {
105 module
.setSavedHwDebounceTime(Integer
.parseInt(hw_debounce_time
.toString()));
110 public void beforeTextChanged(CharSequence s
, int start
, int count
, int after
) {
114 public void onTextChanged(CharSequence s
, int start
, int before
, int count
) {
121 private void updateUI() {
124 // Calling these is expensive, so cache the result...
125 loaded
= module
.isLoaded();
126 safe_to_load
= module
.is_safe_to_load();
127 debounce_delay
= module
.getDelay();
128 settle_time
= module
.getSettle();
129 poll_time
= module
.getPoll();
130 hw_debounce_en
= module
.getHwDebounce();
131 hw_debounce_time
= module
.getHwDebounceTime();
133 TextView text
= (TextView
)findViewById(R
.id
.text
);
134 text
.setText("Module loaded: " + loaded
+ "\n" +
135 "debounce_delay: " + debounce_delay
+ "ms\n" +
136 "settle_time: " + settle_time
+ "us\n" +
137 "poll_time: " + poll_time
+ "ms\n" +
138 "safe_to_load: " + safe_to_load
+ " (module loaded by this app)\n" +
139 "hw_debounce: " + (hw_debounce_en?
"enabled":"disabled") + ", " + ((hw_debounce_time
+1)*31) + "us (" + hw_debounce_time
+ ")");
141 EditText textDelay
= (EditText
)findViewById(R
.id
.debounce_delay
);
142 textDelay
.setText(Integer
.toString(module
.getSavedDelay()));
143 textDelay
.setEnabled(true);
145 EditText textSettle
= (EditText
)findViewById(R
.id
.settle_time
);
146 textSettle
.setText(Integer
.toString(module
.getSavedSettle()));
147 textSettle
.setEnabled(true);
149 EditText textPoll
= (EditText
)findViewById(R
.id
.poll_time
);
150 textPoll
.setText(Integer
.toString(module
.getSavedPoll()));
151 textPoll
.setEnabled(true);
153 EditText textHwDebounceTime
= (EditText
)findViewById(R
.id
.hw_debounce_time
);
154 textHwDebounceTime
.setText(Integer
.toString(module
.getSavedHwDebounceTime()));
155 textHwDebounceTime
.setEnabled(true);
157 Button set
= (Button
)findViewById(R
.id
.set
);
159 set
.setEnabled(true);
161 set
.setEnabled(false);
164 Button load
= (Button
)findViewById(R
.id
.load
);
166 load
.setEnabled(false);
168 load
.setEnabled(true);
171 Button unload
= (Button
)findViewById(R
.id
.unload
);
173 unload
.setEnabled(true);
175 unload
.setEnabled(false);
178 CheckBox on_boot
= (CheckBox
)findViewById(R
.id
.on_boot
);
179 on_boot
.setChecked(module
.get_on_boot());
181 on_boot
.setEnabled(true);
183 on_boot
.setEnabled(false);
186 CheckBox hw_debounce
= (CheckBox
)findViewById(R
.id
.hw_debounce
);
187 hw_debounce
.setChecked(module
.getSavedHwDebounce());
188 hw_debounce
.setEnabled(true);
191 private void disableUI() {
192 EditText textDelay
= (EditText
)findViewById(R
.id
.debounce_delay
);
193 textDelay
.setEnabled(false);
195 EditText textSettle
= (EditText
)findViewById(R
.id
.settle_time
);
196 textSettle
.setEnabled(false);
198 EditText textPoll
= (EditText
)findViewById(R
.id
.poll_time
);
199 textPoll
.setEnabled(false);
201 EditText textHwDebounceTime
= (EditText
)findViewById(R
.id
.hw_debounce_time
);
202 textHwDebounceTime
.setEnabled(false);
204 Button set
= (Button
)findViewById(R
.id
.set
);
205 set
.setEnabled(false);
207 Button load
= (Button
)findViewById(R
.id
.load
);
208 load
.setEnabled(false);
210 Button unload
= (Button
)findViewById(R
.id
.unload
);
211 unload
.setEnabled(false);
213 CheckBox on_boot
= (CheckBox
)findViewById(R
.id
.on_boot
);
214 on_boot
.setEnabled(false);
216 CheckBox hw_debounce
= (CheckBox
)findViewById(R
.id
.hw_debounce
);
217 hw_debounce
.setEnabled(false);
220 public void loadModule(View view
) {
222 if (!module
.isLoaded()) {
228 public void unloadModule(View view
) {
230 if (module
.isLoaded()) {
231 module
.unloadModule();
236 public void setValues(View view
) {
238 if (!module
.isLoaded()) {
241 module
.setAllValues();
245 public void toggle_on_boot(View view
) {
246 CheckBox on_boot
= (CheckBox
)view
;
248 module
.set_on_boot(on_boot
.isChecked());
251 public void toggle_hw_debounce(View view
) {
252 CheckBox hw_debounce
= (CheckBox
)view
;
254 module
.setSavedHwDebounce(hw_debounce
.isChecked());
258 public boolean onCreateOptionsMenu(Menu menu
) {
259 MenuInflater inflater
= getMenuInflater();
260 inflater
.inflate(R
.menu
.main
, menu
);
265 public boolean onOptionsItemSelected(MenuItem item
) {
266 switch (item
.getItemId()) {
271 return super.onOptionsItemSelected(item
);
275 protected Dialog
onCreateDialog(int id
) {
278 AlertDialog
.Builder about
= new AlertDialog
.Builder(this);
279 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")
281 .setPositiveButton("Ok", new DialogInterface
.OnClickListener() {
282 public void onClick(DialogInterface dialog
, int id
) {
286 dlg
= about
.create();