__init__.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. #!/usr/bin/env python3.5
  2. # Copyright 2017 Digital
  3. #
  4. # This file is part of DigiLib.
  5. #
  6. # DigiLib is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # DigiLib is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with DigiLib. If not, see <http://www.gnu.org/licenses/>.
  18. # Python modules
  19. import logging
  20. import threading
  21. import traceback
  22. # Third party modules
  23. import curio
  24. import digilib.network
  25. class FakeGPIO(object):
  26. OUT = "out"
  27. IN = "in"
  28. BCM = "broadcom"
  29. pin_values = {}
  30. def setup(self,pins,value):
  31. lpin.debug("pin(s) {} set to {}".format(pins,value))
  32. def setupmode(self,mode):
  33. lpin.debug("mode set to {}".format(mode))
  34. def output(self,pins,value):
  35. lpin.debug("setting pin(s) {} to value {}".format(
  36. pins, value
  37. ))
  38. if type(pins) == int:
  39. pins = [pins]
  40. for p in pins:
  41. self.pin_values[p] = value
  42. def read(self,pin):
  43. return self.pin_values[pin]
  44. try:
  45. import RPi.GPIO as gpio
  46. except:
  47. # print("Using FakeGPIO because RPi.GPIO was not found")
  48. gpio = FakeGPIO()
  49. gpio.setmode(gpio.BCM)
  50. log = logging.getLogger(__name__+"")
  51. lpin = logging.getLogger(__name__+".pin")
  52. class PinBase(object):
  53. """PinBase is the base class for all classes representing a gpio pin"""
  54. pin_number = None
  55. value = None
  56. def __init__(self,pin_number):
  57. super(PinBase,self).__init__()
  58. self.pin_number = pin_number
  59. self.value = self.value_low
  60. def output(self,value):
  61. lpin.info(
  62. "pin {} set to {}".format(
  63. str(self.pin_number),
  64. str(value)
  65. )
  66. )
  67. self.value = value
  68. gpio.output(self.pin_number,value)
  69. def read(self):
  70. value = gpio.output(self.pin_number,value)
  71. lpin.debug(
  72. "pin {} has value {}".format(
  73. str(self.pin_number),
  74. str(value)
  75. )
  76. )
  77. return value
  78. class DigitalPin(PinBase):
  79. value_high = True
  80. value_low = False
  81. def __init__(self,pin_number):
  82. super(DigitalPin,self).__init__(pin_number)
  83. class AnalogPin(PinBase):
  84. value_high = 1
  85. value_low = 0
  86. def __init__(self,pin_number):
  87. super(AnalogPin,self).__init__()
  88. class PinControllerBase(object):
  89. """docstring for PinControllerBase.
  90. PinControllerBase is the base class for all classes controlling one or more physical devices connected to a gpio header
  91. """
  92. pins = []
  93. def __init__(self):
  94. super(PinControllerBase, self).__init__()
  95. # def make_digital_pin(self,*args):
  96. # return DigitalPin(*args)
  97. # def make_analog_pin(self,*args):
  98. # return AnalogPin(*args)
  99. class PinAPIBase(object):
  100. """docstring for PinAPI.
  101. PinAPIBase is the base class for all classes providing an api to multiple
  102. PinController.
  103. """
  104. controllers = []
  105. def __init__(self):
  106. super(PinAPIBase, self).__init__()
  107. class PCEngine(PinControllerBase):
  108. """Test Class"""
  109. max_speed=1
  110. speed = 0
  111. turn_on_speed = 1
  112. is_on = False
  113. def __init__(self,pin_on_off,pin_analog):
  114. super(PCEngine, self).__init__()
  115. self.pins.append(pin_on_off)
  116. self.pins.append(pin_analog)
  117. self.pin_on_off = DigitalPin(pin_on_off)
  118. self.pin_analog = DigitalPin(pin_analog)
  119. # self.pins.append(self.pin_on_off)
  120. # self.pins.append(self.pin_analog)
  121. gpio.setup(self.pins,gpio.OUT)
  122. # gpio.setup(self.pin_analog,gpio.OUT)
  123. self.set_speed(1)
  124. self.set_state(1)
  125. def set_speed(self,speed):
  126. self.pin_analog.output(speed)
  127. self.speed = speed
  128. def set_state(self,state):
  129. """state is boolean and specifies if the engine should be turned on (True) or turned off (False) """
  130. if state == self.is_on:
  131. return
  132. # self.set_speed(speed)
  133. self.pin_on_off.output(1)
  134. self.is_on = state
  135. class EnginesController(PinAPIBase):
  136. engine_left = None
  137. engine_right = None
  138. action_in_process = False
  139. def __init__(self,left,right):
  140. super(EnginesController,self).__init__()
  141. self.engine_right = self.make_engine(*right)
  142. self.engine_left = self.make_engine(*left)
  143. def __enter__(self):
  144. if self.action_in_process:
  145. lpin.debug("action already in process, not entering")
  146. return False
  147. def make_engine(self,*args):
  148. return digilib.pin.PCEngine(*args)
  149. async def turn(self,args=[],command=None,respond=None):
  150. if len(args) != 1:
  151. respond("one missing argument: direction")
  152. return
  153. [direction] = args
  154. # lpin.debug(threading.current_thread())
  155. # with self ad
  156. lpin.info("turning {}".format(direction))
  157. right_state = self.engine_right.is_on
  158. right_speed = self.engine_right.speed
  159. left_state = self.engine_left.is_on
  160. left_speed = self.engine_left.speed
  161. if direction == "right":
  162. self.engine_right.set_state(False)
  163. self.engine_left.set_state(True)
  164. self.engine_left.set_speed(1)
  165. elif direction == "left":
  166. self.engine_right.set_state(True)
  167. self.engine_right.set_speed(1)
  168. self.engine_left.set_state(False)
  169. await curio.sleep(2)
  170. # time.sleep(2)
  171. self.engine_right.set_state(right_state)
  172. self.engine_right.set_speed(right_speed)
  173. self.engine_left.set_state(left_state)
  174. self.engine_left.set_speed(left_speed)
  175. lpin.info("done turning {}".format(direction))
  176. # set engines to previous values
  177. class LED(DigitalPin):
  178. def __init__(self,pin):
  179. super(DigitalPin,self).__init__(pin)
  180. gpio.setup(pin,gpio.OUT)
  181. self.on()
  182. def on(self,args=[],command=None,respond=None):
  183. self.output(True)
  184. def off(self,args=[],command=None,respond=None):
  185. self.output(False)
  186. def set(self,args=[],command=None,respond=None):
  187. if len(args) != 1:
  188. respond("one missing argument: state")
  189. return
  190. [state] = args
  191. self.output(state)
  192. if __name__ == "__main__":
  193. pass
  194. #