LedHandler.java 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package control;
  2. import java.io.BufferedReader;
  3. import java.io.BufferedWriter;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.io.OutputStreamWriter;
  7. public class LedHandler {
  8. private BufferedReader inp;
  9. private BufferedWriter out;
  10. private Process p;
  11. public LedHandler(){
  12. try {
  13. p = Runtime.getRuntime().exec("sudo python led.py");
  14. inp = new BufferedReader( new InputStreamReader(p.getInputStream()) );
  15. out = new BufferedWriter( new OutputStreamWriter(p.getOutputStream()) );
  16. //setLed(15, 100, 100, 100);
  17. //strobo();
  18. }
  19. catch (Exception err) {
  20. err.printStackTrace();
  21. }
  22. }
  23. public void close(){
  24. try {
  25. inp.close();
  26. } catch (IOException e) {
  27. e.printStackTrace();
  28. }
  29. try {
  30. out.close();
  31. } catch (IOException e) {
  32. e.printStackTrace();
  33. }
  34. p.destroy();
  35. }
  36. /**
  37. * Set the color of a specific led
  38. * @param Pixel number of the led
  39. * @param Red value
  40. * @param Green value
  41. * @param Blue value
  42. * Remember: Button leds are GRB, normal ledstrip is RGB
  43. */
  44. public void setLed(int led, int r, int g, int b) {
  45. if(led<0)
  46. return;
  47. try {
  48. System.out.println("Set the led with " + r);
  49. out.write( "1|" + led + "|" + r + "|" + g + "|" + b + "\n" );
  50. out.flush();
  51. }
  52. catch (Exception err) {
  53. err.printStackTrace();
  54. }
  55. }
  56. /**
  57. * Shows the (new) colors
  58. */
  59. public void show(){
  60. try {
  61. out.write( "0\n" );
  62. out.flush();
  63. }
  64. catch (Exception err) {
  65. err.printStackTrace();
  66. }
  67. }
  68. public void strobo(){
  69. boolean on = true;
  70. while(true){
  71. if(on){
  72. for(int i = 1; i < 66; i++){
  73. setLed(i, 0, 0, 0);
  74. }
  75. }else{
  76. for(int i = 1; i < 66; i++){
  77. setLed(i, 255, 255, 255);
  78. }
  79. }
  80. on = !on;
  81. show();
  82. try {
  83. Thread.sleep(100);
  84. } catch (InterruptedException e) {
  85. e.printStackTrace();
  86. }
  87. }
  88. }
  89. }