Advertisement
Guest User

POV_Generator_Py3.py

a guest
Apr 3rd, 2022
35
237 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. #
  4. ###############################################
  5. #
  6. #   POV Generator
  7. #
  8. ###############################################
  9. #
  10. #
  11. #   www.blinkenlight.net
  12. #
  13. #   Copyright 2011 Udo Klein
  14. #
  15. #   This program is free software: you can redistribute it and/or modify
  16. #   it under the terms of the GNU General Public License as published by
  17. #   the Free Software Foundation, either version 3 of the License, or
  18. #   (at your option) any later version.
  19. #
  20. #   This program is distributed in the hope that it will be useful,
  21. #   but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. #   GNU General Public License for more details.
  24. #
  25. #   You should have received a copy of the GNU General Public License
  26. #   along with this program. If not, see http://www.gnu.org/licenses/
  27.  
  28.  
  29. import sys
  30. from PIL import Image  #for installation, see: https://pillow.readthedocs.io/en/stable/installation.html
  31.  
  32. # double check that there is at least one argument (the file name)
  33. if len(sys.argv) != 2:
  34.     print("Please specify a source filename")
  35.     sys.exit(-1)
  36.  
  37. # determine source file name
  38. sourcefilename = sys.argv[1]
  39. # strip extension
  40. name = sourcefilename.split(".")[0]
  41.  
  42. # let PIL determine the proper graphics file type
  43. image = Image.open(sourcefilename)
  44.  
  45. # convert image to black and white
  46. image = image.convert("1")
  47.  
  48. # get hold of image size
  49. (xsize, ysize) = image.size
  50. # ensure height is 20 pixels (=number of LEDs)
  51. if ysize != 20:
  52.     print("Image height must be 20 pixels but is {0} pixels".format(ysize))
  53.  
  54. # output common start of program
  55. print("""#include <MsTimer2.h>
  56. #include <avr/io.h>
  57. #include <avr/pgmspace.h>
  58.  
  59. uint8_t const pov_pattern[] PROGMEM = {""")
  60.  
  61. # for each colum in the picture output 3 bytes
  62. # that will be copied into the LED output ports
  63. for x in range(0, xsize):
  64.     line = "                                  0b"
  65.     for y in range(0, ysize):
  66.         if y==6 or y==12:
  67.             line = line+", 0b"
  68.         if image.getpixel((x, ysize-1-y)) != 0:
  69.             line = line + "0"
  70.         else:
  71.             line = line + '1'
  72.  
  73. #   add a comment that makes it easier to see
  74. #   the intended meaning of the 3 bytes
  75.     line = line + (", // line {0:>3}: ".format(x+1))
  76.      
  77.     for y in range(0, ysize):
  78.         if image.getpixel((x, ysize-1-y)) != 0:
  79.             line = line + "."
  80.         else:
  81.             line = line + 'X'
  82.  
  83.     print(line)
  84.  
  85. # output common end of program
  86. print("""                                };
  87.  
  88. void blink() {
  89.    static uint16_t index = 0;
  90.  
  91.    PORTC = pgm_read_byte(pov_pattern+(index++));
  92.    PORTB = pgm_read_byte(pov_pattern+(index++));
  93.    PORTD = pgm_read_byte(pov_pattern+(index++));
  94.  
  95.    if (index >= sizeof(pov_pattern)) { index = 0; }
  96. }
  97.  
  98. void setup() {
  99.    DDRD = 0b11111111; // set digital  0- 7 to output
  100.    DDRB = 0b00111111; // set digital  8-13 to output
  101.    DDRC = 0b00111111; // set digital 14-19 to output (coincidences with analog 0-5)
  102.  
  103.    MsTimer2::set(2, blink);
  104.    MsTimer2::start();
  105. }
  106.  
  107. void loop() { }
  108. """)
  109.  
  110.  
  111.  
Advertisement
RAW Paste Data Copied
Advertisement