Advertisement
Guest User

wallboxcobination

a guest
Aug 12th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.63 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. import RPi.GPIO as GPIO
  5. import time
  6. import sys, httplib
  7. import logging
  8.  
  9.  
  10. logger = logging.getLogger('jukeboxcontroller')
  11. hdlr = logging.FileHandler('/var/log/jukeboxcontroller.log')
  12. formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
  13. hdlr.setFormatter(formatter)
  14. logger.addHandler(hdlr)
  15. logger.setLevel(logging.INFO)
  16.  
  17. #contants and literals
  18. SELECTION_LETTERS=("A","B","C","D","E","F","G","H","J","K")
  19. WALLBOX=21
  20.  
  21. #>>>these constants can be changed to fit the characteristics of your wallbox
  22. MAXMIMUM_GAP=3
  23. MINIMUM_PULSE_GAP_WIDTH=0.01
  24. LETTER_NUMBER_GAP=0.22
  25.  
  26. #set up IO port for input
  27. GPIO.setmode(GPIO.BCM)
  28. GPIO.setup(WALLBOX, GPIO.IN)
  29.  
  30.  
  31. #this function tests if a pulse or gap is wide enough to be registered
  32. #this is needed for two reasons. 1) Sometimes the wallbox will generate an errant pulse
  33. #which will cause errors if interpretted as a proper contact pulse 2) because of the
  34. #way that I have tapped the wallbox pulses, there will be short gaps inside each pulse
  35. #that need to be ignored
  36.  
  37. def state_has_changed(starting_state):
  38. starting_time = time.time()
  39. elapsed_time = 0
  40.  
  41. for i in range (400):
  42. if GPIO.input(WALLBOX) != starting_state:
  43. elapsed_time = time.time() - starting_time
  44. #print ("check time recorded: %.3f" %elapsed_time)
  45. return False
  46. return True
  47.  
  48. #this function is called as soon as the main loop determines that a train of pulses
  49. #has started. It begins by counting the number pulses, then when it encounters a larger
  50. #gap, it starts incrementing the letters. If your wallbox uses the opposite order
  51. #you will need to change this. Also the final calculation of the track may need to be changed
  52. #as some boxes have additional pulses at either the start or the end of the train
  53. #once it encounters a gap over a pre-determined maxmimum we know that the rotator arm
  54. #has stopped and we calculate the track
  55.  
  56. def calculate_track():
  57.  
  58. state = True
  59. count_of_number_pulses = 1 #since we are in the first pulse
  60. count_of_letter_pulses = 0
  61. length_of_last_gap = 0
  62. first_train = True
  63. time_of_last_gap = time.time()
  64.  
  65. while length_of_last_gap < MAXMIMUM_GAP:
  66. if GPIO.input(WALLBOX) != state:
  67.  
  68. if state_has_changed(not state): # state has changed but check it is not anomaly
  69. state = not state # I use this rather than the GPIO value just in case GPIO has changed - unlikely but possible
  70. if state: #indicates we're in a new pulse
  71. length_of_last_gap = time.time() - time_of_last_gap
  72. #print ("Pulse. Last gap: %.3f" %length_of_last_gap)
  73.  
  74. if length_of_last_gap > LETTER_NUMBER_GAP: # indicates we're into the second train of pulses
  75. first_train = False
  76.  
  77. if first_train:
  78. count_of_number_pulses += 1
  79. else:
  80. count_of_letter_pulses +=1
  81. else: #indicates we're in a new gap
  82. time_of_last_gap = time.time()
  83. else:
  84. length_of_last_gap = time.time() - time_of_last_gap #update gap length and continue to poll
  85. print count_of_number_pulses
  86. print count_of_letter_pulses
  87. if count_of_number_pulses > 11 :
  88. count_of_number_pulses = count_of_number_pulses - 10
  89. count_of_letter_pulses = count_of_letter_pulses * 2
  90. else :
  91. count_of_letter_pulses = (count_of_letter_pulses * 2) - 1
  92.  
  93. track = SELECTION_LETTERS[count_of_letter_pulses-1] + str((count_of_number_pulses-1))
  94. message = ("+++ TRACK FOUND +++ Track Selection: ", track)
  95. logger.info (message)
  96. return track
  97.  
  98. def play_song(track) :
  99. print "Playing Track %s" % track
  100. global ACTIVE_PROCESS
  101. if ACTIVE_PROCESS:
  102. ACTIVE_PROCESS.terminate()
  103. ACTIVE_PROCESS = subprocess.Popen(["mpg321", "/Desktop/2/%s.mp3" % track])
  104.  
  105.  
  106.  
  107. #this is the main loop. We poll the GPIO port until there is a pulse.
  108. #sometimes there can be random pulses, or a spike when the rotor arm starts to move
  109. #so before trying to decode the pulse train I check that
  110. #the pulse is long enough to indicate a contact on the selector arm
  111.  
  112. logger.info ("starting controller")
  113. while True:
  114. if GPIO.input(WALLBOX):
  115. if state_has_changed(True):
  116. try:
  117. track = calculate_track()
  118. play_song(track)
  119. except:
  120. logger.info ("error calculating track")
  121. #else:
  122. # print ("--> Pulse ignored")nqueue&selection=%s"%track
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement