// Solenoid tweets
// Under CC0 1.0 Universal (CC0 1.0) - Public Domain Dedication
//
// By:
// Miguel Colom - http://mcolom.info/
// Julia Puyo - http://juliapuyo.com/

import java.io.*; 

import processing.*;
import processing.core.*;
import processing.serial.*; // serial communication library


public class Solenoid {
  private String pin;
  private boolean do_hit = false;
  private byte[] bytes_pin = null;

  public Solenoid(PApplet p_applet, String pin, boolean do_hit) {
    this.do_hit = do_hit;
    this.pin = pin;

    if (do_hit) {
      // GPIO config
      unexport_pin();
      export_pin();

      this.activate(false);
    }
  }

  private void export_pin() {
      try {
        RandomAccessFile f = new RandomAccessFile("/sys/class/gpio/export", "rw");
        f.writeByte(this.bytes_pin[0]);
        if (this.bytes_pin.length > 1)
          f.writeByte(this.bytes_pin[1]);
        f.close();
      }
      catch (FileNotFoundException e) {
        System.out.println("FileNotFoundException: " + e);
      }
      catch (IOException e) {
        System.out.println("IOException: " + e);
      }
  }

  private void unexport_pin() {
      try {
        RandomAccessFile f = new RandomAccessFile("/sys/class/gpio/unexport", "rw");
        f.writeByte(this.bytes_pin[0]);
        if (this.bytes_pin.length > 1)
          f.writeByte(this.bytes_pin[1]);
        f.close();
      }
      catch (FileNotFoundException e) {
        System.out.println("FileNotFoundException: " + e);
      }
      catch (IOException e) {
        System.out.println("IOException: " + e);
      }
  }

  public void delay() {
    try {
      Thread.sleep(200); // ms
    }
    catch (InterruptedException e) {
      System.out.println("solenoid delay: " + e);
    }
  }

  private void activate(boolean status) {    
    // Open file and write '0' or '1' to control solenoid device using the GPIO pin
    try {
      RandomAccessFile pin_file = new RandomAccessFile("/sys/class/gpio/gpio" + this.pin + "/value", "rw");
      if (status)
        pin_file.writeByte((byte)'1');
      else
        pin_file.writeByte((byte)'0');
      pin_file.close();
    }
    catch (FileNotFoundException e) {
      System.out.println("FileNotFoundException: " + e);
    }
    catch (IOException e) {
      System.out.println("IOException: " + e);
    }
  }

  public void hit() {
    if (this.do_hit) {
      System.out.println("Solenoid hit!");

      this.activate(true);
      this.delay();
      this.activate(false);
    } else {
      System.out.println("Solenoid simulated hit!");
    }
  }
  
  // Free sources before finishing
  public void finalize() throws IOException {
    // Unexport pin so it can be used by others
    if (do_hit)
	  this.unexport_pin();
  }
}


@@ -181,16 +181,36 @@
 a.io.*; 
+%0Aimport java.util.*;
 %0A%0Aimport
@@ -406,14 +406,12 @@
 ate 
-byte%5B%5D
+List
  byt
@@ -424,11 +424,28 @@
  = n
-ull
+ew ArrayList%3CByte%3E()
 ;%0A%0A 
@@ -556,16 +556,159 @@
 = pin;%0A%0A
+    this.bytes_pin.add(new Byte((byte)(pin.charAt(0))));%0A    if (pin.length() %3E 1)%0A      this.bytes_pin.add(new Byte((byte)(pin.charAt(1))));%0A%0A
     if (
@@ -949,36 +949,49 @@
 %22);%0A        
-f.write
+%0A        byte b0 = (
 Byte
-(
+)
 this.bytes_p
@@ -984,35 +984,38 @@
 e)this.bytes_pin
-%5B0%5D
+.get(0
 );%0A        if (t
@@ -1013,39 +1013,111 @@
     
-if (this.bytes_pin.length %3E 1)%0A
+f.writeByte(b0);%0A%0A        if (this.bytes_pin.size() %3E 1) %7B%0A%09%09%09byte b1 = (Byte)this.bytes_pin.get(1);%0A  
     
@@ -1134,35 +1134,27 @@
 iteByte(
-this.bytes_pin%5B1%5D);
+b1);%0A%09    %7D
 %0A       
@@ -1501,24 +1501,25 @@
  %22rw%22);%0A
+%0A
         
 f.writeB
@@ -1506,36 +1506,40 @@
 );%0A%0A        
-f.write
+byte b0 = (
 Byte
-(
+)
 this.bytes_p
@@ -1544,14 +1544,51 @@
 _pin
-%5B0%5D
+.get(0);%0A        f.writeByte(b0
 );%0A
+        %0A
     
@@ -1614,20 +1614,66 @@
 pin.
-length %3E 1)%0A
+size() %3E 1) %7B%0A%09%09%09byte b1 = (Byte)this.bytes_pin.get(1);%0A  
     
@@ -1694,27 +1694,20 @@
 yte(
-this.bytes_pin%5B1%5D);
+b1);%0A%09    %7D%0A
 %0A   

convert bytes_pin into a ArrayList<Byte>