Last active
February 18, 2018 02:56
-
-
Save kumamotone/073433f1bc061201a4cb71fca8eb68ea to your computer and use it in GitHub Desktop.
WeatherStationActivity.kt
This file contains hidden or 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
class WeatherStationActivity : Activity() { | |
private var mDisplay: AlphanumericDisplay? = null | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
Log.d(TAG, "Weather Station Started") | |
try { | |
mDisplay = RainbowHat.openDisplay() | |
mDisplay?.setEnabled(true) | |
mDisplay?.display("OK ") | |
Log.d(TAG, "Initialized I2C Display") | |
} catch (e: IOException) { | |
throw RuntimeException("Error initializing display", e) | |
} | |
try { | |
if (ledstrip != null) { | |
ledstrip?.close() | |
} | |
} catch (e: IOException) { | |
Log.d(TAG, e.toString()) | |
} | |
val database = FirebaseDatabase.getInstance() | |
val reference = database.getReference("googlehome").child("word") | |
reference.addValueEventListener(object : ValueEventListener { | |
override fun onDataChange(dataSnapshot: DataSnapshot) { | |
val str = dataSnapshot.getValue(String::class.java) | |
if (TextUtils.equals(str, "party")) { | |
try { | |
val rainbow = IntArray(7) | |
ledstrip = RainbowHat.openLedStrip() | |
ledstrip?.setBrightness(1) | |
for (i in rainbow.indices) { | |
rainbow[i] = Color.HSVToColor(255, floatArrayOf(i * 360f / rainbow.size, 1.0f, 1.0f)) | |
} | |
ledstrip?.write(rainbow) | |
} catch (e: IOException) { | |
Log.d(TAG, e.toString()) | |
} | |
Log.d("party!", str) | |
} | |
} | |
override fun onCancelled(databaseError: DatabaseError) {} | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment