Guest User

Untitled

a guest
Jan 6th, 2026
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.98 KB | None | 0 0
  1. import threading
  2. import common.Api_pb2 as hudiy_api
  3. from common.Client import Client, ClientEventHandler
  4. import time
  5. import can
  6.  
  7. bus = can.Bus(interface='socketcan',
  8. channel='can0',
  9. receive_own_messages=True)
  10. bus.set_filters([{"can_id": 0x5C1, "can_mask": 0xFFF, "extended": False}])
  11.  
  12. values = []
  13. for i in range(15):
  14. values.append(i+1)
  15. values = values + values + values
  16. print(values)
  17. clicks = [7,6,5,4,3,2,1]
  18.  
  19. vol_value = None
  20. ctrl_value = None
  21.  
  22. class ActionTrigger:
  23. def __init__(self, client):
  24. """
  25. Initialize with a client that can send messages.
  26. """
  27. self.client = client
  28.  
  29. def trigger_action(self, action: str):
  30. """
  31. Send the specified action through the client.
  32. """
  33. dispatch_action = hudiy_api.DispatchAction()
  34. dispatch_action.action = action
  35.  
  36. # MESSAGE_DISPATCH_ACTION is a constant defined in hudiy_api
  37. self.client.send(
  38. hudiy_api.MESSAGE_DISPATCH_ACTION,
  39. 0,
  40. dispatch_action.SerializeToString()
  41. )
  42.  
  43. def volume_up(self):
  44. """Trigger volume up."""
  45. self.trigger_action("output_volume_up")
  46.  
  47. def volume_down(self):
  48. """Trigger volume down."""
  49. self.trigger_action("output_volume_down")
  50.  
  51. def toggle_mute(self):
  52. """Toggle output mute."""
  53. self.trigger_action("toggle_output_muted")
  54.  
  55. def listen_for_canbus_events(client):
  56. while True:
  57.  
  58. for msg in bus:
  59. #print(msg)
  60. if msg.arbitration_id == 1473:
  61.  
  62. if len(msg.data) > 0:
  63. if msg.data[0] == 19: #volume
  64. new = int(msg.data[2])
  65. print(new)
  66. if vol_value == None:
  67. vol_value = new
  68. newvalues = values[new+7:new+22]
  69. print(newvalues)
  70. valuepos = newvalues.index(vol_value)
  71. if valuepos > 7:
  72. #print(valuepos)
  73. for i in range(valuepos - 7):
  74. print('-')
  75. trigger.volume_down()
  76. if valuepos < 7:
  77. for i in range(clicks[valuepos]):
  78. print('+')
  79. trigger.volume_up()
  80. vol_value = new
  81. print("")
  82.  
  83. if msg.data[0] == 20: #control
  84. new = int(msg.data[2])
  85. print(new)
  86. if ctrl_value == None:
  87. ctrl_value = new
  88. newvalues = values[new+7:new+22]
  89. print(newvalues)
  90. valuepos = newvalues.index(ctrl_value)
  91. if valuepos > 7:
  92. #print(valuepos)
  93. for i in range(valuepos - 7):
  94. print('-')
  95.  
  96. if valuepos < 7:
  97. for i in range(clicks[valuepos]):
  98. print('+')
  99.  
  100. ctrl_value = new
  101. print("")
  102.  
  103. if msg.data[0] == 43: # vol click
  104. print("")
  105.  
  106. if msg.data[0] == 40: # ctrl click
  107. print("")
  108. if msg.data[0] == 1: # cycle
  109. print("")
  110. if msg.data[0] == 2: # >
  111. print("")
  112. if msg.data[0] == 3: # <
  113. print("")
  114. if msg.data[0] == 41: # return
  115. print("")
  116. if msg.arbitration_id == 849: #reverse
  117. if len(msg.data) > 0:
  118. if msg.data[0] == 2:
  119. GPIO.output(relay, GPIO.HIGH)
  120. if msg.data[0] == 0:
  121. GPIO.output(relay, GPIO.LOW)
  122.  
  123.  
  124. class EventHandler(ClientEventHandler):
  125.  
  126. def on_hello_response(self, client, message):
  127. print(
  128. "received hello response, result: {}, app version: {}.{}, api version: {}.{}"
  129. .format(message.result, message.app_version.major,
  130. message.app_version.minor, message.api_version.major,
  131. message.api_version.minor))
  132.  
  133. threading.Thread(target=listen_for_canbus_events, args=(client, )).start()
  134.  
  135.  
  136. def main():
  137. client = Client("canbus")
  138. event_handler = EventHandler()
  139. client.set_event_handler(event_handler)
  140. client.connect('127.0.0.1', 44405)
  141.  
  142. active = True
  143. while active:
  144. try:
  145. active = client.wait_for_message()
  146. except KeyboardInterrupt:
  147. break
  148.  
  149. client.disconnect()
  150.  
  151.  
  152. if __name__ == "__main__":
  153. main()
Advertisement
Add Comment
Please, Sign In to add comment