-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmqtt_joy_pub.py
60 lines (49 loc) · 1.52 KB
/
mqtt_joy_pub.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!usr/bin/env python
# -*- coding: utf-8 -*-
import paho.mqtt.client as mqtt
import pygame
from pygame.locals import *
import os
import time
# ssh接続だとディスプレイが開けないので
# DummyVideoDriverを使用する
os.environ["SDL_VIDEODRIVER"] = "dummy"
def on_connect(client, userdata, flag, rc):
print("Connected with result code " + str(rc))
def on_disconnect(client, userdata, flag, rc):
if rc != 0:
print("Unexpected disconnection.")
def on_publish(client, userdata, mid):
print("publish: {0}".format(mid))
def joy_demo():
client = mqtt.Client()
client.on_connect = on_connect
client.on_disconnect = on_disconnect
client.on_publish = on_publish
client.connect('192.168.0.86', 1884, 60)
client.loop_start()
pygame.init()
pygame.joystick.init()
joys = pygame.joystick.Joystick(0)
joys.init()
joystick = pygame.joystick.Joystick(0)
joystick.init()
x = 0
y = 0
while True:
for e in pygame.event.get():
if e.type == pygame.locals.JOYAXISMOTION:
x = int(joystick.get_axis(0)*300)
y = int(joystick.get_axis(1)*300)
elif e.type == pygame.locals.JOYBUTTONDOWN:
print('buttin: ' + str(e.button) + ' pushed')
elif e.type == pygame.locals.JOYBUTTONUP:
print('button: ' + str(e.button) + ' released')
client.publish("Joy/x", x)
client.publish("Joy/y", y)
time.sleep(0.1)
if __name__ == '__main__':
try:
joy_demo()
except KeyboardInterrupt:
print("終了")