Last active
December 17, 2015 23:59
-
-
Save fraguada/5693555 to your computer and use it in GitHub Desktop.
Just trying to send RGB values via three sliders in an Android App. I do not understand how many bytes I need to send as Latch.
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
package ioio.bosioio; | |
import ioio.lib.api.SpiMaster; | |
import ioio.lib.api.exception.ConnectionLostException; | |
import ioio.lib.util.BaseIOIOLooper; | |
import ioio.lib.util.IOIOLooper; | |
import ioio.lib.util.android.IOIOActivity; | |
import android.os.Bundle; | |
import android.view.Menu; | |
import android.widget.SeekBar; | |
import android.widget.SeekBar.OnSeekBarChangeListener; | |
public class MainActivity extends IOIOActivity { | |
int red_ = 0; | |
int green_ = 0; | |
int blue_ = 0; | |
int numLEDS = 12; | |
byte[] buffer1_ = new byte[48]; | |
byte[] buffer2_ = new byte[48]; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
// The first seekbar controls the RED. | |
final SeekBar redBar = (SeekBar) findViewById(R.id.redBar); | |
updateRed(redBar, redBar.getProgress()); | |
redBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() { | |
@Override | |
public void onStopTrackingTouch(SeekBar seekBar) { | |
} | |
@Override | |
public void onStartTrackingTouch(SeekBar seekBar) { | |
} | |
@Override | |
public void onProgressChanged(SeekBar seekBar, int progress, | |
boolean fromUser) { | |
updateRed(seekBar, progress); | |
} | |
}); | |
// The second seekbar controls the GREEN | |
final SeekBar greenBar = (SeekBar) findViewById(R.id.greenBar); | |
updateGreen(greenBar, greenBar.getProgress()); | |
greenBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() { | |
@Override | |
public void onStopTrackingTouch(SeekBar seekBar) { | |
} | |
@Override | |
public void onStartTrackingTouch(SeekBar seekBar) { | |
} | |
@Override | |
public void onProgressChanged(SeekBar seekBar, int progress, | |
boolean fromUser) { | |
updateGreen(seekBar, progress); | |
} | |
}); | |
//Blue | |
final SeekBar blueBar = (SeekBar) findViewById(R.id.blueBar); | |
updateBlue(blueBar, blueBar.getProgress()); | |
blueBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() { | |
@Override | |
public void onStopTrackingTouch(SeekBar seekBar) { | |
} | |
@Override | |
public void onStartTrackingTouch(SeekBar seekBar) { | |
} | |
@Override | |
public void onProgressChanged(SeekBar seekBar, int progress, | |
boolean fromUser) { | |
updateBlue(seekBar, progress); | |
} | |
}); | |
} | |
@Override | |
protected void onStart() { | |
super.onStart(); | |
} | |
@Override | |
protected void onStop() { | |
super.onStop(); | |
} | |
/** An RGB triplet. */ | |
private static class RGB { | |
byte r; | |
byte g; | |
byte b; | |
RGB(byte r, byte g, byte b) { | |
this.r = r; | |
this.g = g; | |
this.b = b; | |
} | |
RGB() { | |
clear(); | |
} | |
public void clear() { | |
r = g = b = 0; | |
} | |
} | |
class IOIOThread extends BaseIOIOLooper { | |
private SpiMaster spi_; | |
@Override | |
protected void setup() throws ConnectionLostException { | |
spi_ = ioio_.openSpiMaster(5, 4, 3, 6, SpiMaster.Rate.RATE_50K); | |
} | |
@Override | |
public void loop() throws ConnectionLostException { | |
for (int i = 0; i < numLEDS; i++) { | |
RGB tmpRGB = new RGB(); | |
tmpRGB.r = (byte) (128 - (red_/2)*-1); | |
tmpRGB.g = (byte) (128 - (green_/2)*-1); | |
tmpRGB.b = (byte) (128 - (blue_/2)*-1); | |
setLed(i, tmpRGB); | |
} | |
byte[] latch = new byte[] {0,0}; //latch! Huh? | |
try{ | |
ioio_.beginBatch(); | |
spi_.writeRead(0, buffer1_, buffer1_.length,buffer1_.length, null, 0); | |
spi_.writeRead(0, latch, latch.length,latch.length, null, 0); | |
ioio_.endBatch(); | |
} catch (InterruptedException e1) { | |
} | |
} | |
} | |
@Override | |
protected IOIOLooper createIOIOLooper() { | |
return new IOIOThread(); | |
} | |
/** | |
* Set an LED to a certain color. | |
*/ | |
private void setLed(int num, RGB rgb) { | |
// Find the right buffer to write to (first or second half). | |
byte[] buffer; | |
if (num >= 16) { | |
buffer = buffer2_; | |
num -= 16; | |
} else { | |
buffer = buffer1_; | |
} | |
num *= 3; | |
buffer[num++] = rgb.g; | |
buffer[num++] = rgb.r; | |
buffer[num++] = rgb.b; | |
} | |
/** Read the progress bar value and scale to [0-1]. */ | |
private void updateRed(SeekBar seekBar, int progress) { | |
//frequency_ = Math.pow((double) progress / seekBar.getMax(), 2.0); | |
red_ = progress; | |
} | |
/** Read the progress bar value and scale to [0-1]. */ | |
private void updateGreen(SeekBar seekBar, int progress) { | |
//fadeRate_ = Math.pow((double) progress / seekBar.getMax(), 2.0); | |
green_ = progress; | |
} | |
/** Read the progress bar value and scale to [0-1]. */ | |
private void updateBlue(SeekBar seekBar, int progress) { | |
//fadeRate_ = Math.pow((double) progress / seekBar.getMax(), 2.0); | |
blue_ = progress; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment