Tady to je, uznávám, že jsem to předtím naprasil na všech osm pinů.
Je to nepřehledné, ono jde o to, odevzdat hotový úkol ve firmě, nikoho nezajímá jak je to udělané, prostě aby to bylo...
Jak říkám, nanosleep prej použít nemám, protože to uspává vlákno...Mne to přišlo jako log. řešení, když vzorkuji 1ms..
Naopak výpis rozdílu času jen když nastane událost(na vstupu bude 1) mi prostě nešel..
tady je kód:
#include <stdio.h>
#include <unistd.h>
#include <cstdint>
#include <thread>
#include <iostream>
#include <iomanip>
#include <ctime>
#include <time.h>
#include "boost/date_time/posix_time/posix_time.hpp"
extern "C" {
#include "libico.h" //mnou upraveny header pro DIO->kvuli prehlednosti fci
}
using namespace std;
using namespace boost::posix_time;
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;
};
struct bits {
unsigned bit0 : 1;
unsigned bit1 : 1;
unsigned bit2 : 1;
unsigned bit3 : 1;
unsigned bit4 : 1;
unsigned bit5 : 1;
unsigned bit6 : 1;
unsigned bit7 : 1;
};
union u {
unsigned char status;
bits b;
};
struct time {
boost::posix_time::ptime start_time, end_time;
int ps;
}times[8];
void thread_scanner() {
u PINS, ps;
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;
}
nsleep(2000); //nanosleep, ceka 2sekundy
ptime t0 = times[0].start_time;
ptime t1 = times[0].end_time;
time_duration pin1 = t1-t0;
cout << " pin1 :" << pin1.total_microseconds() << '\n';
};
int main() {
ICO300_set_DIO_mode(0xFF); //definovani DIO -> vsechny jsou vstupni -> 1
ICO300_set_DO(0); //pro bezproblemovost provozu jsem vynuloval DO
thread scanner{thread_scanner}; //inicializace vlakna
scanner.join();
return 0;
}