Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //////////////////////////////////////////////////////////////////////////
- //Filenames: GIF_Converter.pde
- //Original Authors: Robert Tu
- //Date Created: February 5, 2014
- //Notes:
- //Modifications: Didier BRIAND -
- /*
- This is Processing sketch that converts animated GIF files into an Arduino
- sketch file that you can then download into the Night & Day Jack.
- Only animated GIF files are accepted, although GIF files with one animation
- frame can be converted. GIF dimensions MUST be 8x6 to show properly
- on MeU.
- */
- //////////////////////////////////////////////////////////////////////////
- //import libraries
- //library can be found here:
- //http://www.extrapixel.ch/processing/gifAnimation/
- import gifAnimation.*;
- //library can be found here:
- //http://www.sojamo.de/libraries/controlP5/
- import controlP5.*;
- //File Handler variables
- PrintWriter output;
- PrintWriter LoopFileOutput;
- //store the GIF animation frames in this array
- PImage[] animation;
- int FrameNumber;
- //File names and text status variables
- String GIFPath="None Chosen";
- String OutputPath = "None Chosen";
- String Status = " ";
- //variable to access ControlP5 class
- ControlP5 cp5;
- //****************************
- //Change GIF dimensions here:
- final int HEIGHT = 6;
- final int WIDTH = 8;
- //****************************
- void setup() {
- //set up the screen size and layout the buttons
- size(550, 650);
- cp5 = new ControlP5(this);
- cp5.addButton("GIFSelect")
- .setSize(130, 35)
- .setPosition(43, 180)
- .setLabel("Select a GIF");
- cp5.addTextfield("ArduinoName")
- .setLabel("Arduino Sketch File Name")
- .setPosition(43, 269)
- .setSize(300, 30);
- cp5.addButton("OutputSelect")
- .setLabel("Select Directory")
- .setPosition(43, 347)
- .setSize(130, 35);
- cp5.addButton("Run")
- .setPosition(43, 440)
- .setSize(130, 35);
- cp5.addButton("Exit")
- .setPosition(43, 550)
- .setSize(130, 35);
- }
- void draw() {
- background(10, 10, 100);
- drawText();
- }
- void drawText() {
- textSize(16);
- text("Olympiades de SI", 43, 30);
- textSize(12);
- text("Night & Day Jacket Animation Conversion Program", 43, 50);
- text("- Only 8x6 gif files will work with the led panel", 43, 75);
- text("- After program runs, an Arduino sketch will be prodcued", 43, 90);
- text("- Download the sketch using the Arduino IDE", 43, 105);
- text("- In Arduino IDE, choose micro", 43, 120);
- // text("(5V, 16MHz) w/ ATmega328", 53, 135);
- text("1. Select a GIF File", 43, 160);
- textSize(8);
- text(GIFPath, 43, 227);
- textSize(12);
- text("2. Choose a file name for the Arduino sketch (no spaces or special char)", 43,255);
- text("3. Select a directory to store the Arduino Sketch", 43, 335);
- textSize(8);
- text(OutputPath, 43, 392);
- textSize(12);
- text("4. Only run if steps 1 to 3 have been completed", 43, 425);
- textSize(13);
- text(Status, 43, 490);
- }
- //Button Functions
- public void GIFSelect() {
- selectInput("Select a GIF file: ", "GIFProcess");
- }
- void GIFProcess(File selection) {
- if (selection == null) {
- GIFPath = "None Chosen";
- } else {
- GIFPath = selection.getAbsolutePath();
- }
- }
- public void OutputSelect() {
- selectFolder("Select an output Folder: ", "OutputProcess");
- }
- void OutputProcess(File selection) {
- if (selection == null) {
- OutputPath = "None Chosen";
- } else {
- OutputPath = selection.getAbsolutePath();
- }
- }
- public void Run() {
- String ArduinoFileName = cp5.get(Textfield.class,"ArduinoName").getText();
- String thePattern = "[^A-Za-z0-9]+";
- String [] m = match(ArduinoFileName, thePattern);
- println("Found: " + m);
- if (m != null) {
- Status = "NOT A VALID ARDUINO NAME";
- } else {
- try {
- Status = "Running";
- //resize screen to fit 8x6 gifs so processing can read into array
- //and translate into Arduino sketch
- size(8,6);
- animation = Gif.getPImages(this, GIFPath);
- try {
- output = createWriter(OutputPath+"/"+ArduinoFileName+"/"+ArduinoFileName + ".ino");
- WriteHeader();
- for (int i = 0; i < animation.length; i++) {
- WriteFile(animation[i], i, ArduinoFileName);
- }
- WriteBody();
- WriteLoopFile(animation.length, ArduinoFileName);
- output.flush();
- output.close();
- //retore the screen size
- size(550, 650);
- Status = "File Conversion Complete";
- } catch (Exception e) {
- //restore the screen size
- size(550, 650);
- e.printStackTrace();
- Status = "THIS IS NOT A VALID GIF!";
- GIFPath = "Choose new file";
- }
- } catch (Exception e) {
- size(550, 650);
- Status = "NOT A VALID GIF FILE!";
- GIFPath = "Choose new file";
- }
- }
- }
- void Exit() {
- exit();
- }
- // Functions to Write Arduino Sketch
- void WriteBody() {
- output.println("SimpleTimer AnimateTimer;");
- output.println("byte FrameNumber = 0;");
- output.println("void setup() {");
- output.println(" matrix.begin();");
- output.println(" matrix.setBrightness(50);");
- output.println(" matrix.fillScreen(0);");
- output.println(" matrix.show();");
- output.println(" AnimateTimer.setInterval(100, TimerEvent);");
- output.println("}");
- output.println("void loop() {");
- output.println(" AnimateTimer.run();");
- output.println(" matrix.show();");
- output.println("}");
- output.println("uint16_t drawRGB24toRGB565(byte r, byte g, byte b) {");
- output.println(" return ((r / 8) << 11) | ((g / 4) << 5) | (b / 8);");
- output.println("}");
- }
- void WriteHeader() {
- output.println("#include <avr/pgmspace.h>");
- output.println("#include <Adafruit_GFX.h>");
- output.println("#include <Adafruit_NeoMatrix.h>");
- output.println("#include <Adafruit_NeoPixel.h>");
- output.println("#include <SimpleTimer.h>");
- output.println("#ifndef PSTR");
- output.println(" #define PSTR"); // Make Arduino Due happy
- output.println("#endif");
- output.println("#define PIN 6");
- output.println("Adafruit_NeoMatrix matrix = Adafruit_NeoMatrix("+WIDTH+","+HEIGHT+", PIN, NEO_MATRIX_TOP + NEO_MATRIX_LEFT + NEO_MATRIX_ROWS + NEO_MATRIX_ZIGZAG, NEO_GRB + NEO_KHZ800);");
- }
- void WriteLoopFile(int NumberOfFrames, String VarName) {
- int NextFrame;
- output.println("void TimerEvent() {");
- for (int i = 0; i < NumberOfFrames; i++) {
- if (i == 0) output.println(" if (FrameNumber == " + i + ") {");
- else output.println(" else if (FrameNumber == " + i + ") {");
- output.println(" for (byte y = 0; y < "+HEIGHT+"; y++) {");
- output.println(" for (byte x = 0; x < "+WIDTH+"; x++) {");
- output.println(" byte loc = x + y*"+WIDTH+";");
- output.println(" matrix.drawPixel(x, y, drawRGB24toRGB565(pgm_read_byte(&("+ VarName + "RedFrame" + i + "[loc])), pgm_read_byte(&(" + VarName + "GreenFrame" + i + "[loc])), pgm_read_byte(&(" + VarName + "BlueFrame" + i + "[loc]))));");
- output.println(" }");
- output.println(" }");
- if (i == NumberOfFrames - 1) output.println(" FrameNumber = 0;");
- else {
- NextFrame = i + 1;
- output.println(" FrameNumber = " + NextFrame + ";");
- }
- output.println(" }");
- }
- output.println("}");
- }
- void WriteFile(PImage img, int FrameNumber, String VarName) {
- //img = loadImage(ImageFileName); ["+WIDTH+"*"+HEIGHT+"]
- img.loadPixels();
- output.print("const unsigned char " + VarName + "RedFrame" + FrameNumber + "[] PROGMEM = ");
- output.print("{");
- for (int y = 0; y < height; y++) {
- for (int x = 0; x < width; x++) {
- int loc = x + y*width;
- // The functions red(), green(), and blue() pull out the 3 color components from a pixel.
- int r = int(red(img.pixels[loc]));
- if (loc < 255) output.print(r+",");
- else output.print(r);
- }
- }
- output.println("};");
- output.print("const unsigned char " + VarName + "GreenFrame" + FrameNumber + "[] PROGMEM = ");
- output.print("{");
- for (int y = 0; y < height; y++) {
- for (int x = 0; x < width; x++) {
- int loc = x + y*width;
- // The functions red(), green(), and blue() pull out the 3 color components from a pixel.
- int g = int(green(img.pixels[loc]));
- if (loc < 255) output.print(g+",");
- else output.print(g);
- }
- }
- output.println("};");
- output.print("const unsigned char " + VarName + "BlueFrame" + FrameNumber + "[] PROGMEM = ");
- output.print("{");
- for (int y = 0; y < height; y++) {
- for (int x = 0; x < width; x++) {
- int loc = x + y*width;
- // The functions red(), green(), and blue() pull out the 3 color components from a pixel.
- int b = int(blue(img.pixels[loc]));
- if (loc < 255) output.print(b+",");
- else output.print(b);
- }
- }
- output.println("};");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement