Skip to content

Commit

Permalink
Merge pull request #12 from donarturo11/implement-two-byte-raw
Browse files Browse the repository at this point in the history
Add support for 2-byte raw wave unsigned
  • Loading branch information
donarturo11 committed Apr 10, 2024
2 parents d477395 + faf80c9 commit fdef9c4
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 22 deletions.
29 changes: 23 additions & 6 deletions src/GUI/SettingsWidget/DeviceConfiguration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ DeviceConfiguration::DeviceConfiguration(QWidget *parent) :
connect(ui->devices_cb, SIGNAL(currentIndexChanged(int)),
this, SLOT(chooseDevice(int)));
connect(ui->baudrates_cb, SIGNAL(currentIndexChanged(int)),
this, SLOT(chooseBaudrate(int)));
this, SLOT(chooseBaudRate(int)));
connect(ui->types_cb, SIGNAL(currentIndexChanged(int)),
this, SLOT(chooseType(int)));
connect(tg, SIGNAL(statusChanged(ThinkGearStatus)),
this, SLOT(onThinkGearStatusChanged(ThinkGearStatus)));

initBaudRates();
setupDeviceTypes();
refresh();

}
Expand All @@ -33,9 +34,11 @@ DeviceConfiguration::~DeviceConfiguration()
delete ui;
}

void DeviceConfiguration::initBaudRates()
void DeviceConfiguration::setupBaudRates()
{
for (auto bRate : ThinkgearBaudrates) {
int idx = ui->types_cb->currentIndex();
ui->baudrates_cb->clear();
for (auto bRate : TGTypes[idx].baudrates) {
ui->baudrates_cb->addItem(QString::number(bRate), bRate);
}
}
Expand All @@ -46,7 +49,7 @@ void DeviceConfiguration::chooseDevice(int id)
tg->setPortName(portName);
}

void DeviceConfiguration::chooseBaudrate(int id)
void DeviceConfiguration::chooseBaudRate(int id)
{
int bRate = ui->baudrates_cb->currentData().toInt();
tg->setBaudRate(bRate);
Expand All @@ -64,5 +67,19 @@ void DeviceConfiguration::refresh()
}
}

void DeviceConfiguration::setupDeviceTypes()
{
for (auto dev : TGTypes) {
auto devName = QString(dev.name.c_str());
ui->types_cb->addItem(devName, devName);
}
}

void DeviceConfiguration::onThinkGearStatusChanged(ThinkGearStatus status)
{}

void DeviceConfiguration::chooseType(int id)
{
tg->setupDeviceType(id);
setupBaudRates();
}
6 changes: 4 additions & 2 deletions src/GUI/SettingsWidget/DeviceConfiguration.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,17 @@ class DeviceConfiguration : public QWidget
~DeviceConfiguration();
public slots:
void chooseDevice(int id);
void chooseBaudrate(int id);
void chooseBaudRate(int id);
void chooseType(int id);
void connectDevice() { tg->open(); }
void disconnectDevice() { tg->close(); }
void setupDeviceTypes();
void refresh();
public slots:
void onThinkGearStatusChanged(ThinkGearStatus status);
private:
Ui::DeviceConfiguration *ui;
void initBaudRates();
void setupBaudRates();
protected:
Status status;
QThinkGear *tg;
Expand Down
39 changes: 31 additions & 8 deletions src/GUI/SettingsWidget/DeviceConfiguration.ui
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>291</width>
<height>82</height>
<height>131</height>
</rect>
</property>
<property name="windowTitle">
Expand All @@ -20,7 +20,7 @@
<property name="geometry">
<rect>
<x>0</x>
<y>20</y>
<y>70</y>
<width>201</width>
<height>27</height>
</rect>
Expand All @@ -30,7 +30,7 @@
<property name="geometry">
<rect>
<x>210</x>
<y>20</y>
<y>70</y>
<width>81</width>
<height>27</height>
</rect>
Expand All @@ -40,7 +40,7 @@
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<y>50</y>
<width>76</width>
<height>19</height>
</rect>
Expand All @@ -53,7 +53,7 @@
<property name="geometry">
<rect>
<x>220</x>
<y>0</y>
<y>50</y>
<width>76</width>
<height>19</height>
</rect>
Expand All @@ -66,7 +66,7 @@
<property name="geometry">
<rect>
<x>210</x>
<y>50</y>
<y>100</y>
<width>81</width>
<height>27</height>
</rect>
Expand All @@ -79,7 +79,7 @@
<property name="geometry">
<rect>
<x>0</x>
<y>50</y>
<y>100</y>
<width>101</width>
<height>27</height>
</rect>
Expand All @@ -92,7 +92,7 @@
<property name="geometry">
<rect>
<x>100</x>
<y>50</y>
<y>100</y>
<width>101</width>
<height>27</height>
</rect>
Expand All @@ -101,6 +101,29 @@
<string>Disconnect</string>
</property>
</widget>
<widget class="QComboBox" name="types_cb">
<property name="geometry">
<rect>
<x>0</x>
<y>20</y>
<width>281</width>
<height>27</height>
</rect>
</property>
</widget>
<widget class="QLabel" name="type_label">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>141</width>
<height>19</height>
</rect>
</property>
<property name="text">
<string>Device Type</string>
</property>
</widget>
</widget>
<resources/>
<connections/>
Expand Down
4 changes: 2 additions & 2 deletions src/GUI/SettingsWidget/SettingsWidget.ui
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@
<property name="geometry">
<rect>
<x>20</x>
<y>40</y>
<y>30</y>
<width>301</width>
<height>121</height>
<height>131</height>
</rect>
</property>
</widget>
Expand Down
8 changes: 7 additions & 1 deletion src/QThinkGear/QThinkGear.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ QThinkGear::QThinkGear(QObject *parent) :
connect(&_device, SIGNAL(readyRead()), this, SLOT(onReadyRead()));
_device.setReadBufferSize(BUFFER_SIZE);
QThinkGear::currentInstance = this;
THINKGEAR_initParser(&_parser, PARSER_TYPE_PACKETS, QThinkGearDataHandle, &_handler);
_status = ThinkGearStatus::NoConnected;
_opened = false;
}
Expand Down Expand Up @@ -75,3 +74,10 @@ void QThinkGear::changeStatus(ThinkGearStatus status)
_status = status;
emit statusChanged(status);
}


void QThinkGear::setupDeviceType(int id)
{
auto type = TGTypes[id];
THINKGEAR_initParser(&_parser, type.parsertype, type.dataHandle, &_handler);
}
18 changes: 16 additions & 2 deletions src/QThinkGear/QThinkGear.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,22 @@

#define BUFFER_SIZE 2048

const int ThinkgearBaudrates[] {
57600, 9600
typedef struct _ThinkgearDeviceType {
std::string name;
std::vector<int> baudrates;
unsigned char parsertype;
void (*dataHandle)(unsigned char extendedCodeLevel,
unsigned char code,
unsigned char numBytes,
const unsigned char *value,
void *customData );
} ThinkgearDeviceType;

const ThinkgearDeviceType TGTypes[] {
{"Neurosky ThinkGear", {57600, 9600}, PARSER_TYPE_PACKETS, QThinkGearDataHandle},
{"2-byte raw wave (unsigned)", {38400, 9600}, PARSER_TYPE_2BYTERAW, QTwoByteRawDataHandle }
};

typedef enum {
Idle,
Reading,
Expand All @@ -23,6 +36,7 @@ class QThinkGear : public QObject
public:
QThinkGear(QObject *parent = nullptr);
~QThinkGear();
void setupDeviceType(int id);
static QThinkGear* qThinkGear() { return currentInstance; }
void addListener(QObject* listener) {
_handler.addListener(listener);
Expand Down
16 changes: 15 additions & 1 deletion src/QThinkGear/QThinkGearDataHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,21 @@ void QThinkGearDataHandle( unsigned char extendedCodeLevel,
{
auto handler = reinterpret_cast<QThinkGearDataHandler*>(customData);
handler->pushData(TGData(code, numBytes, value));
}
}

void QTwoByteRawDataHandle( unsigned char extendedCodeLevel,
unsigned char code,
unsigned char numBytes,
const unsigned char *value,
void *customData )
{
auto handler = reinterpret_cast<QThinkGearDataHandler*>(customData);
short val = 0;
val |= (value[0]&0x3F)<<6;
val |= value[1]&0x3F;
val -= 2048;
emit handler->onRaw(val);
}

QThinkGearDataHandler::QThinkGearDataHandler(QObject *parent)
: QObject(parent)
Expand Down
6 changes: 6 additions & 0 deletions src/QThinkGear/QThinkGearDataHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ void QThinkGearDataHandle( unsigned char extendedCodeLevel,
unsigned char numBytes,
const unsigned char *value,
void *customData );

void QTwoByteRawDataHandle( unsigned char extendedCodeLevel,
unsigned char code,
unsigned char numBytes,
const unsigned char *value,
void *customData );
class QThinkGearListener;
class QThinkGearDataHandler : public QObject
{
Expand Down

0 comments on commit fdef9c4

Please sign in to comment.