A repository for MicroPython code that communicates with and relays information from the Texas Instruments TMP117/119 temperature sensor.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

34 lines
983 B

2 months ago
  1. class TopicMessage():
  2. "Object containing both a topic and message string attribute. Can convert itself to a string representation"
  3. def __init__(self, topic:str = "", message:str = ""):
  4. self.topic = topic
  5. self.message = message
  6. def to_string(self) -> str:
  7. return f"{self.topic}: {self.message}"
  8. class TMQueue():
  9. """Simple queue (FIFO) of TopicMessage objects"""
  10. def __init__(self):
  11. self.data: list[TopicMessage] = list()
  12. def get(self):
  13. if not self.is_empty():
  14. item = self.data.pop(0)
  15. return item
  16. else:
  17. raise IndexError
  18. def add(self, item):
  19. self.data.append(item)
  20. def is_empty(self):
  21. if len(self.data) == 0:
  22. return True
  23. else:
  24. return False
  25. def string_to_topicmessage(s: str):
  26. """Convert string to a TopicMessage object"""
  27. topic, message = str.split(":", maxsplit = 1)
  28. return TopicMessage(topic, message)