Skip to content

Commit

Permalink
Apply .astylerc file on rehidhelper
Browse files Browse the repository at this point in the history
  • Loading branch information
hax0kartik committed May 14, 2024
1 parent 2a52eed commit bbeabd4
Show file tree
Hide file tree
Showing 28 changed files with 8,324 additions and 6,356 deletions.
77 changes: 45 additions & 32 deletions rehidhelper/source/States/Camera.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "Camera.hpp"
#include "../app.hpp"

Camera::Camera(){
Camera::Camera() {
camInit();
LightLock_Init(&m_messagelock);
LightLock_Init(&m_imagelock);
Expand All @@ -10,12 +10,15 @@ Camera::Camera(){
C3D_TexInit(m_tex, 512, 256, GPU_RGB565);
C3D_TexSetFilter(m_tex, GPU_LINEAR, GPU_LINEAR);
static const Tex3DS_SubTexture subt3x = { 512, 256, 0.0f, 1.0f, 1.0f, 0.0f };
m_img = (C2D_Image){ m_tex, &subt3x };
m_img = (C2D_Image) {
m_tex, &subt3x
};

m_context = quirc_new();
quirc_resize(m_context, 400, 240);
}

Camera::~Camera(){
Camera::~Camera() {
quirc_destroy(m_context);
C3D_TexDelete(m_tex);
delete m_tex;
Expand All @@ -25,7 +28,7 @@ Camera::~Camera(){
camExit();
}

void Camera::OnStateEnter(App *app){
void Camera::OnStateEnter(App *app) {
m_payloadfound = false;
m_textbuf = C2D_TextBufNew(500);
m_buffer = new uint16_t[400 * 240];
Expand All @@ -41,24 +44,28 @@ void Camera::OnStateEnter(App *app){
int w, h;
bool done = false;
svcSetThreadPriority(CUR_THREAD_HANDLE, camera.GetMainThreadPrio() + 1);
while(!done){

while (!done) {
svcSleepThread(10); // yield from this thread;
uint8_t *img = (uint8_t*)quirc_begin(camera.GetQuircContext(), &w, &h);
LightLock_Lock(camera.GetImageLock());
for(int x = 0; x < w; x++){
for(int y = 0; y < h; y++){

for (int x = 0; x < w; x++) {
for (int y = 0; y < h; y++) {
uint16_t px = camera.GetImageBuffer()[y * 400 + x];
img[y * w + x] = (uint8_t)(((((px >> 11) & 0x1F) << 3) + (((px >> 5) & 0x3F) << 2) + ((px & 0x1F) << 3)) / 3);
}
}

LightLock_Unlock(camera.GetImageLock());
quirc_end(camera.GetQuircContext());
if(quirc_count(camera.GetQuircContext()) > 0)
{

if (quirc_count(camera.GetQuircContext()) > 0) {
struct quirc_code code;
struct quirc_data scandata;
quirc_extract(camera.GetQuircContext(), 0, &code);
if(!quirc_decode(&code, &scandata)){

if (!quirc_decode(&code, &scandata)) {
s2 = "QR found. Press A to proceed.";
camera.SetString(s2);
camera.SetPayloadFound(true);
Expand All @@ -67,7 +74,8 @@ void Camera::OnStateEnter(App *app){
done = true;
}
}
if(!svcWaitSynchronization(camera.GetFinishedEvent(), 0))

if (!svcWaitSynchronization(camera.GetFinishedEvent(), 0))
done = true;
}
}, *this, app, 100 * 1024);
Expand All @@ -94,12 +102,13 @@ void Camera::OnStateEnter(App *app){
svcSetThreadPriority(CUR_THREAD_HANDLE, camera.GetMainThreadPrio() - 2);
CAMU_SetReceiving(&events[0], buffer, PORT_CAM1, 400 * 240 * sizeof(u16), (s16) transferunit);
CAMU_StartCapture(PORT_CAM1);
while(!finished){

while (!finished) {
int32_t index = 0;
svcWaitSynchronizationN(&index, events, 3, false, -1LL);
switch(index){
case 0: // Recieve Event
{

switch (index) {
case 0: { // Recieve Event
LightLock_Lock(camera.GetImageLock());
svcCloseHandle(events[0]);
events[0] = 0;
Expand All @@ -109,8 +118,7 @@ void Camera::OnStateEnter(App *app){
break;
}

case 1: // Buffer Error event
{
case 1: { // Buffer Error event
svcCloseHandle(events[0]);
events[0] = 0;
CAMU_ClearBuffer(PORT_CAM1);
Expand All @@ -119,56 +127,61 @@ void Camera::OnStateEnter(App *app){
break;
}

case 2: // Finished scanning
{
case 2: { // Finished scanning
finished = true;
break;
}
}
}
CAMU_StopCapture(PORT_CAM1);
bool busy = false;
while(R_SUCCEEDED(CAMU_IsBusy(&busy, PORT_CAM1)) && busy)
svcSleepThread(1e+9);

while (R_SUCCEEDED(CAMU_IsBusy(&busy, PORT_CAM1)) && busy)
svcSleepThread(1e + 9);
CAMU_ClearBuffer(PORT_CAM1);
CAMU_Activate(SELECT_NONE);
delete[] buffer;
}, *this, app, 1024 * 1024 * 2);
}

void Camera::OnStateExit(App *app){
void Camera::OnStateExit(App *app) {
svcSignalEvent(m_finishedevent);
while(!m_worker.IsDone() || !m_decodeworker.IsDone()){
svcSleepThread(0.05e+9);

while (!m_worker.IsDone() || !m_decodeworker.IsDone()) {
svcSleepThread(0.05e + 9);
}

delete[] m_buffer;
C2D_TextBufDelete(m_textbuf);
}

std::optional<ui::States> Camera::HandleEvent(){
if((keysDown() & KEY_A) && m_payloadfound)
std::optional<ui::States> Camera::HandleEvent() {
if ((keysDown() & KEY_A) && m_payloadfound)
return ui::States::GameSelection;
if(keysDown() & KEY_B)

if (keysDown() & KEY_B)
return ui::States::MainMenu;

return {};
}

void Camera::RenderLoop(){
void Camera::RenderLoop() {
auto top = ui::g_RenderTarget.GetRenderTarget(ui::Screen::Top);
auto bottom = ui::g_RenderTarget.GetRenderTarget(ui::Screen::Bottom);

/* Top */
C2D_SceneBegin(top);
C2D_TargetClear(top, C2D_Color32(0xEC, 0xF0, 0xF1, 0xFF));
LightLock_Lock(&m_imagelock);
for(uint32_t x = 0; x < 400; x++){
for(uint32_t y = 0; y < 240; y++){

for (uint32_t x = 0; x < 400; x++) {
for (uint32_t y = 0; y < 240; y++) {
uint32_t dstPos = ((((y >> 3) * (512 >> 3) + (x >> 3)) << 6) + ((x & 1) | ((y & 1) << 1) | ((x & 2) << 1) | ((y & 2) << 2) | ((x & 4) << 2) | ((y & 4) << 3))) * 2;
uint32_t srcPos = (y * 400 + x) * 2;
memcpy(&((u8*)m_img.tex->data)[dstPos], &((u8*)m_buffer)[srcPos], 2);
}
}

C2D_DrawImageAt(m_img, 0.0f, 0.0f, 0.5f, NULL, 1.0f, 1.0f);
LightLock_Unlock(&m_imagelock);

Expand All @@ -184,7 +197,7 @@ void Camera::RenderLoop(){
LightLock_Unlock(&m_messagelock);
}

void Camera::SetString(const std::string &str){
void Camera::SetString(const std::string &str) {
LightLock_Lock(&m_messagelock);
m_message = str;
C2D_TextParse(&m_text, m_textbuf, m_message.c_str());
Expand Down
94 changes: 57 additions & 37 deletions rehidhelper/source/States/Camera.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,63 @@
#include "../ui.hpp"
#include "../workerthread.hpp"
extern "C" {
#include "../quirc/quirc.h"
#include "../quirc/quirc.h"
}

class Camera : public ui::State{
public:
Camera();
~Camera() override;
std::optional<ui::States> HandleEvent() override;
void OnStateEnter(App *app) override;
void OnStateExit(App *app) override;
void RenderLoop() override;

void SetString(const std::string& s);
Handle GetFinishedEvent() { return m_finishedevent; }
LightLock *GetImageLock() { return &m_imagelock; }
uint16_t *GetImageBuffer() { return m_buffer; }
int32_t GetMainThreadPrio(){ return m_mainthreadprio; }
struct quirc *GetQuircContext(){ return m_context; }
std::string& GetPayload() { return m_payload; }
void SetPayloadFound(bool val) { m_payloadfound = val; }

private:
std::string m_message;
std::string m_payload;
LightLock m_messagelock;
LightLock m_imagelock;
Handle m_finishedevent;
uint16_t *m_buffer;
int32_t m_mainthreadprio;
struct quirc *m_context;
bool m_payloadfound;

/* UI */
C3D_Tex *m_tex;
C2D_Image m_img;
C2D_TextBuf m_textbuf;
C2D_Text m_text;
WorkerThread<Camera&, App*> m_worker;
WorkerThread<Camera&, App*> m_decodeworker;
class Camera : public ui::State {
public:
Camera();
~Camera() override;
std::optional<ui::States> HandleEvent() override;
void OnStateEnter(App *app) override;
void OnStateExit(App *app) override;
void RenderLoop() override;

void SetString(const std::string& s);
Handle GetFinishedEvent() {
return m_finishedevent;
}

LightLock *GetImageLock() {
return &m_imagelock;
}

uint16_t *GetImageBuffer() {
return m_buffer;
}

int32_t GetMainThreadPrio() {
return m_mainthreadprio;
}

struct quirc *GetQuircContext() {
return m_context;
}

std::string& GetPayload() {
return m_payload;
}

void SetPayloadFound(bool val) {
m_payloadfound = val;
}

private:
std::string m_message;
std::string m_payload;
LightLock m_messagelock;
LightLock m_imagelock;
Handle m_finishedevent;
uint16_t *m_buffer;
int32_t m_mainthreadprio;
struct quirc *m_context;
bool m_payloadfound;

/* UI */
C3D_Tex *m_tex;
C2D_Image m_img;
C2D_TextBuf m_textbuf;
C2D_Text m_text;
WorkerThread<Camera&, App*> m_worker;
WorkerThread<Camera&, App*> m_decodeworker;
};
Loading

0 comments on commit bbeabd4

Please sign in to comment.