46
Vývoj / Re:Detekce náběžné hrany
« kdy: 07. 08. 2016, 17:07:21 »Nechtěl bys poslat celý kód?
Ok, tady je:
Kód: [Vybrat]
#include <stdio.h> //g++ -o scanner_vstupu vstupy-vystupy.cpp -m32 -std=c++11 -I /usr/include/i386-linux-gnu/ -L . -lico300 -pthread
#include <unistd.h>
#include <cstdint>
#include <thread>
#include <iostream>
#include <iomanip>
#include "boost/date_time/posix_time/posix_time.hpp"
extern "C" {
#include "libico300.h"
}
using namespace std;
using namespace boost::posix_time;
void wait(int milli_seconds)
{
this_thread::sleep_for(chrono::milliseconds(milli_seconds));
}
bool endflag = false;
void threadf(int cas)
{
int a = 0;
while(true)
{
wait(cas);
std::uint8_t stav; //preda referenci stav jednotlivych bitu
ICO300_get_DI(&stav); //ziska stav DI pinů
if (stav != a )
{
cout << " PULS " << " " << " hexadecimalni cislo: " << hex << " " << (int)stav << " dekadicke cislo: " << dec << (int)stav << endl;
}
else
{
cout << " MEZERA " << " " << " hexadecimalni cislo: " << hex << " " << (int)stav << " dekadicke cislo: " << dec << (int)stav << endl;
}
a = stav;
/*
for(int i =0; i < 8; ++i){ //Daviduv optimalizacni algoritmus
cout << (int)(stav & 1);
stav >>= 1;
}
*/
if(endflag)
break;
}
}
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 posledni_hodnota;
} times[8];
void delka_impulsu(int CAS){
u STAV, ps;
ps.status = 0;
while(true)
{
ptime current_time = microsec_clock::local_time();
ICO300_get_DI(&STAV.status);
if (STAV.b.bit0 != ps.b.bit0)
{
if(STAV.b.bit0 == 1)
{
times[0].start_time = current_time;
}
else
{
times[0].end_time = current_time;
}
}
wait(CAS);
}
}
int main()
{ char c;
ICO300_set_DIO_mode(0xff); //Vsechny piny jsou vstupni
int b;
cout << "Zadejte cas v [ms]: " << endl;
cin >> b;
std::thread t{threadf, b}; //Inicializace vlakna
cout << "Pro odpojeni vlakna stisknete lib. klavesu od 'Q'" << endl;
cin >> c;
if (c != 'Q')
{
endflag = true;
}
t.join();
delka_impulsu(b);
ptime t1 = times[0].start_time;
ptime t2 = times[0].end_time;
time_duration diff = t1 - t2;
cout << "Delka pulsu je: " << diff << endl;
return 0;
}