ATuin

RGBArduino

Nov 17th, 2011
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 3.67 KB | None | 0 0
  1. package com.landroid.atuin.RGBArduinoBT
  2.  
  3. import android.app.Activity
  4. import android.os.Bundle
  5. import android.view.View
  6. import android.widget.Toast
  7. import android.widget.TextView
  8. import android.widget.SeekBar
  9. import android.widget.SeekBar.OnSeekBarChangeListener
  10. import android.widget.CompoundButton
  11. import android.widget.CompoundButton.OnCheckedChangeListener
  12. import android.util.Log
  13. import android.content.Context
  14. import android.bluetooth.BluetoothAdapter
  15. import android.content.Intent
  16. import scala.collection.mutable.HashMap
  17.  
  18. class RGBColor (var r:Int, var g:Int, var b:Int) {
  19.   def setByIndex (index:Int, value:Int) {
  20.     index match {
  21.       case 1 => r = value
  22.       case 2 => g = value
  23.       case 3 => b = value
  24.     }
  25.   }
  26.  
  27.   def getByIndex (index:Int): Int = index match {
  28.       case 1 => r
  29.       case 2 => g
  30.       case 3 => b
  31.     }
  32. }
  33.  
  34. trait BTSerial {
  35.   val mBT = BluetoothAdapter.getDefaultAdapter()
  36.   val context:Context
  37.  
  38.   if (mBT == null) {
  39.     Toast.makeText(context, "BT Not avaible", Toast.LENGTH_LONG).show()
  40.   }
  41.  
  42.   def enable () {
  43.     if (! mBT.isEnabled()) {
  44.       mBT.enable()
  45.       val enabler = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE)
  46.       context.startActivityForResult(enabler, REQUEST_DISCOVERABLE)
  47.     }
  48.   }
  49.  
  50.   def disable () {
  51.     mBT.disable()
  52.   }
  53.  
  54.   def discover () {
  55. //    val enabler = new Intent(this, classOf[DiscoveryActivitay])
  56.  //   context.startActivity(enabler)
  57.   }
  58. }
  59.  
  60. class RGBArduinoBT extends TypedActivity
  61.                   with BTSerial
  62.                   with OnCheckedChangeListener
  63.                   with OnSeekBarChangeListener {
  64.   lazy val redSeekBar = findView(TR.redSeekBar)
  65.   lazy val greenSeekBar = findView(TR.greenSeekBar)
  66.   lazy val blueSeekBar = findView(TR.blueSeekBar)
  67.   lazy val rgb = new RGBColor(30,30,30)
  68.   lazy val context = getApplicationContext()
  69.  
  70.   def updateProgress () = {
  71.     redSeekBar.setProgress(rgb.r)
  72.     greenSeekBar.setProgress(rgb.g)
  73.     blueSeekBar.setProgress(rgb.b)
  74.   }
  75.  
  76.   def findSeekBarByCheckBoxId (checkBoxId: Int): (Int, SeekBar) = checkBoxId match {
  77.     case R.id.redCheckBox => (1,redSeekBar)
  78.     case R.id.greenCheckBox => (2,greenSeekBar)
  79.     case R.id.blueCheckBox => (3,blueSeekBar)
  80.   }
  81.  
  82.   /** Called when the activity is first created. */
  83.   override def onCreate(savedInstanceState: Bundle) {
  84.     super.onCreate(savedInstanceState)
  85.     setContentView(R.layout.main)
  86.  
  87.     updateProgress()
  88.     findView(TR.redCheckBox).setOnCheckedChangeListener(this)
  89.     findView(TR.greenCheckBox).setOnCheckedChangeListener(this)
  90.     findView(TR.blueCheckBox).setOnCheckedChangeListener(this)
  91.     redSeekBar.setOnSeekBarChangeListener(this)
  92.     greenSeekBar.setOnSeekBarChangeListener(this)
  93.     blueSeekBar.setOnSeekBarChangeListener(this)
  94.  
  95.     Log.i("RGBArduinoBT", "redCheckBox:" + TR.redCheckBox)
  96.   }
  97.  
  98.   override def onCheckedChanged (button: CompoundButton,
  99.                                 isChecked: Boolean) {
  100.     val (index, progressBar) = findSeekBarByCheckBoxId(button.getId())
  101.  
  102.     if (isChecked == true) {
  103.       rgb.setByIndex(index, progressBar.getProgress())
  104.       progressBar.setProgress(0)
  105.     }
  106.     else
  107.       progressBar.setProgress(rgb.getByIndex(index))
  108.   }
  109.  
  110.   /*
  111.    * Get events for progress bar changes.
  112.    * Send value for specified led to arduino via bt
  113.    */
  114.   override def onProgressChanged (seekBar:SeekBar,
  115.                                   progress:Int,
  116.                                   fromUser:Boolean) {
  117.     Log.i("RGBArduinoBT", "onProgressChanged:" + seekBar.toString() + ": " + progress)
  118.   }
  119.  
  120.   override def onStopTrackingTouch (seekBar:SeekBar) {}
  121.   override def onStartTrackingTouch (seekBar: SeekBar) {}
  122.  
  123. }
  124.  
Advertisement
Add Comment
Please, Sign In to add comment