Skip to content

Commit

Permalink
The user directory functionality was added with support for PNG image…
Browse files Browse the repository at this point in the history
… types.

A PIXEL object was introduced.
  • Loading branch information
onebeartoe committed Mar 22, 2013
1 parent 9e6518d commit e98c4d1
Show file tree
Hide file tree
Showing 6 changed files with 324 additions and 291 deletions.
180 changes: 180 additions & 0 deletions PixelAnimationsPC/src/main/java/com/ledpixelart/hardware/Pixel.java
@@ -0,0 +1,180 @@

package com.ledpixelart.hardware;

import com.ledpixelart.pc.PixelApp;
import ioio.lib.api.RgbLedMatrix;
import ioio.lib.api.exception.ConnectionLostException;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;

/**
*
* @author rmarquez
*/
public class Pixel
{

public RgbLedMatrix matrix;

public RgbLedMatrix.Matrix KIND;

protected byte[] BitmapBytes;

protected InputStream BitmapInputStream;

protected short[] frame_;

public Pixel(RgbLedMatrix.Matrix KIND)
{
this.KIND = KIND;

BitmapBytes = new byte[KIND.width * KIND.height * 2]; //512 * 2 = 1024 or 1024 * 2 = 2048

frame_ = new short[KIND.width * KIND.height];
}

public void loadRGB565(String raw565ImagePath) throws ConnectionLostException
{

BitmapInputStream = PixelApp.class.getClassLoader().getResourceAsStream(raw565ImagePath);

try
{
int n = BitmapInputStream.read(BitmapBytes, 0, BitmapBytes.length); // reads
// the
// input
// stream
// into
// a
// byte
// array
Arrays.fill(BitmapBytes, n, BitmapBytes.length, (byte) 0);
}
catch (IOException e)
{
System.err.println("An error occured while trying to load " + raw565ImagePath + ".");
System.err.println("Make sure " + raw565ImagePath + "is included in the executable JAR.");
e.printStackTrace();
}

int y = 0;
for (int f = 0; f < frame_.length; f++)
{
frame_[f] = (short) (((short) BitmapBytes[y] & 0xFF) | (((short) BitmapBytes[y + 1] & 0xFF) << 8));
y = y + 2;
}
matrix = PixelApp.getMatrix();
matrix.frame(frame_);
}

private void loadRGB565PNG() throws ConnectionLostException
{
int y = 0;
for (int f = 0; f < frame_.length; f++)
{
frame_[f] = (short) (((short) BitmapBytes[y] & 0xFF) | (((short) BitmapBytes[y + 1] & 0xFF) << 8));
y = y + 2;
}

matrix = PixelApp.getMatrix();
matrix.frame(frame_);
}

public void writeImagetoMatrix(BufferedImage originalImage) throws ConnectionLostException
// public void writeImagetoMatrix(String imagePath) throws ConnectionLostException
{
//here we'll take a PNG, BMP, or whatever and convert it to RGB565 via a canvas, also we'll re-size the image if necessary


// try
{
// URL url = PixelApp.class.getClassLoader().getResource(imagePath);
// try
// {
// BufferedImage originalImage = ImageIO.read(url);
int width_original = originalImage.getWidth();
int height_original = originalImage.getHeight();

if (width_original != KIND.width || height_original != KIND.height)
{
//the image is not the right dimensions, ie, 32px by 32px
BufferedImage ResizedImage = new BufferedImage(KIND.width, KIND.height, originalImage.getType());
Graphics2D g = ResizedImage.createGraphics();
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.drawImage(originalImage, 0, 0, KIND.width, KIND.height, 0, 0, originalImage.getWidth(), originalImage.getHeight(), null);
g.dispose();
originalImage = ResizedImage;
}

int numByte = 0;
int i = 0;
int j = 0;

for (i = 0; i < KIND.height; i++)
{
for (j = 0; j < KIND.width; j++)
{
Color c = new Color(originalImage.getRGB(j, i)); //i and j were reversed which was rotationg the image by 90 degrees
int aRGBpix = originalImage.getRGB(j, i); //i and j were reversed which was rotationg the image by 90 degrees
int alpha;
int red = c.getRed();
int green = c.getGreen();
int blue = c.getBlue();

//RGB565
red = red >> 3;
green = green >> 2;
blue = blue >> 3;
//A pixel is represented by a 4-byte (32 bit) integer, like so:
//00000000 00000000 00000000 11111111
//^ Alpha ^Red ^Green ^Blue
//Converting to RGB565

short pixel_to_send = 0;
int pixel_to_send_int = 0;
pixel_to_send_int = (red << 11) | (green << 5) | (blue);
pixel_to_send = (short) pixel_to_send_int;

//dividing into bytes
byte byteH = (byte) ((pixel_to_send >> 8) & 0x0FF);
byte byteL = (byte) (pixel_to_send & 0x0FF);

//Writing it to array - High-byte is the first

BitmapBytes[numByte + 1] = byteH;
BitmapBytes[numByte] = byteL;
numByte += 2;
}
}
}
// catch (IOException e)
{
// TODO Auto-generated catch block
// e.printStackTrace();
}

loadRGB565PNG();
}

/**
*
* //************ this part of code writes to the LED matrix in code without any external file *********
// writeTest(); //this just writes a test pattern to the LEDs in code without using any external file, uncomment out this line if you want to see that and then comment out the next two lines
//***************************************************************************************************
*/
private void writeTest()
{
for (int i = 0; i < frame_.length; i++)
{
// frame_[i] = (short) (((short) 0x00000000 & 0xFF) | (((short) (short) 0x00000000 & 0xFF) << 8)); //all black
frame_[i] = (short) (((short) 0xFFF5FFB0 & 0xFF) | (((short) (short) 0xFFF5FFB0 & 0xFF) << 8)); //pink
//frame_[i] = (short) (((short) 0xFFFFFFFF & 0xFF) | (((short) (short) 0xFFFFFFFF & 0xFF) << 8)); //all white
}
}

}
Expand Up @@ -32,106 +32,90 @@ public AnimationsPanel(RgbLedMatrix.Matrix KIND)
super(KIND); super(KIND);
imageListPath = "/animations.text"; imageListPath = "/animations.text";


AnimateTimer = new ActionListener() { AnimateTimer = new ActionListener()
public void actionPerformed(ActionEvent evt) { {
if (!pixelFound) { //only go here if PIXEL wa found, other leave the timer public void actionPerformed(ActionEvent evt)
return; {
} if (!pixelFound)
// System.out.println("animate"); {
//only go here if PIXEL wa found, other leave the timer
i++; return;

}
if (i >= numFrames - 1) {
i = 0; i++;
}

if (i >= numFrames - 1)
// framestring = "animations/decoded/boat/boat" + i + ".rgb565"; {
String framestring = "animations/decoded/" + animation_name + "/" + animation_name + i + ".rgb565"; i = 0;
try { }
loadRGB565(framestring);
} catch (ConnectionLostException e1) { String framestring = "animations/decoded/" + animation_name + "/" + animation_name + i + ".rgb565";
// TODO Auto-generated catch block try
e1.printStackTrace(); {
} PixelApp.pixel.loadRGB565(framestring);

}
// if (i == numFrames - 1) { catch (ConnectionLostException e1)
// Animate.restart(); {
// } // TODO Auto-generated catch block
e1.printStackTrace();
} }
}
}; };
} }


@Override @Override
public void actionPerformed(ActionEvent event) public void actionPerformed(ActionEvent event)
{ {
String command = event.getActionCommand(); String command = event.getActionCommand();
System.out.println("animation comamand: " + command); System.out.println("animation comamand: " + command);


String selectedFileName = event.getActionCommand(); String selectedFileName = event.getActionCommand();
String decodedDirPath = "animations/decoded"; String decodedDirPath = "animations/decoded";


//System.out.println("selected file name: " + selectedFileName); //System.out.println("selected file name: " + selectedFileName);


InputStream decodedFile = PixelAnimationsPC.class.getClassLoader().getResourceAsStream(decodedDirPath + "/" + selectedFileName + "/" + selectedFileName + ".txt"); //decoded/rain/rain.text InputStream decodedFile = PixelAnimationsPC.class.getClassLoader().getResourceAsStream(decodedDirPath + "/" + selectedFileName + "/" + selectedFileName + ".txt"); //decoded/rain/rain.text
//note can't use file operator here as you can't reference files from a jar file //note can't use file operator here as you can't reference files from a jar file


if (decodedFile != null) if (decodedFile != null)
{ {
// ok good, now let's read it, we need to get the total numbers of frames and the frame speed // ok good, now let's read it, we need to get the total numbers of frames and the frame speed


String line = ""; String line = "";


try try
{ {
BufferedReader InputStreamReader streamReader = new InputStreamReader(decodedFile);
br = new BufferedReader( BufferedReader br = new BufferedReader(streamReader);
new InputStreamReader(decodedFile)); line = br.readLine();

}
line = br.readLine(); catch (IOException e)

{
// while ((line = br.readLine()) != null) { //You'll need to add proper error handling here
// text.append(line); }
// text.append('\n');
// } String fileAttribs = line.toString(); //now convert to a string
}
catch (IOException e) String fdelim = "[,]"; //now parse this string considering the comma split ie, 32,60
{ String[] fileAttribs2 = fileAttribs.split(fdelim);
//You'll need to add proper error handling here int selectedFileTotalFrames = Integer.parseInt(fileAttribs2[0].trim());
}

int selectedFileDelay = Integer.parseInt(fileAttribs2[1].trim());
String fileAttribs = line.toString(); //now convert to a string
//System.out.println(fileAttribs); //****** Now let's setup the animation ******
String fdelim = "[,]"; //now parse this string considering the comma split ie, 32,60 i = 0;
String [] fileAttribs2 = fileAttribs.split(fdelim); animation_name = event.getActionCommand();
int selectedFileTotalFrames = Integer.parseInt(fileAttribs2[0].trim()); numFrames = selectedFileTotalFrames;

// System.out.println("file delay: " + selectedFileDelay);
//System.out.println("total frames: " + selectedFileTotalFrames);
//System.out.println(fileAttribs2[0] + " " + fileAttribs2[1] + fileAttribs2[2]); timer = new Timer(selectedFileDelay, AnimateTimer);


int selectedFileDelay = Integer.parseInt(fileAttribs2[1].trim()); if (timer.isRunning() == true)
//selectedFileResolution = 32; {
//selectedFileResolution = Integer.parseInt(fileAttribs2[2].trim()); timer.stop();

}

timer.start();
//****** Now let's setup the animation ****** }
i = 0;
animation_name = event.getActionCommand();
numFrames = selectedFileTotalFrames;
// System.out.println("file delay: " + selectedFileDelay);

timer = new Timer(selectedFileDelay , AnimateTimer);

if (timer.isRunning() == true) {
timer.stop();
}
timer.start();
}

// System.out.println(fileAttribs);


} }


@Override @Override
Expand All @@ -141,3 +125,4 @@ protected String imagePath()
} }


} }

0 comments on commit e98c4d1

Please sign in to comment.