__init__.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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 blinker
  24. import curio
  25. import digilib.network
  26. import digilib.misc
  27. log = logging.getLogger(__name__+"")
  28. lgpio = logging.getLogger(__name__+".gpio")
  29. # Error messages
  30. ERROR_TAKES_ARGUMENTS = "{} takes {} {} argument(s): {}"
  31. # respond(ERROR_TAKES_ARGUMENTS.format(
  32. # command, "one", "positional", "<name>"))
  33. _pins_for_cleanup = set()
  34. _gpio = None
  35. class GPIOWrapper(object):
  36. gpio = None
  37. OUT = "out"
  38. IN = "in"
  39. # BCM = "broadcom"
  40. pin_values = {}
  41. def __init__(self):
  42. super(GPIOWrapper,self).__init__()
  43. self.load_rpi_gpio()
  44. lgpio.debug("setting pin numbering to broadcom")
  45. if self.gpio:
  46. self.gpio.setmode(self.gpio.BCM)
  47. sig_exit = blinker.signal("global-exit")
  48. sig_exit.connect(self.cleanup)
  49. def cleanup(self,*args):
  50. lgpio.debug("cleanup! ({})".format(args))
  51. if self.gpio:
  52. self.gpio.cleanup(_pins_for_cleanup)
  53. def load_rpi_gpio(self):
  54. try:
  55. self.gpio = False
  56. self.gpio = __import__("RPi.GPIO",fromlist="GPIO")
  57. except ImportError as e:
  58. lgpio.debug("failed to import RPi.GPIO")
  59. print("134")
  60. except Exception as e:
  61. lgpio.debug("unknown error occured", exc_info=e)
  62. print("137")
  63. finally:
  64. self.OUT = getattr(self.gpio,"OUT",self.OUT)
  65. self.IN = getattr(self.gpio,"IN",self.IN)
  66. # self.BCM = getattr(self.gpio,self.BCM)
  67. def output(self,pins,state,*args):
  68. lgpio.debug("setting pin(s) {} to value {}".format(
  69. pins,state))
  70. if type(pins) is int:
  71. pins = [pins]
  72. _pins_for_cleanup.update(pins)
  73. if self.gpio:
  74. self.gpio.output(pins,state)
  75. else:
  76. lgpio.debug("no gpio module")
  77. lgpio.debug("gpio module: {}".format(self.gpio))
  78. def input(self,pins,*args):
  79. values = []
  80. for p in pins:
  81. if self.gpio:
  82. values.append(self.gpio.input(p))
  83. else:
  84. values.append(-1)
  85. lgpio.debug("reading pins {}: {}".format(
  86. pins,values))
  87. return values
  88. def setup(self,pins,value,*args):
  89. lgpio.debug("setting pin(s) {} to {}".format(pins,value))
  90. if self.gpio:
  91. self.gpio.setup(pins,value)
  92. _gpio = GPIOWrapper()
  93. class PinBase(object):
  94. """PinBase is the base class for all classes representing a gpio pin"""
  95. pin_number = None
  96. value = None
  97. def __init__(self,pin_number,mode):
  98. super(PinBase,self).__init__()
  99. self.pin_number = pin_number
  100. self.value = self.value_low
  101. _gpio.setup(self.pin_number,_gpio.OUT)
  102. def output(self,value):
  103. [value] = digilib.misc.parse_to_int_list(value)
  104. self.value = value
  105. _gpio.output(self.pin_number,value)
  106. def input(self):
  107. value = _gpio.input(self.pin_number,value)
  108. return value
  109. class DigitalPin(PinBase):
  110. value_high = True
  111. value_low = False
  112. def __init__(self,pin_number,mode):
  113. super(DigitalPin,self).__init__(pin_number,mode)
  114. class AnalogPin(PinBase):
  115. value_high = 1
  116. value_low = 0
  117. def __init__(self,pin_number):
  118. super(AnalogPin,self).__init__(pin_number)
  119. class PinControllerBase(object):
  120. """docstring for PinControllerBase.
  121. PinControllerBase is the base class for all classes controlling one or more physical devices connected to a gpio header
  122. """
  123. pins = []
  124. def __init__(self,pins):
  125. super(PinControllerBase, self).__init__()
  126. self.pins.extend(pins)
  127. # def make_digital_pin(self,*args):
  128. # return DigitalPin(*args)
  129. # def make_analog_pin(self,*args):
  130. # return AnalogPin(*args)
  131. class PinAPIBase(object):
  132. """docstring for PinAPI.
  133. PinAPIBase is the base class for all classes providing an api to multiple
  134. PinController.
  135. """
  136. controllers = []
  137. def __init__(self):
  138. super(PinAPIBase, self).__init__()
  139. class PCEngine(PinControllerBase):
  140. """Test Class"""
  141. max_speed=1
  142. speed = 0
  143. turn_on_speed = 1
  144. is_on = False
  145. def __init__(self,pin_on_off,pin_analog):
  146. super(PCEngine, self).__init__([pin_on_off,pin_analog])
  147. # self.pins.append(pin_on_off)
  148. # self.pins.append(pin_analog)
  149. self.pin_on_off = DigitalPin(pin_on_off,_gpio.OUT)
  150. self.pin_analog = DigitalPin(pin_analog,_gpio.OUT)
  151. # self.pins.append(self.pin_on_off)
  152. # self.pins.append(self.pin_analog)
  153. # gpio.setup(self.pins,_gpio.OUT)
  154. # gpio.setup(self.pin_analog,_gpio.OUT)
  155. self.set_speed(1)
  156. self.set_state(1)
  157. def set_speed(self,speed):
  158. self.pin_analog.output(speed)
  159. self.speed = speed
  160. def set_state(self,state):
  161. """state is integer and specifies if the engine should be turned on (1) or turned off (0) """
  162. if state == self.is_on:
  163. return
  164. # self.set_speed(speed)
  165. self.pin_on_off.output(state)
  166. self.is_on = state
  167. class EnginesController(PinAPIBase):
  168. engine_left = None
  169. engine_right = None
  170. action_in_process = False
  171. def __init__(self,left,right):
  172. super(EnginesController,self).__init__()
  173. self.engine_right = self.make_engine(*right)
  174. self.engine_left = self.make_engine(*left)
  175. def make_engine(self,*args):
  176. return digilib.pin.PCEngine(*args)
  177. def set_engine_state(self,args=[],command=None,respond=None):
  178. if len(args) != 2:
  179. respond("missing argument(s): <engine> <state>")
  180. return
  181. [engine,state] = args
  182. if engine == "right":
  183. self.engine_right.set_state(state)
  184. elif engine == "left":
  185. self.engine_left.set_state(state)
  186. else:
  187. respond("unknown engine. use 'left' or 'right'")
  188. def set_engine_speed(self,args=[],command=None,respond=None):
  189. if len(args) != 2:
  190. respond("missing argument(s): <engine> <speed>")
  191. return
  192. [engine,speed] = args
  193. if engine == "right":
  194. self.engine_right.set_speed(speed)
  195. elif engine == "left":
  196. self.engine_left.set_speed(speed)
  197. else:
  198. respond("unknown engine. use 'left' or 'right'")
  199. async def turn(self,args=[],command=None,respond=None):
  200. if len(args) != 1:
  201. respond("one missing argument: direction")
  202. return
  203. [direction] = args
  204. # lgpio.debug(threading.current_thread())
  205. # with self ad
  206. lgpio.info("turning {}".format(direction))
  207. right_state = self.engine_right.is_on
  208. right_speed = self.engine_right.speed
  209. left_state = self.engine_left.is_on
  210. left_speed = self.engine_left.speed
  211. if direction == "right":
  212. self.engine_right.set_state(False)
  213. self.engine_left.set_state(True)
  214. self.engine_left.set_speed(1)
  215. elif direction == "left":
  216. self.engine_right.set_state(True)
  217. self.engine_right.set_speed(1)
  218. self.engine_left.set_state(False)
  219. await curio.sleep(2)
  220. # time.sleep(2)
  221. self.engine_right.set_state(right_state)
  222. self.engine_right.set_speed(right_speed)
  223. self.engine_left.set_state(left_state)
  224. self.engine_left.set_speed(left_speed)
  225. lgpio.info("done turning {}".format(direction))
  226. # set engines to previous values
  227. class LED(DigitalPin):
  228. def __init__(self,pin):
  229. super(DigitalPin,self).__init__(pin,_gpio.OUT)
  230. self.on()
  231. def on(self,args=[],command=None,respond=None):
  232. self.output(True)
  233. def off(self,args=[],command=None,respond=None):
  234. self.output(False)
  235. def set(self,args=[],command=None,respond=None):
  236. if len(args) != 1:
  237. respond("one missing argument: state")
  238. return
  239. [state] = args
  240. self.output(state)
  241. class StatusLED(PinControllerBase):
  242. def __init__(self,pin_red,pin_green):
  243. super(StatusLED,self).__init__([pin_red,pin_green])
  244. self.pin_red = DigitalPin(pin_red,_gpio.OUT)
  245. self.pin_green = DigitalPin(pin_green,_gpio.OUT)
  246. self.green()
  247. def red(self,args=[],command=None,respond=None):
  248. if len(args) > 1:
  249. respond(ERROR_TAKES_ARGUMENTS.format(
  250. command,"one","optional","<state>"))
  251. return
  252. elif len(args) == 1:
  253. state = digilib.misc.parse_to_int_list(*args)
  254. else:
  255. state = 1
  256. self.pin_red.output(state)
  257. self.pin_green.output(int(not state))
  258. def green(self,args=[],command=None,respond=None):
  259. if len(args) > 1:
  260. respond(ERROR_TAKES_ARGUMENTS.format(
  261. command,"one","optional","<state>"))
  262. return
  263. elif len(args) == 1:
  264. state = int(*args)
  265. else:
  266. state = 1
  267. self.pin_green.output(state)
  268. self.pin_red.output(int(not state))
  269. class DebugPinController(PinControllerBase):
  270. def output(self,args=[],command=None,respond=None):
  271. if len(args) != 2:
  272. respond(ERROR_TAKES_ARGUMENTS.format(
  273. command, "two", "positional", "<name>"))
  274. return False
  275. pins = digilib.misc.parse_to_int_list(args[0])
  276. [state] = digilib.misc.parse_to_int_list(args[1])
  277. _gpio.output(pins,state)
  278. def input(self,args=[],command=None,respond=None):
  279. if len(args) != 2:
  280. respond(ERROR_TAKES_ARGUMENTS.format(
  281. command, "two", "positional", "<name>"))
  282. return False
  283. pins = digilib.misc.parse_to_int_list(args[0])
  284. [state] = digilib.misc.parse_to_int_list(args[1])
  285. rv = _gpio.input(pins,state)
  286. lgpio.debug(rv)
  287. respond(str(rv))
  288. def raise_exc(self,args=[],command=None,respond=None):
  289. raise Exception("Test Exception")
  290. async def araise_exc(self,args=[],command=None,respond=None):
  291. state = digilib.misc.parse_to_int_list("1,2,3,4")
  292. a = 1+2
  293. raise Exception("Test Async Exception")
  294. if __name__ == "__main__":
  295. pass
  296. #