Initial commit of version from 2015

This commit is contained in:
Holger Weber 2025-11-27 16:52:08 +01:00
commit 6e248add51
5 changed files with 444 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
build

5
CMakeLists.txt Normal file
View File

@ -0,0 +1,5 @@
cmake_minimum_required (VERSION 3.5)
project (rocket_usb)
add_definitions( -DUSE_UNTESTED_LIBUSBPP_METHODS )
add_executable(rocket CUsbRocket.cpp main.cpp)
target_link_libraries(rocket usb pthread)

262
CUsbRocket.cpp Normal file
View File

@ -0,0 +1,262 @@
#include "CUsbRocket.h"
#include <iostream>
#include <unistd.h>
using namespace std;
CUsbRocket::CUsbRocket() :
m_iMoveState(MOVE_STOP), m_iSpeed(SPEED_SLOW), m_hdlRock(NULL)
{
}
CUsbRocket::~CUsbRocket()
{
CloseUsbRocket();
}
int
CUsbRocket::InitUsbRocket()
{
struct usb_bus *busses;
usb_init();
usb_find_busses();
usb_find_devices();
busses = usb_get_busses();
struct usb_bus *bus;
int c, i, a;
/* ... */
for (bus = busses; bus; bus = bus->next)
{
struct usb_device *dev;
for (dev = bus->devices; dev; dev = dev->next)
{
/* check for rocket */
if (dev->descriptor.idVendor == VID && dev->descriptor.idProduct == PID)
{
m_hdlRock = usb_open(dev);
cout << "open: " << m_hdlRock << endl;
cout << "Num Config: " << (int) dev->descriptor.bNumConfigurations << endl;
cout << "Num If: " << (int) dev->config[0].bNumInterfaces << endl;
cout << "usb_detach_kernel_driver_np: " << usb_detach_kernel_driver_np(m_hdlRock, 0) << endl;
cout << "usb_set_configuration: " << usb_set_configuration(m_hdlRock, 1) << endl;
cout << "usb_claim_interface: " << usb_claim_interface(m_hdlRock, 0) << endl;
}
}
}
/*
if (iRetval < 0)
{
cerr << "Init Error " << iRetval << endl; //there was an error
return -1;
}
// cout << "Init usb session done...\n";
m_hdlRock = libusb_open_device_with_vid_pid(m_ctx, VID, PID);
// check if device was found
if (m_hdlRock == NULL)
{
cerr << "Rocket device not found\n";
return -1;
}
// cout << "Found Rocket\n";
// check if device is used by kernel and remove if it is
if (libusb_kernel_driver_active(m_hdlRock, 0))
{
//cerr << "Detaching from kernel\n";
libusb_detach_kernel_driver(m_hdlRock, 0);
}
// setup configuration
iRetval = libusb_set_configuration(m_hdlRock, 1);
if (iRetval)
{
cerr << "Error setting configuration.\n";
}
// claim device
iRetval = libusb_claim_interface(m_hdlRock, 0);
if (iRetval)
{
cerr << "Error claiming device.\n";
return -1;
}
*/
return 0;
}
void
CUsbRocket::CloseUsbRocket()
{
if (m_hdlRock)
{
m_iMoveState = MOVE_EXIT;
usleep(500000);
MoveStop();
usleep(500000);
usb_close(m_hdlRock);
}
m_hdlRock = NULL;
// if (m_ctx)
// libusb_exit(m_ctx);
// m_ctx = NULL;
}
void
CUsbRocket::MoveUp()
{
char ucData[] = {0x02, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
if (m_hdlRock)
{
usb_control_msg(m_hdlRock, 0x21, 0x09, 0, 0, ucData, 8, 0);
}
}
void
CUsbRocket::MoveDown()
{
char ucData[] = {0x02, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
if (m_hdlRock)
{
usb_control_msg(m_hdlRock, 0x21, 0x09, 0, 0, ucData, 8, 0);
}
}
void
CUsbRocket::MoveLeft()
{
char ucData[] = {0x02, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
if (m_hdlRock)
{
usb_control_msg(m_hdlRock, 0x21, 0x09, 0, 0, ucData, 8, 0);
}
}
void
CUsbRocket::MoveRight()
{
char ucData[] = {0x02, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
if (m_hdlRock)
{
usb_control_msg(m_hdlRock, 0x21, 0x09, 0, 0, ucData, 8, 0);
}
}
void
CUsbRocket::Fire()
{
char ucData[] = {0x02, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
if (m_hdlRock)
{
usb_control_msg(m_hdlRock, 0x21, 0x09, 0, 0, ucData, 8, 0);
}
}
void
CUsbRocket::MoveStop()
{
char ucData[] = {0x02, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
if (m_hdlRock)
{
usb_control_msg(m_hdlRock, 0x21, 0x09, 0, 0, ucData, 8, 0);
}
}
int
CUsbRocket::StartMovement()
{
if (InitUsbRocket() == 0)
{
pthread_create(&m_thread, NULL, &CUsbRocket::MoveThread, this);
return 0;
}
return -1;
}
void*
CUsbRocket::MoveThread(void *ptr)
{
//cout << "Running Move thread\n";
CUsbRocket* pRocket = static_cast<CUsbRocket*> (ptr);
while (pRocket->m_iMoveState != MOVE_EXIT)
{
switch (pRocket->m_iMoveState)
{
case MOVE_UP:
pRocket->MoveUp();
break;
case MOVE_DOWN:
pRocket->MoveDown();
break;
case MOVE_LEFT:
pRocket->MoveLeft();
break;
case MOVE_RIGHT:
pRocket->MoveRight();
break;
case MOVE_STOP:
pRocket->MoveStop();
break;
case MOVE_FIRE:
pRocket->MoveStop();
pRocket->Fire();
pRocket->m_iMoveState = MOVE_STOP;
sleep(3);
break;
default:
pRocket->m_iMoveState = MOVE_STOP;
}
switch (pRocket->m_iSpeed)
{
case SPEED_SLOW:
usleep(2500);
pRocket->MoveStop();
usleep(20000);
break;
case SPEED_MEDIUM:
usleep(10000);
pRocket->MoveStop();
usleep(20000);
break;
case SPEED_FASTEST:
usleep(40000);
break;
default:
pRocket->m_iSpeed = SPEED_SLOW;
}
}
//cout << "Exiting Move thread\n";
return NULL;
}
void
CUsbRocket::SetMoveDirection(MOVESTATE iDir)
{
m_iMoveState = iDir;
}
void
CUsbRocket::SetMoveSpeed(MOVESPEED iSpeed)
{
m_iSpeed = iSpeed;
}

55
CUsbRocket.h Normal file
View File

@ -0,0 +1,55 @@
#ifndef _CUSB_ROCKET_H_
#define _CUSB_ROCKET_H_
#include <usb.h>
#define VID 0x2123
#define PID 0x1010
class CUsbRocket
{
public:
CUsbRocket();
~CUsbRocket();
int InitUsbRocket();
void CloseUsbRocket();
enum MOVESPEED
{
SPEED_SLOW, SPEED_MEDIUM, SPEED_FASTEST
};
enum MOVESTATE
{
MOVE_STOP,
MOVE_UP,
MOVE_DOWN,
MOVE_LEFT,
MOVE_RIGHT,
MOVE_FIRE,
MOVE_EXIT
};
void SetMoveDirection(MOVESTATE iDir);
void SetMoveSpeed(MOVESPEED iSpeed);
int StartMovement();
private:
static void* MoveThread( void *ptr );
void MoveUp();
void MoveDown();
void MoveLeft();
void MoveRight();
void Fire();
void MoveStop();
int m_iMoveState;
int m_iSpeed;
pthread_t m_thread;
usb_dev_handle* m_hdlRock;
};
#endif /* _CUSB_ROCKET_H_ */

121
main.cpp Normal file
View File

@ -0,0 +1,121 @@
#include <iostream>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include "CUsbRocket.h"
using namespace std;
bool HandleMsg(int newsockfd);
CUsbRocket g_Rocket;
int main(int argc, char** argv)
{
g_Rocket.StartMovement();
bool bRunning = true;
int sockfd, newsockfd, portno;
socklen_t clilen;
struct sockaddr_in serv_addr, cli_addr;
int n;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
cerr << "ERROR opening socket\n";
bzero((char *) &serv_addr, sizeof(serv_addr));
portno = 8088;
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
cerr << "ERROR on binding\n";
n = 1;
while (bRunning)
{
listen(sockfd, 5);
clilen = sizeof(cli_addr);
newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
if (newsockfd < 0)
cerr << "ERROR on accept\n";
else
cout << "Client connected\n";
bRunning = HandleMsg(newsockfd);
cout << "Client disconnected\n";
}
close(newsockfd);
close(sockfd);
return 0;
}
bool HandleMsg(int newsockfd)
{
bool bRunning = true;
int n = 1;
char buffer[2];
while (n > 0 && bRunning)
{
n = read(newsockfd, buffer, 1);
if (n > 0)
{
cout << buffer[0] << endl;
switch (buffer[0])
{
case 'w':
g_Rocket.SetMoveDirection(CUsbRocket::MOVE_UP);
break;
case 's':
g_Rocket.SetMoveDirection(CUsbRocket::MOVE_DOWN);
break;
case 'a':
g_Rocket.SetMoveDirection(CUsbRocket::MOVE_LEFT);
break;
case 'd':
g_Rocket.SetMoveDirection(CUsbRocket::MOVE_RIGHT);
break;
case 'q':
g_Rocket.SetMoveDirection(CUsbRocket::MOVE_STOP);
break;
case 'x':
g_Rocket.SetMoveDirection(CUsbRocket::MOVE_EXIT);
bRunning = false;
break;
case 'r':
g_Rocket.SetMoveSpeed(CUsbRocket::SPEED_FASTEST);
break;
case 'f':
g_Rocket.SetMoveSpeed(CUsbRocket::SPEED_MEDIUM);
break;
case 'v':
g_Rocket.SetMoveSpeed(CUsbRocket::SPEED_SLOW);
break;
case ' ':
g_Rocket.SetMoveDirection(CUsbRocket::MOVE_FIRE);
break;
}
}
}
return bRunning;
}