summaryrefslogtreecommitdiffhomepage
path: root/proto-iokit-foohid/foohidjoystick.h
blob: e987c229fdd9b81d43fe37944fcbe37d0560699e (plain)
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
/* Copyright (c) 2017, Eike Ziller                                               *
 *                                                                               *
 * Permission to use, copy, modify, and/or distribute this software for any      *
 * purpose with or without fee is hereby granted, provided that the above        *
 * copyright notice and this permission notice appear in all copies.             *
 */

#pragma once

#include <QByteArray>
#include <QString>
#include <QCoreApplication>

#include <IOKit/IOKitLib.h>

struct JoystickValues {
    uint8_t x;
    uint8_t y;
    uint8_t z;
    uint8_t rx;
    uint8_t ry;
    uint8_t rz;
};

class FooHIDJoystick
{
    Q_DECLARE_TR_FUNCTIONS(FooHIDJoystick)
public:
    FooHIDJoystick(const QByteArray &name, const QByteArray &serialNumber);
    ~FooHIDJoystick();

    bool hasError() const;
    QString errorMessage() const;

    int minValue() const { return 0; }
    int range() const { return 255; }

    void setValue(JoystickValues newValues);

private:
    bool createDevice() const;
    bool sendToDevice() const;
    void destroyDevice() const;

    const QByteArray name;
    const QByteArray serialNumber;
    io_connect_t connection = 0;
    JoystickValues values = {127, 127};
    QString _errorMessage;
    bool _hasError = true;
    bool connectionOpened = false;
    bool deviceCreated = false;
};