Vypiš ten čas jen tehdy, když ta událost nebo co sleduješ končí, tím se vyhneš odčítání minulosti od přítomnosti. Pro těch zbylých 7 pinů asi nikde nenastavuješ start_time a ta proměnná se defaultně inicializuje na nulu, která zde znamená začátek UNIXové epochy, tedy 1. 1. 1970.
Jasný, to jsem si taky myslel, že se to defaultně nastavuje na 0.
Tady mám kód, u kterýho potřebuju, aby se mi vypsal ten rozdíl časů jakmile přijde událost. Použil jsem fci nanosleep ve vláknu, což je prej špatně, protože to vlákno to uspí..
V kódu mám zakomentovaný if podmínky , dle kterých by to logicky mělo jít, ale nejde. Tak budu rád, za nějaký konstruktivní návrh.
void wait(int milli_seconds) { //fce casovace, defaultne jsem ji nastavil na 100[ms]
this_thread::sleep_for(chrono::milliseconds(milli_seconds));
}
/*
int nsleep(unsigned long millisec) {
struct timespec req = {0};
time_t sec = (int)(millisec/1000);
millisec = millisec - (sec*1000);
req.tv_sec = sec;
req.tv_nsec = millisec *1000000L;
while(nanosleep(&req, &req) == -1)
continue;
return 1;
};
*/
void thread_scanner() {
u PINS, ps;
ps.status = 0;
while(true) {
ptime current_time = microsec_clock::local_time();
wait(1);
ICO300_get_DI(&PINS.status);
if(PINS.b.bit0 != 1)
{
times[0].start_time = current_time;
}
else
{
times[1].end_time = current_time;
}
if(PINS.b.bit1 != 1)
{
times[2].start_time = current_time;
}
else
{
times[3].end_time = current_time;
}
if(PINS.b.bit2 != 1)
{
times[4].start_time = current_time;
}
else
{
times[5].end_time = current_time;
}
if(PINS.b.bit3 != 1)
{
times[6].start_time = current_time;
}
else
{
times[7].end_time = current_time;
}
if(PINS.b.bit4 != 1)
{
times[8].start_time = current_time;
}
else
{
times[9].end_time = current_time;
}
if(PINS.b.bit5 != 1)
{
times[10].start_time = current_time;
}
else
{
times[11].end_time = current_time;
}
if(PINS.b.bit6 != 1)
{
times[12].start_time = current_time;
}
else
{
times[13].end_time = current_time;
}
if(PINS.b.bit7 != 1)
{
times[14].start_time = current_time;
}
else
{
times[15].end_time = current_time;
}
// nsleep(2000);
wait(2000); //nanosleep, ceka 2sekundy
ptime t0 = times[0].start_time;
ptime t1 = times[1].end_time;
time_duration pin1 = t1-t0;
// if((pin1.total_microseconds() == 4000000)) {
// cout << " odchylka v [s]: " << pin1.total_seconds() << endl;
// cout << " " << endl;
cout << " pin1 :" << pin1.total_microseconds() << '\n';
// cout << " " << endl;
// cout << "odchylka v [ms]: " << pin1.total_milliseconds() << endl;
// cout << " " << endl;
// cout << "zbytkove vteriny: " << pin1.fractional_seconds() << endl;
// }
ptime t2 = times[2].start_time;
ptime t3 = times[3].end_time;
time_duration pin2 = t3-t2;
// if((pin2.total_microseconds() == 4000000)) {
cout << " pin2: " << pin2.total_microseconds() << '\n';
// }
ptime t4 = times[4].start_time;
ptime t5 = times[5].end_time;
time_duration pin3 = t5-t4;
// if((pin3.total_microseconds() == 4000000)) {
cout << " pin3: " << pin3.total_microseconds() << '\n';
// }
ptime t6 = times[6].start_time;
ptime t7 = times[7].end_time;
time_duration pin4 = t7-t6;
// if((pin4.total_microseconds() == 4000000)) {
cout << " pin4: " << pin4.total_microseconds() << '\n';
// }
ptime t8 = times[8].start_time;
ptime t9 = times[9].end_time;
time_duration pin5 = t9-t8;
// if((pin5.total_microseconds() == 4000000)) {
cout << " pin5: " << pin5.total_microseconds() << '\n';
// }
ptime t10 = times[10].start_time;
ptime t11 = times[11].end_time;
time_duration pin6 = t11-t10;
// if((pin6.total_microseconds() == 4000000)) {
cout << " pin6: " << pin6.total_microseconds() << '\n';
// }
ptime t12 = times[12].start_time;
ptime t13 = times[13].end_time;
time_duration pin7 = t13-t12;
// if(( pin7.total_microseconds() == 4000000)) {
cout << " pin7: " << pin7.total_microseconds() << '\n';
// }
ptime t14 = times[14].start_time;
ptime t15 = times[15].end_time;
time_duration pin8 = t15-t14;
// if(( pin8.total_microseconds() == 4000000)) {
cout << " pin8: " << pin8.total_microseconds() << '\n';
// }
}
};