Advertisement
vegaseat

Clean code off odd characters

Feb 28th, 2015
394
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.31 KB | None | 0 0
  1. ''' code_cleaner.py
  2. code pasted from pastebin or DaniWeb shows odd ascii characters 160 and 194
  3. on empty lines that I suspect come in from the Linux/Debian clipboard
  4. so I put the whole pasted code into a string and have
  5. this program clean the code and then write it to a .py file
  6. '''
  7.  
  8. code = """\
  9. #!/usr/bin/python
  10. ''' rp_output_test104.py
  11.  
  12. Turn 4 different LEDs on and off
  13.  
  14. The black terminal strip is located on the upper side of the Raspberry B+ board
  15. there a two rows of 20 physical pins each
  16. the upper row starts with the even pins 2 (upper left corner, ulc) to 40
  17. the lower row starts with the odd pins 1 (lower left corner, llc) to 39
  18.  
  19. We will be using the following pins:
  20. GND (pin 9)
  21. GPIO17 (pin 11)
  22. GPIO27 (pin 13)
  23. GPIO22 (pin 15)
  24. GPIO23 (pin 16)
  25. 17, 22, 23, 27 are the internal Broadcom (BCM) numbers
  26.  
  27. For this experiment I used the LED colors red, yellow, blue, green
  28.  
  29. Each of the LEDs is in series with a 220 ohm current limiting resistor
  30. connect the LED cathode (has flat spot, shorter lead) toward GND and
  31. the free end of the resistor to the proper GPIO
  32. GPIO HIGH will be about +3 volt (each LED draws about 5 mA)
  33. GPIO LOW will be 0 volt
  34.  
  35. 220 ohm --> red(2) red(2) brown(1) gold(5%) bands
  36. (you can use a 200 to 500 ohm resistor to limit the current)
  37.  
  38. Note:
  39. each GPIO is limited to no more than 15mA current draw
  40. the sum of all currents flowing through GPIOs is limited to 50mA
  41. If you want to draw more current at a higher voltage simply use
  42. an inexpensive power MOSFET that can drive a motor and also isolates
  43. the Raspberry Pi from damaging voltages.  See the MOSFET video at:
  44. https://www.youtube.com/watch?v=I1h-HZmIJ4E
  45. eg. IRF540N (100V, 30A, 0.1ohm)
  46.  
  47. To execute this code from the idle IDE start idle as root (super user)
  48. via terminal command:  sudo idle
  49.  
  50. To run from the LXTerminal -->
  51. (assume that the code is in directory rpi_python)
  52. cd rpi_python
  53. sudo python rp_output_test104.py
  54.  
  55. picture at: prntscr.com/5kbm3s
  56. code stored at: http://pastebin.com/riezGXTE
  57.  
  58. for parts used see ...
  59. http://www.canakit.com/raspberry-pi-starter-ultimate-kit.html
  60.  
  61. pastebin ...
  62. http://pastebin.com/N5mxYfVA
  63.  
  64. tested with a $35 Raspberry PI B+, Linux/Rasbian OS, Python27
  65. by  vegaseat   23jan2015
  66. '''
  67.  
  68. import RPi.GPIO as gpio
  69. import time
  70.  
  71. def output_on(nn):
  72.    # GPIOnn will go to +3.3 volt
  73.    # you can use gpio.HIGH, True, or 1
  74.    gpio.output(nn, gpio.HIGH)
  75.  
  76. def output_off(nn):
  77.    # GPIOnn goes to 0 volt
  78.    # you can use gpio.LOW, False, or 0
  79.    gpio.output(nn, gpio.LOW)
  80.  
  81.  
  82. def output_blink(nn, blinks, delay=1):
  83.    for k in range(blinks):
  84.        print("Blink {}".format(k+1))
  85.        output_on(nn)
  86.        time.sleep(delay)
  87.        output_off(nn)
  88.        time.sleep(delay)
  89.  
  90. # use Broadcom GPIO 00..nn numbers (recommended)
  91. gpio.setmode(gpio.BCM)
  92.  
  93. led_list = ['red', 'yellow', 'blue', 'green']
  94. # these are the BCM GPIO numbers we are using
  95. # the red LED is connected to GPIO17 and so on
  96. gpio_list = [17, 22, 23, 27]
  97. # set them all to output
  98. for nn in gpio_list:
  99.    gpio.setup(nn, gpio.OUT)
  100.  
  101. delay = 3
  102. for color, nn in enumerate(gpio_list):
  103.    sf = "LED {} on for {} seconds ..."
  104.    print(sf.format(led_list[color], delay))
  105.    output_on(nn)
  106.    # keep LED on for delay seconds
  107.    time.sleep(delay)
  108.    output_off(nn)
  109.    # wait 1 second
  110.    time.sleep(1)
  111.  
  112.  
  113. # let's do some blinking
  114. nn = 17
  115. blinks = 5
  116. delay = 0.5
  117. sf = "LED {} blinking {} times with {} second delay"
  118. for color, nn in enumerate(gpio_list):
  119.    print(sf.format(led_list[color], blinks, delay))
  120.    output_blink(nn, blinks, delay)
  121.  
  122. print("Real quick action ...")
  123. for k in range(20):
  124.    for nn in gpio_list:
  125.        output_on(nn)
  126.        time.sleep(0.1)
  127.        output_off(nn)
  128.        time.sleep(0.05)
  129.      
  130. print("Turn all LEDs on for 1 minute ...")
  131. for nn in gpio_list:
  132.    output_on(nn)
  133.  
  134. time.sleep(60)
  135.  
  136. # reset all GPIO channels used by this program
  137. gpio.cleanup()
  138.  
  139. #help(gpio)
  140. """
  141.  
  142. fname = "/home/pi/src/python/rp_output_test104.py"
  143.  
  144. new_code = ""
  145. for c in code:
  146.     # skip these extraneous characters
  147.     if ord(c) == 160 or ord(c) == 194:
  148.         continue
  149.     new_code += c
  150.  
  151. with open(fname, "w") as fout:
  152.     fout.write(new_code)
  153.  
  154. print("ascii characters 160 and 194 removed from code")
  155. print("corrected code saved to file {}".format(fname))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement