Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import threading
- import common.Api_pb2 as hudiy_api
- from common.Client import Client, ClientEventHandler
- import time
- import can
- bus = can.Bus(interface='socketcan',
- channel='can0',
- receive_own_messages=True)
- bus.set_filters([{"can_id": 0x5C1, "can_mask": 0xFFF, "extended": False}])
- values = []
- for i in range(15):
- values.append(i+1)
- values = values + values + values
- print(values)
- clicks = [7,6,5,4,3,2,1]
- vol_value = None
- ctrl_value = None
- class ActionTrigger:
- def __init__(self, client):
- """
- Initialize with a client that can send messages.
- """
- self.client = client
- def trigger_action(self, action: str):
- """
- Send the specified action through the client.
- """
- dispatch_action = hudiy_api.DispatchAction()
- dispatch_action.action = action
- # MESSAGE_DISPATCH_ACTION is a constant defined in hudiy_api
- self.client.send(
- hudiy_api.MESSAGE_DISPATCH_ACTION,
- 0,
- dispatch_action.SerializeToString()
- )
- def volume_up(self):
- """Trigger volume up."""
- self.trigger_action("output_volume_up")
- def volume_down(self):
- """Trigger volume down."""
- self.trigger_action("output_volume_down")
- def toggle_mute(self):
- """Toggle output mute."""
- self.trigger_action("toggle_output_muted")
- def listen_for_canbus_events(client):
- while True:
- for msg in bus:
- #print(msg)
- if msg.arbitration_id == 1473:
- if len(msg.data) > 0:
- if msg.data[0] == 19: #volume
- new = int(msg.data[2])
- print(new)
- if vol_value == None:
- vol_value = new
- newvalues = values[new+7:new+22]
- print(newvalues)
- valuepos = newvalues.index(vol_value)
- if valuepos > 7:
- #print(valuepos)
- for i in range(valuepos - 7):
- print('-')
- trigger.volume_down()
- if valuepos < 7:
- for i in range(clicks[valuepos]):
- print('+')
- trigger.volume_up()
- vol_value = new
- print("")
- if msg.data[0] == 20: #control
- new = int(msg.data[2])
- print(new)
- if ctrl_value == None:
- ctrl_value = new
- newvalues = values[new+7:new+22]
- print(newvalues)
- valuepos = newvalues.index(ctrl_value)
- if valuepos > 7:
- #print(valuepos)
- for i in range(valuepos - 7):
- print('-')
- if valuepos < 7:
- for i in range(clicks[valuepos]):
- print('+')
- ctrl_value = new
- print("")
- if msg.data[0] == 43: # vol click
- print("")
- if msg.data[0] == 40: # ctrl click
- print("")
- if msg.data[0] == 1: # cycle
- print("")
- if msg.data[0] == 2: # >
- print("")
- if msg.data[0] == 3: # <
- print("")
- if msg.data[0] == 41: # return
- print("")
- if msg.arbitration_id == 849: #reverse
- if len(msg.data) > 0:
- if msg.data[0] == 2:
- GPIO.output(relay, GPIO.HIGH)
- if msg.data[0] == 0:
- GPIO.output(relay, GPIO.LOW)
- class EventHandler(ClientEventHandler):
- def on_hello_response(self, client, message):
- print(
- "received hello response, result: {}, app version: {}.{}, api version: {}.{}"
- .format(message.result, message.app_version.major,
- message.app_version.minor, message.api_version.major,
- message.api_version.minor))
- threading.Thread(target=listen_for_canbus_events, args=(client, )).start()
- def main():
- client = Client("canbus")
- event_handler = EventHandler()
- client.set_event_handler(event_handler)
- client.connect('127.0.0.1', 44405)
- active = True
- while active:
- try:
- active = client.wait_for_message()
- except KeyboardInterrupt:
- break
- client.disconnect()
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment