__init__.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #! /usr/bin/python3
  2. #
  3. ## LICENSE
  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. import logging
  19. import digilib.network
  20. log = logging.getLogger(__name__+"")
  21. lpin = logging.getLogger(__name__+".pin")
  22. client = digilib.network.Client("perry",5069,af_family="AF_INET")
  23. client.daemon = True
  24. client.start()
  25. client.connect()
  26. #
  27. # class PinBase(object):
  28. # """docstring for PinBase."""
  29. # # lockdir = "/var/lock/pins/"
  30. # lockdir = "lock/pins/"
  31. # pin_number = None
  32. # lockfile = lockdir + str(pin_number)
  33. # value_high = True
  34. # value_low = False
  35. # value = value_low
  36. # def __init__(self,pin_number):
  37. # super(PinBase,self).__init__()
  38. # self.pin_number = pin_number
  39. # self.lockfile = self.lockdir + str(self.pin_number)
  40. class PinBase(object):
  41. """PinBase is the base class for all classes representing a gpio pin"""
  42. pin_number = None
  43. value = None
  44. def __init__(self,pin_number):
  45. super(PinBase,self).__init__()
  46. self.pin_number = pin_number
  47. self.value = self.value_low
  48. def output(self,value):
  49. lpin.info(
  50. "pin {} set to {}".format(
  51. str(self.pin_number),
  52. str(value)
  53. )
  54. )
  55. self.value = value
  56. def read(self):
  57. lpin.debug(
  58. "pin {} has value {}".format(
  59. str(self.pin_number),
  60. str(self.value)
  61. )
  62. )
  63. return self.value
  64. class RemotePin(PinBase):
  65. """RemotePin represents a remote pin"""
  66. def output(self,value):
  67. lpin.info(
  68. "pin {} set to {}".format(
  69. str(self.pin_number),
  70. str(value)
  71. )
  72. )
  73. self.value = value
  74. client.send("set {} {}".format(
  75. str(self.pin_number),
  76. str(value)
  77. )
  78. )
  79. def read(self):
  80. lpin.debug(
  81. "pin {} has value {}".format(
  82. str(self.pin_number),
  83. str(self.value)
  84. )
  85. )
  86. return self.value
  87. class DigitalPin(RemotePin):
  88. value_high = True
  89. value_low = False
  90. def __init__(self,pin_number):
  91. super(DigitalPin,self).__init__(pin_number)
  92. class AnalogPin(PinBase):
  93. value_high = 1
  94. value_low = 0
  95. def __init__(self,pin_number):
  96. super(AnalogPin,self).__init__()
  97. class PinControllerBase(object):
  98. """docstring for PinControllerBase.
  99. PinControllerBase is the base class for all classes controlling a physical
  100. device connected to multiple pins
  101. """
  102. pins = []
  103. def __init__(self):
  104. super(PinControllerBase, self).__init__()
  105. class PCEngine(PinControllerBase):
  106. """Test Class"""
  107. max_speed=1
  108. speed = 0
  109. turn_on_speed = 1
  110. is_on = False
  111. def __init__(self,pin_on_off,pin_analog):
  112. super(PCEngine, self).__init__()
  113. self.pin_on_off = self.make_pin(pin_on_off)
  114. self.pin_analog = self.make_pin(pin_analog)
  115. self.pins.append(self.pin_on_off)
  116. self.pins.append(self.pin_analog)
  117. def make_pin(self,*args):
  118. return DigitalPin(*args)
  119. def set_speed(self,speed):
  120. self.pin_analog.output(speed)
  121. self.speed = speed
  122. def turn_on(self,speed=turn_on_speed):
  123. if self.is_on:
  124. return
  125. self.set_speed(speed)
  126. self.pin_on_off.output(1)
  127. self.is_on = True
  128. def turn_off(self,speed=turn_on_speed):
  129. if not self.is_on:
  130. return
  131. self.set_speed(speed)
  132. self.pin_on_off.output(1)
  133. self.is_on = True
  134. class PinAPIBase(object):
  135. """docstring for PinAPI.
  136. PinAPIBase is the base class for all classes providing an api to multiple
  137. PinController.
  138. """
  139. def __init__(self):
  140. super(PinAPIBase, self).__init__()
  141. if __name__ == "__main__":
  142. pass
  143. #