Advertisement
Guest User

detekcia-nabeznej-hrany

a guest
Aug 24th, 2016
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.25 KB | None | 0 0
  1. #include <stdio.h> //g++ -o scanner_vstupu vstupy-vystupy.cpp -m32 -std=c++11 -I /usr/include/i386-linux-gnu/ -L . -lico300 -pthread
  2. #include <unistd.h>
  3. #include <cstdint>
  4. #include <thread>
  5. #include <mutex>  // for securing shared variable
  6. #include <iostream>
  7. #include <iomanip>
  8. #include "boost/date_time/posix_time/posix_time.hpp"
  9.  
  10. extern "C" {
  11.     #include "libico300.h"
  12. }
  13.  
  14. using namespace std;
  15. using namespace boost::posix_time;
  16.  
  17. void wait(int milli_seconds) {
  18.     this_thread::sleep_for(chrono::milliseconds(milli_seconds));
  19. }
  20.  
  21. std::mutex m_mutex;     // var for securing controlling access to endflag var
  22. bool endflag = false;
  23.  
  24. void threadf(int cas) {
  25.     int a = 0;
  26.  
  27.     // Var moved outside while due to minimising
  28.     std::uint8_t stav;                //preda referenci stav jednotlivych bitu
  29.  
  30.     while (true)
  31.     {
  32.         wait(cas);
  33.  
  34.         //std::uint8_t stav;                //preda referenci stav jednotlivych bitu
  35.  
  36.         ICO300_get_DI(&stav);                //ziska stav DI pinů
  37.  
  38.         if (stav != a)
  39.         {
  40.             cout << " PULS " << " " << " hexadecimalni cislo: " << hex << " " << (int) stav << " dekadicke cislo: "
  41.                  << dec << (int) stav << endl;
  42.         }
  43.         else
  44.         {
  45.             cout << " MEZERA " << " " << " hexadecimalni cislo: " << hex << " " << (int) stav << " dekadicke cislo: "
  46.                  << dec << (int) stav << endl;
  47.         }
  48.  
  49.         a = stav;
  50.  
  51. /*
  52.     for(int i =0; i < 8; ++i){          //Daviduv optimalizacni algoritmus
  53.       cout << (int)(stav & 1);
  54.       stav >>= 1;
  55.  }
  56.  
  57. */
  58.  
  59.         {
  60.             std::lock_guard<std::mutex> lck(m_mutex);   // entering critical section
  61.             if (endflag)
  62.                 break;
  63.         }
  64.     }
  65. }
  66.  
  67. struct bits {
  68.     unsigned bit0 : 1;
  69.     unsigned bit1 : 1;
  70.     unsigned bit2 : 1;
  71.     unsigned bit3 : 1;
  72.     unsigned bit4 : 1;
  73.     unsigned bit5 : 1;
  74.     unsigned bit6 : 1;
  75.     unsigned bit7 : 1;
  76.  
  77. };
  78.  
  79. union u {
  80.     unsigned char status;
  81.     bits b;
  82. };
  83.  
  84. struct time {
  85.     boost::posix_time::ptime start_time, end_time;
  86.     int posledni_hodnota;
  87.  
  88. } times[8];
  89.  
  90. void delka_impulsu(int CAS) {
  91.  
  92.     u STAV, ps;
  93.  
  94.     ps.status = 0;
  95.  
  96.     while (true)
  97.     {
  98.         ptime current_time = microsec_clock::local_time();
  99.  
  100.         ICO300_get_DI(&STAV.status);
  101.  
  102.         if (STAV.b.bit0 != ps.b.bit0)
  103.         {
  104.             if (STAV.b.bit0 == 1)
  105.                 times[0].start_time = current_time;
  106.             else
  107.                 times[0].end_time = current_time;
  108.         }
  109.         wait(CAS);
  110.     }
  111. }
  112.  
  113.  
  114. int main() {
  115.     char c;
  116.     ICO300_set_DIO_mode(0xff);                        //Vsechny piny jsou vstupni
  117.     int b;
  118.     cout << "Zadejte cas v [ms]: " << endl;
  119.     cin >> b;
  120.  
  121.     std::thread t{threadf, b};                            //Inicializace vlakna
  122.  
  123.     cout << "Pro odpojeni vlakna stisknete lib. klavesu od 'Q'" << endl;
  124.     cin >> c;
  125.  
  126.     if (c != 'Q')
  127.     {
  128.         std::lock_guard<std::mutex> lck(m_mutex);   // entering critical section
  129.         endflag = true;
  130.     }
  131.  
  132.     t.join();
  133.  
  134.     delka_impulsu(b);
  135.  
  136.     ptime t1 = times[0].start_time;
  137.     ptime t2 = times[0].end_time;
  138.     time_duration diff = t1 - t2;
  139.  
  140.     cout << "Delka pulsu je: " << diff << endl;
  141.  
  142.     return 0;
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement