Sixaxis pair emu

Talk about anything concerning the source code.
Post Reply
superconny
Posts: 8
Joined: Wed Dec 25, 2013 10:09 pm

Sixaxis pair emu

Post by superconny »

Hello Matlo,

could you upload / push to your Github repository your source code of your final sixpair-emu firmware (https://blog.gimx.fr/sixpair-emu-customizer/)?
I have already found the source code of the python app to configure the sixpair-emu firmware and adapted it to Python 3. If everything works, I will upload the adapted code.

Merci pour le GIMX.
superconny
Posts: 8
Joined: Wed Dec 25, 2013 10:09 pm

Re: Sixaxis pair emu

Post by superconny »

I found it myself https://github.com/rafikel/diyps3contro ... ixpair-emu. I will try it on the weekend.
superconny
Posts: 8
Joined: Wed Dec 25, 2013 10:09 pm

Re: Sixaxis pair emu

Post by superconny »

Everything works fine.

After inserting the keyword const in https://github.com/rafikel/diyps3contro ... criptors.c in the beginning of line 47, 158, 227, 238, and 249, the firmware compiled under Ubuntu 20.04 (I used sudo apt-get install avr-libc binutils-avr gcc-avr avrdude to install the compilation dependencies). I have a Teensy++ 2.0 instead of a Teensy 2.0, therefore I compiled again with MCU = at90usb1286 instead of MCU = atmega32u4 (in line 63 in https://github.com/rafikel/diyps3contro ... u/makefile). It also compiled and I transfered the hex file to my Teensy++ 2.0.

The to Python 3 adapted Python app (based on https://github.com/rafikel/diyps3contro ... mu-cust.py) to configure the sixpair-emu firmware also works fine.

The links to https://github.com/rafikel/diyps3controller are state of the commit 0084afb43549a3f887cb66e34ab2ae20f821ddcd.

In your repository https://github.com/matlo/GIMX-firmwares there is not the sixpair-emu firmware. It would be amazing if you can add it.
It would also be amazing to add the adapted Python app to one of your repositories, maybe in https://github.com/matlo/GIMX-tools. Do you want to add it to one of your repos? If yes, I can upload it to the forum and you can push it if you are satisfied with the code quality (I oriented me on the PEP 8 style guide).
User avatar
Matlo
Posts: 5768
Joined: Wed Jul 06, 2011 7:01 am
Location: France
Contact:

Re: Sixaxis pair emu

Post by Matlo »

The tools can also be found here: https://github.com/matlo/diyps3controller

I am not planning to integrate them into the GIMX and GIMX-firmware projects.
GIMX creator
superconny
Posts: 8
Joined: Wed Dec 25, 2013 10:09 pm

Re: Sixaxis pair emu

Post by superconny »

I guess in this case, the forum is the best place to publish the promised adapted code.

Code: Select all

#!/usr/bin/env python3

from array import array
import struct
import sys

import tkinter
from tkinter import messagebox

import usb

if sys.platform == "win32":
    import bluetooth as _bt
elif sys.platform == "linux":
    import bluetooth._bluetooth as _bt

usb_devs = []
bd_devs = []


def read_usb_devices():
    buses = usb.busses()
    for bus in buses:
        for device in bus.devices:
            usb_devs.append((device.idVendor, device.idProduct))


def usb_set(didVendor, didProduct, idVendor, idProduct, bdaddr):
    dev = usb.core.find(idVendor=didVendor, idProduct=didProduct)
    if dev is None:
        messagebox.showerror("Device not connected", "Vendor Id: 0x%04x\nProduct Id: 0x%04x" % (didVendor, didProduct))
        return
    try:
        dev.detach_kernel_driver(0)
    except:
        pass
    try:
        read = dev.ctrl_transfer(bmRequestType=0xA1, bRequest=0x01, wValue=0xdead, data_or_wLength=10)
    except:
        messagebox.showerror("Insufficient permissions (set udev rules or execute app as root) or "
                             "device lost connection",
                             "Vendor Id: 0x%04x\nProduct Id: 0x%04x" % (didVendor, didProduct))
        return
    myArray = array('B', [idVendor & 255, idVendor // 256, idProduct & 255, idProduct // 256])
    myArray.fromlist(bdaddr)
    if myArray.tolist() != read.tolist():
        ret1 = dev.ctrl_transfer(bmRequestType=0x21, bRequest=0x09, wValue=0xdead, data_or_wLength=myArray)
        if ret1 != 10:
            messagebox.showerror("Device error", "Set Request Failed!")
        else:
            messagebox.showinfo("Device info", "Set Request Successful!\nUnplug The Device!")
    else:
        messagebox.showwarning("Device info", "Nothing Changed!")


def read_local_bdaddr(hci_sock):
    old_filter = hci_sock.getsockopt(_bt.SOL_HCI, _bt.HCI_FILTER, 14)
    flt = _bt.hci_filter_new()
    opcode = _bt.cmd_opcode_pack(_bt.OGF_INFO_PARAM, _bt.OCF_READ_BD_ADDR)
    _bt.hci_filter_set_ptype(flt, _bt.HCI_EVENT_PKT)
    _bt.hci_filter_set_event(flt, _bt.EVT_CMD_COMPLETE)
    _bt.hci_filter_set_opcode(flt, opcode)
    hci_sock.setsockopt(_bt.SOL_HCI, _bt.HCI_FILTER, flt)

    _bt.hci_send_cmd(hci_sock, _bt.OGF_INFO_PARAM, _bt.OCF_READ_BD_ADDR)

    pkt = hci_sock.recv(255)

    status, raw_bdaddr = struct.unpack("xxxxxxB6s", pkt)
    assert status == 0

    t = ["%02X" % b for b in raw_bdaddr]
    t.reverse()
    bdaddr = ":".join(t)

    # restore old filter
    hci_sock.setsockopt(_bt.SOL_HCI, _bt.HCI_FILTER, old_filter)
    return bdaddr


def read_bdaddrs():
    for dev_id in range(0, 32):
        try:
            hci_sock = _bt.hci_open_dev(dev_id)
            if hci_sock.fileno() > -1:
                bdaddr = read_local_bdaddr(hci_sock)
                bd_devs.append(bdaddr)
        except:
            pass


def refresh():
    del usb_devs[:]
    del bd_devs[:]
    read_usb_devices()
    read_bdaddrs()
    bd_devs.append("Custom bdaddr")
    liste1.delete(0, tkinter.END)
    liste1.config(height=len(usb_devs))
    for usb_dev in usb_devs:
        if (usb_dev[0] == 0x054c and usb_dev[1] == 0x0268) or (usb_dev[0] == 0x03eb and usb_dev[1] == 0x2042):
            element = "VendorId: 0x%04x - ProductId: 0x%04x" % (usb_dev[0], usb_dev[1])
            liste1.insert(tkinter.END, element)
    liste2.delete(0, tkinter.END)
    liste2.config(height=len(bd_devs))
    for bd_dev in bd_devs:
        liste2.insert(tkinter.END, bd_dev)


def apply():
    if liste1.curselection() == ():
        messagebox.showerror("Teensy++", "Please select the Teensy++ device!")
        return
    dvid = int(liste1.get(liste1.curselection())[10:16], 16)
    dpid = int(liste1.get(liste1.curselection())[30:36], 16)
    if liste2.curselection() == ():
        messagebox.showerror("Bluetooth device", "Please select the bluetooth device!")
        return
    bdaddr = liste2.get(liste2.curselection())
    if bdaddr == "Custom bdaddr":
        bdaddr = entry1.get()
        if bdaddr == "00:00:00:00:00:00":
            messagebox.showerror("Bdaddr", "Invalid bdaddr: %s" % bdaddr)
            return
    if len(bdaddr) != 17:
        messagebox.showerror("Bdaddr", "Invalid bdaddr: %s" % bdaddr)
        return
    try:
        addr = []
        addr.append(int(bdaddr[0:2], 16))
        addr.append(int(bdaddr[3:5], 16))
        addr.append(int(bdaddr[6:8], 16))
        addr.append(int(bdaddr[9:11], 16))
        addr.append(int(bdaddr[12:14], 16))
        addr.append(int(bdaddr[15:17], 16))
    except:
        messagebox.showerror("Bdaddr", "Invalid bdaddr: %s" % bdaddr)
        return
    if liste3.curselection() == ():
        messagebox.showerror("VendorId - ProductID", "Please select the VendorId - ProductId!")
        return
    vendorid = liste3.get(liste3.curselection())[0:6]
    try:
        vid = int(vendorid, 16)
    except:
        messagebox.showerror("Vendor Id", "Invalid Vendor Id: %s" % vendorid)
        return
    productid = liste3.get(liste3.curselection())[9:16]
    try:
        pid = int(productid, 16)
    except:
        messagebox.showerror("Product Id", "Invalid Product Id: %s" % productid)
        return
    if not ((vid == 0x054c and pid == 0x0268) or (vid == 0x03eb and pid == 0x2042)):
        messagebox.showerror("Product Id", "Invalid Product Id: %s" % productid)
        return
    if not messagebox.askokcancel("Confirm",
                                  "Vendor Id: %s\nProduct Id: %s\nBdaddr: %s" % (vendorid, productid, bdaddr)):
        return
    usb_set(dvid, dpid, vid, pid, addr)


if __name__ == "__main__":

    read_usb_devices()
    read_bdaddrs()

    bd_devs.append("Custom bdaddr")

    racine = tkinter.Tk()
    racine.resizable(0, 0)
    racine.title("Sixpair-emu customizer")

    frame1 = tkinter.Frame(racine, padx=10)
    frame1.pack(side=tkinter.LEFT)

    label1 = tkinter.Label(frame1, text="Teensy++ device:")
    label1.pack(side=tkinter.TOP, fill=tkinter.Y)

    liste1 = tkinter.Listbox(frame1, width=36, height=len(usb_devs), selectmode=tkinter.SINGLE,
                             exportselection=0, cursor="hand2")
    liste1.pack(side=tkinter.LEFT, fill=tkinter.Y)

    frame2 = tkinter.Frame(racine, padx=10)
    frame2.pack(side=tkinter.LEFT)

    label2 = tkinter.Label(frame2, text="Bluetooth device:")
    label2.pack(side=tkinter.TOP, fill=tkinter.Y)

    liste2 = tkinter.Listbox(frame2, width=17, height=len(bd_devs), selectmode=tkinter.SINGLE,
                             exportselection=0, cursor="hand2")
    liste2.pack(side=tkinter.LEFT, fill=tkinter.Y)

    frame3 = tkinter.Frame(racine, padx=10)
    frame3.pack(side=tkinter.LEFT)

    label3 = tkinter.Label(frame3, text="Custom bdaddr")
    label3.pack(side=tkinter.TOP)

    entry1 = tkinter.Entry(frame3, bd=5)
    entry1.pack(side=tkinter.TOP)

    label4 = tkinter.Label(frame3, text="VendorId - ProductId")
    label4.pack(side=tkinter.TOP)

    liste3 = tkinter.Listbox(frame3, width=15, height=2, selectmode=tkinter.SINGLE, exportselection=0, cursor="hand2")
    liste3.pack(side=tkinter.TOP, fill=tkinter.Y)

    liste3.insert(tkinter.END, ("0x054c - 0x0268"))
    liste3.insert(tkinter.END, ("0x03eb - 0x2042"))

    entry1.insert(0, "00:00:00:00:00:00")

    frame4 = tkinter.Frame(racine, padx=10)
    frame4.pack(side=tkinter.LEFT)

    bouton = tkinter.Button(frame4, text="Apply", command=apply)
    bouton.pack(side=tkinter.BOTTOM)

    bouton = tkinter.Button(frame4, text="Refresh", command=refresh)
    bouton.pack(side=tkinter.BOTTOM)

    for usb_dev in usb_devs:
        if (usb_dev[0] == 0x054c and usb_dev[1] == 0x0268) or (usb_dev[0] == 0x03eb and usb_dev[1] == 0x2042):
            element = "VendorId: 0x%04x - ProductId: 0x%04x" % (usb_dev[0], usb_dev[1])
            liste1.insert(tkinter.END, element)

    for bd_dev in bd_devs:
        liste2.insert(tkinter.END, bd_dev)

    racine.mainloop()
Post Reply