__init__.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. pin_values = {}
  27. def setup()
  28. def output(self,pins,value):
  29. lpin.debug("setting pin(s) {} to value {}".format(
  30. pins, value
  31. ))
  32. if type(pins) == int:
  33. pins = [pins]
  34. for p in pins:
  35. self.pin_values[p] = value
  36. def read(self,pin):
  37. return self.pin_values[pin]
  38. try:
  39. import RPi.GPIO as gpio
  40. gpio.setmode(gpio.BCM)
  41. except:
  42. # print("Using FakeGPIO because RPi.GPIO was not found")
  43. gpio = FakeGPIO()
  44. log = logging.getLogger(__name__+"")
  45. lpin = logging.getLogger(__name__+".pin")
  46. class PinBase(object):
  47. """PinBase is the base class for all classes representing a gpio pin"""
  48. pin_number = None
  49. value = None
  50. def __init__(self,pin_number):
  51. super(PinBase,self).__init__()
  52. self.pin_number = pin_number
  53. self.value = self.value_low
  54. def output(self,value):
  55. lpin.info(
  56. "pin {} set to {}".format(
  57. str(self.pin_number),
  58. str(value)
  59. )
  60. )
  61. self.value = value
  62. gpio.output(self.pin_number,value)
  63. def read(self):
  64. value = gpio.output(self.pin_number,value)
  65. lpin.debug(
  66. "pin {} has value {}".format(
  67. str(self.pin_number),
  68. str(value)
  69. )
  70. )
  71. return value
  72. class DigitalPin(PinBase):
  73. value_high = True
  74. value_low = False
  75. def __init__(self,pin_number):
  76. super(DigitalPin,self).__init__(pin_number)
  77. class AnalogPin(PinBase):
  78. value_high = 1
  79. value_low = 0
  80. def __init__(self,pin_number):
  81. super(AnalogPin,self).__init__()
  82. class PinControllerBase(object):
  83. """docstring for PinControllerBase.
  84. PinControllerBase is the base class for all classes controlling one or more physical devices connected to a gpio header
  85. """
  86. pins = []
  87. def __init__(self):
  88. super(PinControllerBase, self).__init__()
  89. def make_digital_pin(self,*args):
  90. return DigitalPin(*args)
  91. def make_analog_pin(self,*args):
  92. return AnalogPin(*args)
  93. class PinAPIBase(object):
  94. """docstring for PinAPI.
  95. PinAPIBase is the base class for all classes providing an api to multiple
  96. PinController.
  97. """
  98. controllers = []
  99. def __init__(self):
  100. super(PinAPIBase, self).__init__()
  101. class PCEngine(PinControllerBase):
  102. """Test Class"""
  103. max_speed=1
  104. speed = 0
  105. turn_on_speed = 1
  106. is_on = False
  107. def __init__(self,pin_on_off,pin_analog):
  108. super(PCEngine, self).__init__()
  109. self.pin_on_off = self.make_digital_pin(pin_on_off)
  110. self.pin_analog = self.make_digital_pin(pin_analog)
  111. self.pins.append(self.pin_on_off)
  112. self.pins.append(self.pin_analog)
  113. def set_speed(self,speed):
  114. self.pin_analog.output(speed)
  115. self.speed = speed
  116. def set_state(self,state):
  117. """state is boolean and specifies if the engine should be turned on (True) or turned off (False) """
  118. if state == self.is_on:
  119. return
  120. # self.set_speed(speed)
  121. self.pin_on_off.output(1)
  122. self.is_on = state
  123. class EnginesController(PinAPIBase):
  124. engine_left = None
  125. engine_right = None
  126. action_in_process = False
  127. def __init__(self,left,right):
  128. super(EnginesController,self).__init__()
  129. self.engine_right = self.make_engine(*right)
  130. self.engine_left = self.make_engine(*left)
  131. def __enter__(self):
  132. if self.action_in_process:
  133. lpin.debug("action already in process, not entering")
  134. return False
  135. def make_engine(self,*args):
  136. return digilib.pin.PCEngine(*args)
  137. async def turn(self,direction=None,command=None,respond=None):
  138. if not direction:
  139. respond("usage: {} <direction>")
  140. lpin.debug(threading.current_thread())
  141. # with self ad
  142. lpin.info("turning {}".format(direction))
  143. right_state = self.engine_right.is_on
  144. right_speed = self.engine_right.speed
  145. left_state = self.engine_left.is_on
  146. left_speed = self.engine_left.speed
  147. if direction == "right":
  148. self.engine_right.set_state(False)
  149. self.engine_left.set_state(True)
  150. self.engine_left.set_speed(1)
  151. elif direction == "left":
  152. self.engine_right.set_state(True)
  153. self.engine_right.set_speed(1)
  154. self.engine_left.set_state(False)
  155. await curio.sleep(2)
  156. # time.sleep(2)
  157. self.engine_right.set_state(right_state)
  158. self.engine_right.set_speed(right_speed)
  159. self.engine_left.set_state(left_state)
  160. self.engine_left.set_speed(left_speed)
  161. lpin.info("done turning {}".format(direction))
  162. # set engines to previous values
  163. class LED(DigitalPin):
  164. def __init__(self,pin):
  165. super(DigitalPin,self).__init__(pin)
  166. def on(self,command=None,respond=None):
  167. self.output(True)
  168. def off(self,command=None,respond=None):
  169. self.output(False)
  170. def set(self,state=None,command=None,respond=None):
  171. self.output(state)
  172. if __name__ == "__main__":
  173. pass
  174. #