Skip to content

Commit

Permalink
limited frequency of scan time changes
Browse files Browse the repository at this point in the history
  • Loading branch information
kmicki committed May 7, 2022
1 parent b23b5a3 commit 093ccff
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 10 deletions.
2 changes: 1 addition & 1 deletion inc/hiddev/hiddevreader.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ namespace kmicki::hiddev

std::mutex startStopMutex;

void LossAnalysis(uint32_t diff);
bool LossAnalysis(uint32_t diff);

int lossPeriod;
bool lossAnalysis;
Expand Down
5 changes: 2 additions & 3 deletions src/cemuhook/cemuhookserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ namespace kmicki::cemuhook
{
stopSending = true;
sendThread.get()->join();
std::this_thread::sleep_for(std::chrono::seconds(1));
//std::this_thread::sleep_for(std::chrono::seconds(1));
sendThread.reset();
}
if(sendThread.get() == nullptr)
Expand All @@ -207,7 +207,7 @@ namespace kmicki::cemuhook
std::cout << "Cemuhook Server: No packet from client for some time. Stop sending data." << std::endl;
stopSending = true;
sendThread.get()->join();
std::this_thread::sleep_for(std::chrono::seconds(1));
//std::this_thread::sleep_for(std::chrono::seconds(1));
sendThread.reset();
}
sendTimeout = 0;
Expand All @@ -228,7 +228,6 @@ namespace kmicki::cemuhook
std::cout << "Cemuhook Server: Initiaiting frame grab start." << std::endl;
motionSource.StartFrameGrab();


std::pair<uint16_t , void const*> outBuf;
uint32_t packet = 0;

Expand Down
30 changes: 26 additions & 4 deletions src/hiddev/hiddevreader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,9 @@ namespace kmicki::hiddev
}
}

void HidDevReader::LossAnalysis(uint32_t diff)
bool HidDevReader::LossAnalysis(uint32_t diff)
{
bool result = false;
if(lossAnalysis)
{
if(++lossPeriod > MAX_LOSS_PERIOD)
Expand All @@ -179,6 +180,7 @@ namespace kmicki::hiddev
scanPeriod -= std::chrono::microseconds(std::max(1,(6000-lossPeriod)/100));
std::cout << "Changed scan period to : " << scanPeriod.count() << " us" << std::endl;
lossAnalysis = true;
result = true;
}
else if(!lossAnalysis)
lossAnalysis = true;
Expand All @@ -187,9 +189,11 @@ namespace kmicki::hiddev
smallLossEncountered = true;
++scanPeriod;
std::cout << "Changed scan period to : " << scanPeriod.count() << " us" << std::endl;
result = true;
}
lossPeriod = 0;
}
return result;
}

void HidDevReader::executeReadingTask()
Expand All @@ -199,6 +203,9 @@ namespace kmicki::hiddev
std::vector<char> buf1_2(INPUT_RECORD_LEN*frameLen);
std::vector<char>* buf = &buf1_1; // buffer swapper

auto startTime = std::chrono::system_clock::now();
bool passedNoLossAnalysis = false;

enter=exit=0;
tick=rdy=wait=false;
int fail = 0;
Expand Down Expand Up @@ -252,12 +259,18 @@ namespace kmicki::hiddev
continue;
}
fail=0;

if(input.fail() || *(reinterpret_cast<unsigned int*>(buf->data())) != 0xFFFF0002)
{
// Failed to read a frame
// or start in the middle of the input frame
reconnectInput(input,inputFilePath);
{
std::unique_lock<std::mutex> lock(m);
reconnectInput(input,inputFilePath);
rdy = false; tick = true;
if(wait)
v.notify_one();
}
}
else {
frameMutex.lock();
Expand All @@ -280,7 +293,16 @@ namespace kmicki::hiddev

uint32_t * newInc = reinterpret_cast<uint32_t *>(frame.data()+4);
uint32_t diff = *newInc - lastInc;
LossAnalysis(diff);

if(passedNoLossAnalysis || std::chrono::system_clock::now()-startTime > std::chrono::seconds(1))
{
passedNoLossAnalysis = true;
if(LossAnalysis(diff))
{
startTime = std::chrono::system_clock::now();
passedNoLossAnalysis = false;
}
}

if(diff > 1 && lastInc > 0 && diff <= 40)
{
Expand Down
4 changes: 2 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ using namespace kmicki::hiddev;
bool showIncrement = false;

#define FRAME_LEN 64
#define SCAN_PERIOD_US 3900
#define SCAN_PERIOD_US 3945

#define VERSION "1.8"

Expand Down Expand Up @@ -58,7 +58,7 @@ int main()
int stopping = 0;

while(true) {
std::this_thread::sleep_for(std::chrono::seconds(1));
std::this_thread::sleep_for(std::chrono::seconds(5));
uint32_t const& newInc = *reinterpret_cast<uint32_t const*>(reader.Frame().data()+4);
if(newInc == lastInc)
{
Expand Down

0 comments on commit 093ccff

Please sign in to comment.