Last active
December 19, 2016 18:54
-
-
Save jdkoren/a3c37883f839c0b3adcee43821128329 to your computer and use it in GitHub Desktop.
Marquee text display for Android Things using HT16K33
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright 2016 Google Inc. | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
public class MarqueeActivity extends Activity { | |
private static final String TAG = "MarqueeActivity"; | |
private static final String MARQUEE_TEXT = "Hello Android Things <3"; | |
private AlphanumericDisplay mDisplay; | |
private Handler mHandler; | |
private MarqueeRunnable mRunnable = new MarqueeRunnable(MARQUEE_TEXT); | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
try { | |
mDisplay = new AlphanumericDisplay(BoardDefaults.getI2CPort()); | |
mDisplay.clear(); | |
mDisplay.setEnabled(true); | |
} catch (IOException e) { | |
throw new RuntimeException("Error configuring display", e); | |
} | |
mHandler = new Handler(); | |
} | |
@Override | |
protected void onStart() { | |
super.onStart(); | |
mHandler.post(mRunnable); | |
} | |
@Override | |
protected void onStop() { | |
super.onStop(); | |
mHandler.removeCallbacks(mRunnable); | |
} | |
@Override | |
protected void onDestroy() { | |
super.onDestroy(); | |
if (mDisplay == null) { | |
try { | |
mDisplay.close(); | |
} catch (IOException e) { | |
Log.e(TAG, "Error closing display", e); | |
} | |
mDisplay = null; | |
} | |
} | |
private class MarqueeRunnable implements Runnable { | |
private static final long MARQUEE_DELAY = 750L; | |
private String mText; | |
private int mStartIndex = 0; | |
MarqueeRunnable(String text) { | |
if (text == null) { | |
throw new NullPointerException(); | |
} | |
// pad the front with spaces (up to 4) as needed | |
char[] a = text.toCharArray(); | |
int padding = 4; | |
for (int i = 0; i < 4 && i < a.length; i++) { | |
if (a[i] != ' ') { | |
break; | |
} | |
padding--; | |
} | |
if (padding > 0) { | |
char[] c = new char[a.length + padding]; | |
System.arraycopy(a, 0, c, padding, a.length); | |
Arrays.fill(c, 0, padding, ' '); | |
a = c; | |
} | |
mText = new String(a); | |
} | |
@Override | |
public void run() { | |
if (mDisplay == null) { | |
return; | |
} | |
try { | |
mDisplay.display(mText.substring(mStartIndex)); | |
} catch (IOException e) { | |
Log.e(TAG, "Error setting display", e); | |
return; | |
} | |
mStartIndex = (mStartIndex + 1) % mText.length(); | |
mHandler.postDelayed(this, MARQUEE_DELAY); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment