Advertisement
Guest User

Programme2

a guest
Apr 25th, 2015
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.03 KB | None | 0 0
  1. //////////////////////////////////////////////////////////////////////////
  2. //Filenames: GIF_Converter.pde
  3. //Original Authors: Robert Tu
  4. //Date Created: February 5, 2014
  5. //Notes:
  6. //Modifications: Didier BRIAND -
  7. /*
  8.  
  9. This is Processing sketch that converts animated GIF files into an Arduino
  10. sketch file that you can then download into the Night & Day Jack.
  11.  
  12. Only animated GIF files are accepted, although GIF files with one animation
  13. frame can be converted. GIF dimensions MUST be 8x6 to show properly
  14. on MeU.
  15.  
  16.  
  17. */
  18.  
  19. //////////////////////////////////////////////////////////////////////////
  20.  
  21.  
  22. //import libraries
  23.  
  24. //library can be found here:
  25. //http://www.extrapixel.ch/processing/gifAnimation/
  26. import gifAnimation.*;
  27.  
  28. //library can be found here:
  29. //http://www.sojamo.de/libraries/controlP5/
  30. import controlP5.*;
  31.  
  32. //File Handler variables
  33. PrintWriter output;
  34. PrintWriter LoopFileOutput;
  35.  
  36. //store the GIF animation frames in this array
  37. PImage[] animation;
  38. int FrameNumber;
  39.  
  40. //File names and text status variables
  41. String GIFPath="None Chosen";
  42. String OutputPath = "None Chosen";
  43. String Status = " ";
  44.  
  45. //variable to access ControlP5 class
  46. ControlP5 cp5;
  47.  
  48. //****************************
  49. //Change GIF dimensions here:
  50.  
  51. final int HEIGHT = 6;
  52. final int WIDTH = 8;
  53. //****************************
  54.  
  55.  
  56. void setup() {
  57.  
  58. //set up the screen size and layout the buttons
  59.  
  60. size(550, 650);
  61. cp5 = new ControlP5(this);
  62.  
  63.  
  64. cp5.addButton("GIFSelect")
  65. .setSize(130, 35)
  66. .setPosition(43, 180)
  67. .setLabel("Select a GIF");
  68.  
  69.  
  70.  
  71. cp5.addTextfield("ArduinoName")
  72. .setLabel("Arduino Sketch File Name")
  73. .setPosition(43, 269)
  74. .setSize(300, 30);
  75.  
  76.  
  77. cp5.addButton("OutputSelect")
  78. .setLabel("Select Directory")
  79. .setPosition(43, 347)
  80. .setSize(130, 35);
  81.  
  82.  
  83.  
  84. cp5.addButton("Run")
  85. .setPosition(43, 440)
  86. .setSize(130, 35);
  87.  
  88.  
  89. cp5.addButton("Exit")
  90. .setPosition(43, 550)
  91. .setSize(130, 35);
  92.  
  93. }
  94.  
  95. void draw() {
  96. background(10, 10, 100);
  97. drawText();
  98.  
  99. }
  100.  
  101. void drawText() {
  102.  
  103. textSize(16);
  104. text("Olympiades de SI", 43, 30);
  105. textSize(12);
  106.  
  107. text("Night & Day Jacket Animation Conversion Program", 43, 50);
  108. text("- Only 8x6 gif files will work with the led panel", 43, 75);
  109. text("- After program runs, an Arduino sketch will be prodcued", 43, 90);
  110. text("- Download the sketch using the Arduino IDE", 43, 105);
  111. text("- In Arduino IDE, choose micro", 43, 120);
  112. // text("(5V, 16MHz) w/ ATmega328", 53, 135);
  113.  
  114. text("1. Select a GIF File", 43, 160);
  115. textSize(8);
  116. text(GIFPath, 43, 227);
  117. textSize(12);
  118. text("2. Choose a file name for the Arduino sketch (no spaces or special char)", 43,255);
  119. text("3. Select a directory to store the Arduino Sketch", 43, 335);
  120. textSize(8);
  121. text(OutputPath, 43, 392);
  122. textSize(12);
  123. text("4. Only run if steps 1 to 3 have been completed", 43, 425);
  124. textSize(13);
  125.  
  126. text(Status, 43, 490);
  127. }
  128.  
  129. //Button Functions
  130.  
  131. public void GIFSelect() {
  132. selectInput("Select a GIF file: ", "GIFProcess");
  133.  
  134. }
  135.  
  136. void GIFProcess(File selection) {
  137.  
  138. if (selection == null) {
  139. GIFPath = "None Chosen";
  140. } else {
  141. GIFPath = selection.getAbsolutePath();
  142. }
  143. }
  144.  
  145. public void OutputSelect() {
  146. selectFolder("Select an output Folder: ", "OutputProcess");
  147. }
  148.  
  149. void OutputProcess(File selection) {
  150. if (selection == null) {
  151. OutputPath = "None Chosen";
  152. } else {
  153. OutputPath = selection.getAbsolutePath();
  154. }
  155.  
  156. }
  157.  
  158. public void Run() {
  159.  
  160.  
  161. String ArduinoFileName = cp5.get(Textfield.class,"ArduinoName").getText();
  162. String thePattern = "[^A-Za-z0-9]+";
  163. String [] m = match(ArduinoFileName, thePattern);
  164.  
  165. println("Found: " + m);
  166. if (m != null) {
  167. Status = "NOT A VALID ARDUINO NAME";
  168. } else {
  169. try {
  170. Status = "Running";
  171.  
  172. //resize screen to fit 8x6 gifs so processing can read into array
  173. //and translate into Arduino sketch
  174. size(8,6);
  175. animation = Gif.getPImages(this, GIFPath);
  176. try {
  177.  
  178. output = createWriter(OutputPath+"/"+ArduinoFileName+"/"+ArduinoFileName + ".ino");
  179. WriteHeader();
  180.  
  181. for (int i = 0; i < animation.length; i++) {
  182. WriteFile(animation[i], i, ArduinoFileName);
  183. }
  184.  
  185. WriteBody();
  186. WriteLoopFile(animation.length, ArduinoFileName);
  187.  
  188. output.flush();
  189. output.close();
  190.  
  191. //retore the screen size
  192. size(550, 650);
  193. Status = "File Conversion Complete";
  194. } catch (Exception e) {
  195.  
  196. //restore the screen size
  197. size(550, 650);
  198. e.printStackTrace();
  199. Status = "THIS IS NOT A VALID GIF!";
  200. GIFPath = "Choose new file";
  201. }
  202.  
  203. } catch (Exception e) {
  204. size(550, 650);
  205. Status = "NOT A VALID GIF FILE!";
  206. GIFPath = "Choose new file";
  207. }
  208. }
  209.  
  210. }
  211.  
  212. void Exit() {
  213. exit();
  214. }
  215.  
  216. // Functions to Write Arduino Sketch
  217.  
  218. void WriteBody() {
  219. output.println("SimpleTimer AnimateTimer;");
  220. output.println("byte FrameNumber = 0;");
  221. output.println("void setup() {");
  222. output.println(" matrix.begin();");
  223. output.println(" matrix.setBrightness(50);");
  224. output.println(" matrix.fillScreen(0);");
  225. output.println(" matrix.show();");
  226. output.println(" AnimateTimer.setInterval(100, TimerEvent);");
  227. output.println("}");
  228. output.println("void loop() {");
  229. output.println(" AnimateTimer.run();");
  230. output.println(" matrix.show();");
  231. output.println("}");
  232. output.println("uint16_t drawRGB24toRGB565(byte r, byte g, byte b) {");
  233. output.println(" return ((r / 8) << 11) | ((g / 4) << 5) | (b / 8);");
  234. output.println("}");
  235.  
  236.  
  237. }
  238. void WriteHeader() {
  239. output.println("#include <avr/pgmspace.h>");
  240. output.println("#include <Adafruit_GFX.h>");
  241. output.println("#include <Adafruit_NeoMatrix.h>");
  242. output.println("#include <Adafruit_NeoPixel.h>");
  243. output.println("#include <SimpleTimer.h>");
  244. output.println("#ifndef PSTR");
  245. output.println(" #define PSTR"); // Make Arduino Due happy
  246. output.println("#endif");
  247.  
  248. output.println("#define PIN 6");
  249.  
  250. 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);");
  251.  
  252.  
  253.  
  254. }
  255. void WriteLoopFile(int NumberOfFrames, String VarName) {
  256. int NextFrame;
  257. output.println("void TimerEvent() {");
  258.  
  259. for (int i = 0; i < NumberOfFrames; i++) {
  260. if (i == 0) output.println(" if (FrameNumber == " + i + ") {");
  261. else output.println(" else if (FrameNumber == " + i + ") {");
  262.  
  263. output.println(" for (byte y = 0; y < "+HEIGHT+"; y++) {");
  264. output.println(" for (byte x = 0; x < "+WIDTH+"; x++) {");
  265. output.println(" byte loc = x + y*"+WIDTH+";");
  266. 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]))));");
  267. output.println(" }");
  268. output.println(" }");
  269.  
  270. if (i == NumberOfFrames - 1) output.println(" FrameNumber = 0;");
  271. else {
  272. NextFrame = i + 1;
  273. output.println(" FrameNumber = " + NextFrame + ";");
  274. }
  275. output.println(" }");
  276. }
  277.  
  278. output.println("}");
  279. }
  280.  
  281. void WriteFile(PImage img, int FrameNumber, String VarName) {
  282. //img = loadImage(ImageFileName); ["+WIDTH+"*"+HEIGHT+"]
  283. img.loadPixels();
  284. output.print("const unsigned char " + VarName + "RedFrame" + FrameNumber + "[] PROGMEM = ");
  285. output.print("{");
  286. for (int y = 0; y < height; y++) {
  287. for (int x = 0; x < width; x++) {
  288. int loc = x + y*width;
  289.  
  290. // The functions red(), green(), and blue() pull out the 3 color components from a pixel.
  291. int r = int(red(img.pixels[loc]));
  292. if (loc < 255) output.print(r+",");
  293. else output.print(r);
  294.  
  295. }
  296. }
  297.  
  298. output.println("};");
  299. output.print("const unsigned char " + VarName + "GreenFrame" + FrameNumber + "[] PROGMEM = ");
  300. output.print("{");
  301. for (int y = 0; y < height; y++) {
  302. for (int x = 0; x < width; x++) {
  303. int loc = x + y*width;
  304.  
  305. // The functions red(), green(), and blue() pull out the 3 color components from a pixel.
  306. int g = int(green(img.pixels[loc]));
  307. if (loc < 255) output.print(g+",");
  308. else output.print(g);
  309.  
  310. }
  311. }
  312.  
  313. output.println("};");
  314.  
  315. output.print("const unsigned char " + VarName + "BlueFrame" + FrameNumber + "[] PROGMEM = ");
  316. output.print("{");
  317. for (int y = 0; y < height; y++) {
  318. for (int x = 0; x < width; x++) {
  319. int loc = x + y*width;
  320.  
  321. // The functions red(), green(), and blue() pull out the 3 color components from a pixel.
  322. int b = int(blue(img.pixels[loc]));
  323. if (loc < 255) output.print(b+",");
  324. else output.print(b);
  325.  
  326. }
  327. }
  328.  
  329. output.println("};");
  330.  
  331.  
  332. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement