Changeset 8183 for lang/cplusplus

Show
Ignore:
Timestamp:
03/20/08 15:33:59 (5 years ago)
Author:
mootoh
Message:

add continuos read from dio.

Location:
lang/cplusplus/gainer++
Files:
1 added
4 modified

Legend:

Unmodified
Added
Removed
  • lang/cplusplus/gainer++/Makefile

    r8110 r8183  
    1111        $(CXX) -dynamiclib -o $@ $^ 
    1212 
    13 gainer-led: gainer-led.o 
     13gainer-led: gainer-led.o $(TARGET_LIB) 
    1414        $(CXX) -o $@ $^ $(LIBS) 
    1515 
    16 gainer-button: gainer-button.o 
     16gainer-button: gainer-button.o $(TARGET_LIB) 
    1717        $(CXX) -o $@ $^ $(LIBS) 
     18 
     19gainer-cdio: gainer-cdio.o $(TARGET_LIB) 
     20        $(CXX) -o $@ $^ $(LIBS) 
     21 
    1822 
    1923clean: 
  • lang/cplusplus/gainer++/gainer-button.cc

    r8112 r8183  
    4242    std::cerr << "process_next_event" << std::endl; 
    4343    //gainer->process_next_event(); 
    44     gainer->peek_analog_inputs(); 
    45     //gainer->peek_digital_inputs(); 
     44    //gainer->peek_analog_inputs(); 
     45    gainer->peek_digital_inputs(); 
    4646    usleep(100000); 
    4747    /* 
  • lang/cplusplus/gainer++/gainer.cc

    r8112 r8183  
    55#include <fcntl.h> 
    66#include <sys/select.h> 
     7#include <pthread.h> 
    78#include "gainer.h" 
    89#include <iomanip> 
     
    2223#endif // DEBUG 
    2324 
    24 namespace Gainer { 
    25  
    26 Gainer::Gainer(const std::string &path, int config) : 
    27     led_(false) 
    28   , config_(config) 
    29   , on_pressed(NULL) 
    30   , on_released(NULL) 
    31 { 
    32   ABORT_UNLESS(-1 != (io_ = open(path.c_str(), O_RDWR))); 
    33   ABORT_UNLESS(-1 != fsync(io_)); 
    34   setup_port(); 
    35   reboot(); 
    36   set_configuration(config_); 
    37 } 
    38  
    39 Gainer::~Gainer() { 
    40   close(io_); 
    41 } 
    42  
    43 void Gainer::set_led(bool flag) { 
    44   command(flag ? "h" : "l"); 
    45 } 
    46  
    47 void Gainer::setup_port() { 
    48   struct termios term; 
    49   ABORT_UNLESS(0 == tcgetattr(io_, &term)); 
    50   ABORT_UNLESS(0 == cfsetispeed(&term, B38400)); 
    51   ABORT_UNLESS(0 == cfsetospeed(&term, B38400)); 
    52   term.c_cflag |= CS8; 
    53   ABORT_UNLESS(0 == tcsetattr(io_, TCSANOW, &term)); 
    54 } 
    55  
    56 void Gainer::reboot() { 
    57   command("Q", 1); 
    58 } 
    59  
    60 void Gainer::set_configuration(int number) { 
    61   if (1 > number or number > 7) 
    62     throw GainerInternal::Exception(); 
    63  
    64   config_ = number; 
    65   std::stringstream ss(""); 
    66   ss << "KONFIGURATION_" << number; 
    67   command(ss.str(), 1); 
    68  
    69   // create analog inputs 
    70   for (int i(0); i<CONFIG[config_][AIN]; i++) { 
    71     analog_inputs.push_back(0); 
    72   } 
    73  
    74   // create digital inputs 
    75   for (int i(0); i<CONFIG[config_][DIN]; i++) { 
    76     digital_inputs.push_back(false); 
    77   } 
    78 } 
    79  
    80 // void Gainer::set_matrix(ary) 
    81  
    82  
    83 void Gainer::peek_digital_inputs() { 
    84   command("R"); 
    85 } 
    86  
    87 void Gainer::peek_analog_inputs() { 
    88   command("I"); 
    89 } 
    90  
    91 void Gainer::set_digital_output(int n) { 
    92   std::stringstream ss("D"); 
    93   for (int i(0); i< CONFIG[config_][DOUT]-1; i++) { 
    94     ss << ' '; 
    95   } 
    96   ss << n; 
    97   command(ss.str()); 
    98 } 
    99  
    100 void Gainer::command(const std::string &cmd, int wait) { 
    101   command_send(cmd + '*'); 
    102   process_next_event(wait); 
    103 } 
    104  
    105 void Gainer::process_next_event(int wait) { 
    106   //std::cerr << "process_next_event" << std::endl; 
    107   std::string reply(next_event()); 
    108   sleep(wait); 
    109   process_event(reply); 
    110 } 
    111  
    112 void Gainer::command_send(const std::string &cmd) { 
    113   write(io_, cmd.c_str(), cmd.size()); 
    114 } 
    115  
    116 std::string Gainer::next_event() { 
    117   while (true) { 
    118     std::cerr << "next_event..." << std::endl; 
     25void *Gainer::receiver(void *arg) { 
     26  Gainer *self(reinterpret_cast<Gainer *>(arg)); 
     27 
     28  struct timeval timeout; 
     29  timeout.tv_sec  = 1; 
     30  timeout.tv_usec = 0; 
     31 
     32  while (not self->end_) { 
     33    DEBUG_PRINT("next_event..."); 
     34 
    11935    fd_set fds; 
    12036    FD_ZERO(&fds); 
    121     FD_SET(io_, &fds); 
    122  
    123     struct timeval timeout; 
    124     timeout.tv_sec = 10; 
    125     timeout.tv_usec = 0; 
    126  
    127     int ret(select(io_+1, &fds, NULL, NULL, &timeout)); 
     37    FD_SET(self->io_, &fds); 
     38 
     39    int ret(select(self->io_+1, &fds, NULL, NULL, &timeout)); 
    12840    if (0 != ret) { 
     41      //DEBUG_PRINT("receiver: selected"); 
    12942      const size_t SIZE(80); 
    13043      char buf[SIZE]; 
    131       read(io_, buf, SIZE); 
    132       return buf; 
    133     } 
    134   } 
    135   return ""; 
     44      read(self->io_, buf, SIZE); 
     45      std::string s(buf); 
     46 
     47      self->process_event(s); 
     48    } 
     49  } 
     50  return NULL; 
    13651} 
    13752 
    138 void Gainer::process_event(std::string &event) { 
    139   DEBUG_PRINT("event: " << event); 
    140   switch(event[0]) { 
    141     case '!': // something wrong 
     53namespace Gainer { 
     54  Gainer::Gainer(const std::string &path, int config) : 
     55    led_(false) 
     56    , config_(config) 
     57    , end_(false) 
     58    , on_pressed(NULL) 
     59    , on_released(NULL) 
     60  { 
     61    ABORT_UNLESS(-1 != (io_ = open(path.c_str(), O_RDWR))); 
     62    ABORT_UNLESS(-1 != fsync(io_)); 
     63    setup_port(); 
     64    pthread_create(&thread_, NULL, receiver, this); 
     65    reboot(); 
     66    set_configuration(config_); 
     67  } 
     68 
     69  Gainer::~Gainer() { 
     70    end_ = true; 
     71    pthread_join(thread_, NULL); 
     72    close(io_); 
     73  } 
     74 
     75  void Gainer::setup_port() { 
     76    struct termios term; 
     77    ABORT_UNLESS(0 == tcgetattr(io_, &term)); 
     78    ABORT_UNLESS(0 == cfsetispeed(&term, B38400)); 
     79    ABORT_UNLESS(0 == cfsetospeed(&term, B38400)); 
     80    term.c_cflag |= CS8; 
     81    ABORT_UNLESS(0 == tcsetattr(io_, TCSANOW, &term)); 
     82  } 
     83 
     84  void Gainer::set_configuration(int number) { 
     85    if (1 > number or number > 7) 
    14286      throw GainerInternal::Exception(); 
    143     case 'h': // led on 
    144       led_ = true; 
    145       break; 
    146     case 'l': // led off 
    147       led_ = false; 
    148       break; 
    149     case 'N': // button pressed 
    150       on_pressed(); 
    151       break; 
    152     case 'F': // button released 
    153       on_released(); 
    154       break; 
    155     case 'I': { // analog_input 
    156       std::string::size_type ast(event.find('*')); 
    157       std::string s(event.substr(1, ast-1)); 
    158       sscanf(s.c_str(), "%02X%02X%02X%02X*", 
    159         &analog_inputs[0], &analog_inputs[1], 
    160         &analog_inputs[2], &analog_inputs[3]); 
    161       break; 
    162     } 
    163     case 'R': { // digital input 
    164       std::string::size_type ast(event.find('*')); 
    165       std::stringstream ss(event.substr(1, ast-1)); 
    166       int num; 
    167       ss >> std::hex >> num; 
    168       for (size_t i(0); i<digital_inputs.size(); i++) 
    169         digital_inputs[i] = num & (1<<i); 
    170       break; 
    171     } 
    172     default: 
    173       break; 
    174   } 
    175 } 
    176  
    177 const int Gainer::CONFIG[][4] = { 
    178   // N_AIN, N_DIN, N_AOUT, N_DOUT 
    179   {0,},         // dummy 
    180   {4, 4, 4, 4}, // 1 
    181   {8, 0, 4, 4}, // 2 
    182   {4, 4, 8, 0}, // 3 
    183   {8, 0, 8, 0}, // 4 
    184   {0,16, 0, 0}, // 5 
    185   {0, 0, 0,16}, // 6 
    186   {0,}          // 7 for matrix LED 
    187 }; 
     87 
     88    config_ = number; 
     89    std::stringstream ss(""); 
     90    ss << "KONFIGURATION_" << number; 
     91    command(ss.str(), 1); 
     92 
     93    for (int i(0); i<CONFIG[config_][AIN]; i++) // create analog inputs 
     94      analog_inputs.push_back(0); 
     95 
     96    for (int i(0); i<CONFIG[config_][DIN]; i++) // create digital inputs 
     97      digital_inputs.push_back(false); 
     98  } 
     99 
     100  // void Gainer::set_matrix(ary) // TODO 
     101 
     102  void Gainer::set_led(bool flag)    { command(flag ? "h" : "l"); } 
     103  void Gainer::reboot()              { command("Q", 1); } 
     104  void Gainer::peek_digital_inputs() { command("R"); } 
     105  void Gainer::peek_analog_inputs()  { command("I"); } 
     106  void Gainer::continuous_digital_inputs() { command("r"); } 
     107  void Gainer::exit_continuos() { command("E"); } 
     108 
     109  void Gainer::set_digital_output(int n) { 
     110    std::stringstream ss("D"); 
     111    for (int i(0); i< CONFIG[config_][DOUT]-1; i++) { 
     112      ss << ' '; 
     113    } 
     114    ss << n; 
     115    command(ss.str()); 
     116  } 
     117 
     118  void Gainer::command(const std::string &cmd, int wait) { 
     119    command_send(cmd + '*'); 
     120    process_next_event(wait); 
     121  } 
     122 
     123  void Gainer::process_next_event(int wait) { 
     124    //std::cerr << "process_next_event" << std::endl; 
     125    //std::string reply(next_event()); 
     126    sleep(wait); 
     127    //process_event(reply); 
     128  } 
     129 
     130  void Gainer::command_send(const std::string &cmd) { 
     131    write(io_, cmd.c_str(), cmd.size()); 
     132  } 
     133 
     134  std::string Gainer::next_event() { 
     135    while (true) { 
     136      std::cerr << "next_event..." << std::endl; 
     137      fd_set fds; 
     138      FD_ZERO(&fds); 
     139      FD_SET(io_, &fds); 
     140 
     141      struct timeval timeout; 
     142      timeout.tv_sec = 1; 
     143      timeout.tv_usec = 0; 
     144 
     145      int ret(select(io_+1, &fds, NULL, NULL, &timeout)); 
     146      if (0 != ret) { 
     147        const size_t SIZE(80); 
     148        char buf[SIZE]; 
     149        read(io_, buf, SIZE); 
     150        return buf; 
     151      } 
     152    } 
     153    return ""; 
     154  } 
     155 
     156  void Gainer::process_event(std::string &event) { 
     157    DEBUG_PRINT("event: " << event); 
     158    switch(event[0]) { 
     159      case '!': // something wrong 
     160        throw GainerInternal::Exception(); 
     161      case 'h': // led on 
     162        led_ = true; 
     163        break; 
     164      case 'l': // led off 
     165        led_ = false; 
     166        break; 
     167      case 'N': // button pressed 
     168        on_pressed(); 
     169        break; 
     170      case 'F': // button released 
     171        on_released(); 
     172        break; 
     173      case 'I': { // analog_input 
     174                  std::string::size_type ast(event.find('*')); 
     175                  std::string s(event.substr(1, ast-1)); 
     176                  sscanf(s.c_str(), "%02X%02X%02X%02X*", 
     177                      &analog_inputs[0], &analog_inputs[1], 
     178                      &analog_inputs[2], &analog_inputs[3]); 
     179                  break; 
     180                } 
     181      case 'R': { // peek digital input 
     182                  std::string::size_type ast(event.find('*')); 
     183                  std::stringstream ss(event.substr(1, ast-1)); 
     184                  int num; 
     185                  ss >> std::hex >> num; 
     186                  for (size_t i(0); i<digital_inputs.size(); i++) 
     187                    digital_inputs[i] = num & (1<<i); 
     188                  break; 
     189                } 
     190      case 'r': { // continuous digital input 
     191                  std::string::size_type ast(event.find('*')); 
     192                  std::stringstream ss(event.substr(1, ast-1)); 
     193                  int num; 
     194                  ss >> std::hex >> num; 
     195                  for (size_t i(0); i<digital_inputs.size(); i++) 
     196                    digital_inputs[i] = num & (1<<i); 
     197                  break; 
     198                } 
     199      default: 
     200                break; 
     201    } 
     202  } 
     203 
     204  const int Gainer::CONFIG[][4] = { 
     205    // N_AIN, N_DIN, N_AOUT, N_DOUT 
     206    {0,},         // dummy 
     207    {4, 4, 4, 4}, // 1 
     208    {8, 0, 4, 4}, // 2 
     209    {4, 4, 8, 0}, // 3 
     210    {8, 0, 8, 0}, // 4 
     211    {0,16, 0, 0}, // 5 
     212    {0, 0, 0,16}, // 6 
     213    {0,}          // 7 for matrix LED 
     214  }; 
    188215 
    189216} // Gainer 
  • lang/cplusplus/gainer++/gainer.h

    r8110 r8183  
    44#include <string> 
    55#include <vector> 
     6#include <pthread.h> 
    67 
    78class Gainer; 
     
    3536  void peek_analog_inputs();   //! capture all analog inputs 
    3637 
     38  void continuous_digital_inputs(); 
     39 
     40  void exit_continuos(); 
     41 
    3742  void set_digital_output(int n); 
    3843  void set_analog_output(int n); 
     
    5762  std::string next_event(); 
    5863 
     64  pthread_t thread_; 
    5965  int config_; 
    6066  int io_; 
     67  bool end_; 
    6168  callback_t on_pressed, on_released; 
    6269 
     
    6471  static const int CONFIG[][4]; 
    6572  static const int MATRIX_LED_CONFIGURATION = 7; 
     73 
     74  static void *receiver(void *arg); 
    6675}; // Gainer 
    6776