Created
January 11, 2023 09:09
-
-
Save iwishiwasaneagle/b469be465b2a147c90e7e55821db0597 to your computer and use it in GitHub Desktop.
RTEP5 2023 Demonstrator Practice Code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# Created by iwishiwasaneagle on 11/01/23. | |
# | |
cmake_minimum_required(VERSION 3.23) | |
project(rtep) | |
set(CMAKE_CXX_STANDARD 23) | |
add_executable(rtep main.cpp main.h) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Created by iwishiwasaneagle on 11/01/23. | |
// | |
#include <sstream> | |
#include "main.h" | |
int Mouse::registerCallback(Mousecallback *mc) { | |
callbacks.insert(callbacks.begin(), mc); | |
return true; | |
} | |
int Mouse::start() { | |
printf("Starting thread\n"); | |
this->worker_thread = std::thread(&Mouse::worker, this); | |
auto tid = this->worker_thread.get_id(); | |
std::stringstream ss; | |
ss << tid; | |
worker_thread_id = ss.str().c_str(); | |
printf("Thread started with ID=%s\n", worker_thread_id); | |
return true; | |
} | |
int Mouse::stop() { | |
printf("Closing thread %s\n", worker_thread_id); | |
this->stop_worker_thread = true; | |
printf("Waiting for thread %s to finish...\n", worker_thread_id); | |
this->worker_thread.join(); | |
printf("Thread %s closed\n", worker_thread_id); | |
return true; | |
} | |
int Mouse::worker() { | |
int fd, bytes; | |
unsigned char data[3]; | |
// Open Mouse | |
fd = open(this->pDevice.c_str(), O_RDWR); | |
if (fd == -1) { | |
printf("ERROR Opening %s\n", pDevice.c_str()); | |
return -1; | |
} | |
int left, middle, right; | |
signed char x, y; | |
while (!this->stop_worker_thread) { | |
// Read Mouse | |
bytes = read(fd, data, sizeof(data)); | |
if (bytes > 0) { | |
left = data[0] & 0x1; | |
right = data[0] & 0x2; | |
middle = data[0] & 0x4; | |
x = data[1]; | |
y = data[2]; | |
for (auto cb: callbacks) { | |
if (cb->hasData(x, y, left, middle, right)) { | |
cb->printData(x, y, left, middle, right); | |
} | |
} | |
} | |
} | |
return 0; | |
} | |
struct MyMousecallback : Mousecallback { | |
bool hasData(int x, int y, bool left, bool middle, bool right) override { | |
return (x && y && (left || middle || right)); | |
} | |
void printData(int x, int y, bool left, bool middle, bool right) override { | |
printf("x=%d, y=%d, left=%d, middle=%d, right=%d\n", x, y, left, middle, right); | |
} | |
}; | |
int main(int argc, char **argv) { | |
Mouse m = Mouse("/dev/input/mice"); | |
MyMousecallback mc; | |
m.registerCallback(&mc); | |
m.start(); | |
sleep(5); | |
m.stop(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Created by iwishiwasaneagle on 11/01/23. | |
// | |
#ifndef RTEP_MAIN_H | |
#define RTEP_MAIN_H | |
#include <stdio.h> | |
#include <unistd.h> | |
#include <fcntl.h> | |
#include <string> | |
#include <atomic> | |
#include <thread> | |
#include <list> | |
struct Mousecallback { | |
virtual bool hasData(int x, int y, bool left, bool middle, bool right) = 0; | |
virtual void printData(int x, int y, bool left, bool middle, bool right) = 0; | |
}; | |
class Mouse { | |
private: | |
std::string pDevice; | |
std::list<Mousecallback *> callbacks; | |
std::atomic_bool stop_worker_thread = false; | |
std::thread worker_thread; | |
const char *worker_thread_id = NULL; | |
int worker(); | |
public: | |
Mouse(std::string pDevice){ | |
this->pDevice = pDevice; | |
} | |
int registerCallback(Mousecallback *mc); | |
int start(); | |
int stop(); | |
}; | |
#endif //RTEP_MAIN_H |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment