Skip to content

Instantly share code, notes, and snippets.

@Staars
Created January 3, 2024 14:10
Show Gist options
  • Save Staars/102ac2f909b0456f3c4d954ec7f4c16f to your computer and use it in GitHub Desktop.
Save Staars/102ac2f909b0456f3c4d954ec7f4c16f to your computer and use it in GitHub Desktop.
Daydream Controller Berry
# Simple Berry driver for the Google Daydream controller
import BLE
class BLE_DAYD : Driver
var buf
var connecting, connected, new_position
var time,seq,xO,yO,zO,xA,yA,zA,xG,yG,zG,xT,yT,button
def init(MAC,addr_type)
var cbp = tasmota.gen_cb(/e,o,u,h->self.cb(e,o,u,h))
self.buf = bytes(-256)
BLE.conn_cb(cbp,self.buf)
BLE.set_MAC(bytes(MAC),addr_type)
print("BLE: will try to connect to Daydream with MAC:",MAC)
self.connect()
tasmota.add_fast_loop(/-> BLE.loop()) # needed for mouse position
end
def connect()
self.connecting = true
self.connected = false
self.new_position = false
BLE.set_svc("FE55")
BLE.set_chr("00000001-1000-1000-8000-00805F9B34FB")
BLE.run(3) # subscribe
end
def every_second()
if (self.connecting == false && self.connected == false)
print("BLE: try to reconnect Daydream")
self.connect()
else
print("-----------------------------")
print(self.time,self.seq,self.button)
print(self.xO,self.yO,self.zO)
print(self.xA,self.yA,self.zA)
print(self.xG,self.yG,self.zG)
print(self.xT,self.yT)
end
end
def every_50ms()
import mqtt
if self.new_position == true
mqtt.publish("tele/daydream",format('{"mouse":{"x":%s,"y":%s}}',self.xT,self.yT))
self.new_position = false
end
end
def handle_DD_notification(h)
#print(self.buf[1..self.buf[0]],h)
self.handle_dd_data()
end
def handle_dd_data()
# http://stackoverflow.com/questions/40730809/use-daydream-controller-on-holol
var data = self.buf[1..]
self.time = ((data[0] & 0xFF) << 1 | (data[1] & 0x80) >> 7 )
self.seq = (data[2] & 0x7C) >> 2
self.button = data[18] & 31
var t
t = (data[1] & 0x03) << 11 | (data[2] & 0xFF) << 3 | (data[3] & 0xE0) >> 5
self.xO = (t << 19) >> 19
t = (data[3] & 0x1F) << 8 | (data[4] & 0xFF)
self.yO = (t << 19) >> 19
t = (data[5] & 0xFF) << 5 | (data[6] & 0xF8) >> 3
self.zO = (t << 19) >> 19
t = (data[6] & 0x07) << 10 | (data[7] & 0xFF) << 2 | (data[8] & 0xC0) >> 6
self.xA = (t << 19) >> 19
t = (data[8] & 0x3F) << 7 | (data[9] & 0xFE) >> 1
self.yA = (t << 19) >> 19
t = (data[9] & 0x01) << 12 | (data[10] & 0xFF) << 4 | (data[11] & 0xF0) >> 4
self.zA = (t << 19) >> 19
t = ((data[11] & 0x0F) << 9 | (data[12] & 0xFF) << 1 | (data[13] & 0x80) >> 7)
self.xG = (t << 19) >> 19
t = ((data[13] & 0x7F) << 6 | (data[14] & 0xFC) >> 2 )
self.yG = (t << 19) >> 19
t = ((data[14] & 0x03) << 11 | (data[15] & 0xFF) << 3 | (data[16] & 0xE0) >> 5)
self.zG = (t << 19) >> 19
self.xT = ((data[16] & 0x1F) << 3 | (data[17] & 0xE0) >> 5)
self.yT = ((data[17] & 0x1F) << 3 | (data[18] & 0xE0) >> 5)
# var x = self.buf.getbits(22,12)
# if x > 2048
# x -= 4096
# end
end
def cb(error,op,uuid,handle)
if error == 0
if op == 1 # read OP
# print(op,uuid)
elif op == 3
self.connecting = false
self.connected = true
print("BLE: init completed for Daydream")
elif op == 5
self.connected = false
self.connecting = false
print("BLE: did disconnect Daydream ... will try to reconnect")
elif op == 103 # notification OP
if self.connected == false return end
self.handle_DD_notification(handle)
end
else
print("BLE: error:",error)
if self.connecting == true
print("BLE: init sequence failed ... try to repeat")
self.connecting = false
end
end
end
end
ble_daydream = BLE_DAYD("333333008D48",0) # controller MAC and address type
tasmota.add_driver(ble_daydream)
#-
11:00:35.060 Retrieve services and characteristics for BLE device wit MAC:333333008D48
11:00:35.071 ___________________________________________________________________________
11:00:35.508 __________________________
11:00:35.509 Service: UUID: 1801
11:00:35.509 Characteristics:
11:00:35.656 UUID: 2A05 , handle: 0x0003 , ['Indicate']
11:00:35.657 __________________________
11:00:35.658 Service: UUID: FFF1
11:00:35.659 Characteristics:
11:00:35.806 UUID: FFE0 , handle: 0x0007 , ['Notify']
11:00:35.808 UUID: FFE1 , handle: 0x000a , ['WriteNoResp', 'Write']
11:00:35.812 UUID: FFE2 , handle: 0x000c , ['Read']
11:00:35.823 __________________________
11:00:35.823 Service: UUID: FE55
11:00:35.824 Characteristics:
11:00:35.957 UUID: 00000001-1000-1000-8000-00805F9B34FB , handle: 0x000f , ['Notify']
11:00:35.960 UUID: 00000002-1000-1000-8000-00805F9B34FB , handle: 0x0012 , ['WriteNoResp', 'Write']
11:00:35.973 UUID: 00000003-1000-1000-8000-00805F9B34FB , handle: 0x0014 , ['Read']
11:00:35.974 __________________________
11:00:35.985 Service: UUID: 180A
11:00:35.985 Characteristics:
11:00:36.106 UUID: 2A29 , handle: 0x0017 , ['Read']
11:00:36.108 UUID: 2A24 , handle: 0x0019 , ['Read']
11:00:36.109 UUID: 2A25 , handle: 0x001b , ['Read']
11:00:36.121 UUID: 2A27 , handle: 0x001d , ['Read']
11:00:36.123 UUID: 2A26 , handle: 0x001f , ['Read']
11:00:36.125 UUID: 2A28 , handle: 0x0021 , ['Read']
11:00:36.137 UUID: 2A50 , handle: 0x0023 , ['Read']
11:00:36.138 __________________________
11:00:36.139 Service: UUID: 180F
11:00:36.150 Characteristics:
11:00:36.257 UUID: 2A19 , handle: 0x0026 , ['Read', 'Notify']
11:00:36.258 __________________________
11:00:36.259 Service: UUID: 4169726F-6861-4446-5553-657276696365
11:00:36.271 Characteristics:
11:00:36.357 UUID: 4169726F-6861-4446-5543-6F6D6D616E64 , handle: 0x002a , ['WriteNoResp', 'Write']
11:00:36.361 UUID: 4169726F-6861-4446-5543-6D6452657370 , handle: 0x002c , ['Notify']
11:00:36.373 _______________________________________________________________
11:00:36.374 Got all services and characteristics of connected BLE device!
11:00:36.386 ###############################################################
11:00:36.387 BLE: disconnect device
11:00:36.455 BLE: did disconnect
11:02:07.276 BLE: Connection Ctx created
-#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment