Changeset 8110 for lang/cplusplus

Show
Ignore:
Timestamp:
03/19/08 01:55:15 (5 years ago)
Author:
mootoh
Message:

lang/cplusplus/gainer++: implementing ain/din.

Location:
lang/cplusplus/gainer++
Files:
5 modified

Legend:

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

    r8064 r8110  
    11CXX = c++ 
    2 CPPFLAGS = -Wall 
     2CPPFLAGS = -Wall -DDEBUG 
    33CXXFLAGS = -g -O0 
    4 #LDFLAGS = -ltermios 
    54 
    6 gainer-led: gainer-led.o gainer.o 
    7         $(CXX) -o $@ $^ $(LDFLAGS) 
     5LIB = gainer 
     6TARGET_LIB = lib$(LIB).dylib 
    87 
    9 gainer-button: gainer-button.o gainer.o 
    10         $(CXX) -o $@ $^ $(LDFLAGS) 
     8LIBS = -L. -l$(LIB) 
     9 
     10$(TARGET_LIB): gainer.o 
     11        $(CXX) -dynamiclib -o $@ $^ 
     12 
     13gainer-led: gainer-led.o 
     14        $(CXX) -o $@ $^ $(LIBS) 
     15 
     16gainer-button: gainer-button.o 
     17        $(CXX) -o $@ $^ $(LIBS) 
    1118 
    1219clean: 
    13         rm -f *.o 
     20        rm -f *.o $(TARGET_LIB) 
  • lang/cplusplus/gainer++/gainer-button.cc

    r8064 r8110  
    88using namespace std; 
    99 
     10Gainer *gainer; 
     11 
    1012static void on_pressed() { 
    1113  cout << "pressed" << endl; 
     14  //gainer->peek_digital_inputs(); 
     15  //gainer->peek_analog_inputs(); 
    1216} 
    1317 
    1418static void on_released() { 
    1519  cout << "released" << endl; 
     20 
     21  cout << "digital : "; 
     22  for (size_t i(0); i<gainer->digital_inputs.size(); i++) { 
     23    cout << gainer->digital_inputs[i] << ' '; 
     24  } cout << endl; 
     25 
     26  cout << "analog : "; 
     27  for (size_t i(0); i<gainer->analog_inputs.size(); i++) { 
     28    cout << gainer->analog_inputs[i] << ' '; 
     29  } cout << endl; 
    1630} 
    1731 
    1832int main(int argc, char **argv) { 
    19   Gainer::Serial gainer(argv[1]); 
    20   gainer.set_on_pressed(on_pressed); 
    21   gainer.set_on_released(on_released); 
     33  if (argc != 2) { 
     34    cerr << "usage: ./gainer-button [device]" << endl; 
     35    exit(1); 
     36  } 
     37  gainer = new Gainer(argv[1], 1); 
     38  gainer->set_on_pressed(on_pressed); 
     39  gainer->set_on_released(on_released); 
    2240 
    2341  while (true) { 
    2442    std::cerr << "process_next_event" << std::endl; 
    25     gainer.process_next_event(); 
     43    //gainer->process_next_event(); 
     44    gainer->peek_analog_inputs(); 
     45    usleep(100000); 
     46    /* 
     47    gainer->peek_analog_inputs(); 
     48    gainer->peek_digital_inputs(); 
     49    */ 
     50 
     51    cout << "A: "; 
     52    for (size_t i(0); i<gainer->analog_inputs.size(); i++) { 
     53      cout << gainer->analog_inputs[i] << ' '; 
     54    } cout << endl; 
     55 
     56    cout << "D: "; 
     57    for (size_t i(0); i<gainer->digital_inputs.size(); i++) { 
     58      cout << gainer->digital_inputs[i] << ' '; 
     59    } cout << endl; 
    2660  } 
     61 
     62  delete gainer; 
    2763 
    2864  return 0; 
  • lang/cplusplus/gainer++/gainer-led.cc

    r8061 r8110  
    55#include "gainer.h" 
    66 
    7 using namespace std; 
    8  
    97int main(int argc, char **argv) { 
    10   Gainer::Serial gainer(argv[1]); 
     8  Gainer gainer(argv[1]); 
    119 
    1210  gainer.set_led(true); 
  • lang/cplusplus/gainer++/gainer.cc

    r8064 r8110  
    66#include <sys/select.h> 
    77#include "gainer.h" 
     8#include <cassert> 
     9 
     10#define ABORT_UNLESS(stmt) \ 
     11if (!((stmt))) { \ 
     12  std::stringstream msg(""); \ 
     13  msg << "failed in L" <<  __LINE__ << ": "; \ 
     14  perror(msg.str().c_str()); \ 
     15  abort(); \ 
     16} 
     17 
     18#ifdef DEBUG 
     19#define DEBUG_PRINT(msg) std::cerr << msg << std::endl; 
     20#else 
     21#define DEBUG_PRINT(msg) ; 
     22#endif // DEBUG 
    823 
    924namespace Gainer { 
    1025 
    11 Serial::Serial(const std::string &path, int config) : 
     26Gainer::Gainer(const std::string &path, int config) : 
    1227    led_(false) 
    1328  , config_(config) 
     
    1530  , on_released(NULL) 
    1631{ 
    17   io_ = open(path.c_str(), O_RDWR); 
    18   fsync(io_); 
     32  ABORT_UNLESS(-1 != (io_ = open(path.c_str(), O_RDWR))); 
     33  ABORT_UNLESS(-1 != fsync(io_)); 
    1934  setup_port(); 
    2035  reboot(); 
     
    2237} 
    2338 
    24 void Serial::set_led(bool flag) { 
     39Gainer::~Gainer() { 
     40  close(io_); 
     41} 
     42 
     43void Gainer::set_led(bool flag) { 
    2544  command(flag ? "h" : "l"); 
    2645} 
    2746 
    28 void Serial::setup_port() { 
     47void Gainer::setup_port() { 
    2948  struct termios term; 
    30   tcgetattr(io_, &term); 
    31   cfsetispeed(&term, B38400); 
    32   cfsetospeed(&term, B38400); 
     49  ABORT_UNLESS(0 == tcgetattr(io_, &term)); 
     50  ABORT_UNLESS(0 == cfsetispeed(&term, B38400)); 
     51  ABORT_UNLESS(0 == cfsetospeed(&term, B38400)); 
    3352  term.c_cflag |= CS8; 
    34   tcsetattr(io_, TCSANOW, &term); 
     53  ABORT_UNLESS(0 == tcsetattr(io_, TCSANOW, &term)); 
    3554} 
    3655 
    37 void Serial::reboot() { 
     56void Gainer::reboot() { 
    3857  command("Q", 1); 
    3958} 
    4059 
    41 void Serial::set_configuration(int number) { 
    42   if (1 <= number and number <= 7) { 
    43     config_ = number; 
    44     std::stringstream ss(""); 
    45     ss << "KONFIGURATION_" << number; 
    46     command(ss.str(), 1); 
     60void 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); 
    4777  } 
    4878} 
    4979 
    50 // void Serial::set_matrix(ary) 
     80// void Gainer::set_matrix(ary) 
    5181 
    5282 
    53 void Serial::peek_digital_input() { 
     83void Gainer::peek_digital_inputs() { 
    5484  command("R"); 
    5585} 
    5686 
    57 void Serial::peek_analog_input() { 
     87void Gainer::peek_analog_inputs() { 
    5888  command("I"); 
    5989} 
    6090 
    61 void Serial::set_digital_outputl(int n) { 
     91void Gainer::set_digital_output(int n) { 
    6292  std::stringstream ss("D"); 
    6393  for (int i(0); i< CONFIG[config_][DOUT]-1; i++) { 
     
    6898} 
    6999 
    70 void Serial::command(const std::string &cmd, int wait) { 
     100void Gainer::command(const std::string &cmd, int wait) { 
    71101  command_send(cmd + '*'); 
    72102  process_next_event(wait); 
    73103} 
    74104 
    75 void Serial::process_next_event(int wait) { 
     105void Gainer::process_next_event(int wait) { 
    76106  //std::cerr << "process_next_event" << std::endl; 
    77107  std::string reply(next_event()); 
     
    80110} 
    81111 
    82 void Serial::command_send(const std::string &cmd) { 
     112void Gainer::command_send(const std::string &cmd) { 
    83113  write(io_, cmd.c_str(), cmd.size()); 
    84114} 
    85115 
    86 std::string Serial::next_event() { 
     116std::string Gainer::next_event() { 
    87117  while (true) { 
    88118    std::cerr << "next_event..." << std::endl; 
     
    106136} 
    107137 
    108 void Serial::process_event(std::string &event) { 
    109   std::cout << "event: " << event << std::endl; 
     138void Gainer::process_event(std::string &event) { 
     139  DEBUG_PRINT("event: " << event); 
    110140  switch(event[0]) { 
    111     case '!': 
    112       throw Exception(); 
    113     case 'h': 
     141    case '!': // something wrong 
     142      throw GainerInternal::Exception(); 
     143    case 'h': // led on 
    114144      led_ = true; 
    115145      break; 
    116     case 'l': 
     146    case 'l': // led off 
    117147      led_ = false; 
    118148      break; 
    119     case 'N': 
     149    case 'N': // button pressed 
    120150      on_pressed(); 
    121151      break; 
    122     case 'F': 
     152    case 'F': // button released 
    123153      on_released(); 
    124154      break; 
    125     case 'I': 
    126       std::cerr << "analog_input: " << event << std::endl; 
    127       // analog_input = ... 
     155    case 'I': // analog_input 
     156      for (int i(1); ; i++) { 
     157        char ch(event[i]); 
     158        if (isdigit(ch) or ('A' <= ch and 'F' >= ch)) 
     159          digital_inputs[i-1] = ch-'0'; 
     160        else 
     161          break; 
     162      } 
    128163      break; 
    129     case 'R': 
    130       std::cerr << "digital_input: " << event << std::endl; 
    131       // digital_input = ... 
     164    case 'R': // digital input 
     165      for (int i(1); ; i++) { 
     166        char ch(event[i]); 
     167        if (isdigit(ch) or ('A' <= ch and 'F' >= ch)) 
     168          digital_inputs[i-1] = ch-'0'; 
     169        else 
     170          break; 
     171      } 
     172      break; 
    132173    default: 
    133174      break; 
     
    135176} 
    136177 
    137 const int Serial::CONFIG[][4] = { 
    138   {0,}, 
    139   {4, 4, 4, 4}, 
    140   {8, 0, 4, 4}, 
    141   {4, 4, 8, 0}, 
    142   {8, 0, 8, 0}, 
    143   {0,16, 0, 0}, 
    144   {0, 0,16, 0} 
     178const int Gainer::CONFIG[][4] = { 
     179  // N_AIN, N_DIN, N_AOUT, N_DOUT 
     180  {0,},         // dummy 
     181  {4, 4, 4, 4}, // 1 
     182  {8, 0, 4, 4}, // 2 
     183  {4, 4, 8, 0}, // 3 
     184  {8, 0, 8, 0}, // 4 
     185  {0,16, 0, 0}, // 5 
     186  {0, 0, 0,16}, // 6 
     187  {0,}          // 7 for matrix LED 
    145188}; 
    146189 
  • lang/cplusplus/gainer++/gainer.h

    r8064 r8110  
     1/* 
     2 * Gainer C++ wrapper 
     3 */ 
    14#include <string> 
    25#include <vector> 
    36 
    4 /** 
    5  * Gainer C++ wrapper 
    6  */ 
    7 namespace Gainer { 
    8 class Port; 
     7class Gainer; 
     8 
     9namespace GainerInternal { 
     10  class Exception { 
     11  }; // Exception 
     12} // GainerInternal 
    913 
    1014/** 
    11  * Serial I/O 
     15 * Gainer Serial I/O. 
    1216 */ 
    13 class Serial { 
     17class Gainer { 
    1418public: 
     19  //! event handler calback 
    1520  typedef void (*callback_t)(void); 
    16   enum pin_t { AIN=0, DIN, AOUT, DOUT }; 
    17   static const int CONFIG[][4]; 
    18   static const int MATRIX_LED_CONFIGURATION = 7; 
    1921 
    20   Serial(const std::string &path, int config=1); 
     22  /** 
     23   * @brief setup Gainer 
     24   * @param path  device path (/dev/cu.usbserial*) 
     25   * @param config configutation mode (1-7) 
     26   */ 
     27  Gainer(const std::string &path, int config=1); 
     28  ~Gainer(); 
    2129 
    22   void set_led(bool flag); 
     30  void set_led(bool flag); //! set led state (true: on/ false:off) 
     31 
    2332  // void set_matrix(ary); 
    24   void peek_digital_input(); 
    25   void peek_analog_input(); 
    26   void set_digital_outputl(int n); 
     33 
     34  void peek_digital_inputs(); //! capture all digital inputs 
     35  void peek_analog_inputs();   //! capture all analog inputs 
     36 
     37  void set_digital_output(int n); 
     38  void set_analog_output(int n); 
     39 
    2740  void process_next_event(int wait = 0); 
    28   void set_on_pressed(callback_t funcp) { 
    29     on_pressed = funcp; 
    30   } 
    31   void set_on_released(callback_t funcp) { 
    32     on_released = funcp; 
    33   } 
     41 
     42  void set_on_pressed(callback_t funcp) { on_pressed = funcp; } 
     43  void set_on_released(callback_t funcp) { on_released = funcp; } 
     44 
     45  // properties 
     46  bool led_; 
     47  std::vector<int> analog_inputs; 
     48  std::vector<bool> digital_inputs; 
    3449 
    3550private: 
     
    4257  std::string next_event(); 
    4358 
    44   bool led_; 
    45   std::vector<Port> analog_inputs, digital_inputs; 
    46   std::vector<Port> analog_outputs, digital_outputs; 
     59  int config_; 
     60  int io_; 
     61  callback_t on_pressed, on_released; 
    4762 
    48   int config_; 
    49   callback_t on_pressed, on_released; 
    50   int io_; 
    51 }; // Serial 
     63  enum pin_t {AIN=0, DIN, AOUT, DOUT}; 
     64  static const int CONFIG[][4]; 
     65  static const int MATRIX_LED_CONFIGURATION = 7; 
     66}; // Gainer 
    5267 
    53  
    54 class Port { 
    55 public: 
    56   Port(Serial &b) : instance(b) {} 
    57  
    58 private: 
    59   Serial &instance; 
    60 }; // Port 
    61  
    62 class Exception { 
    63 }; // Exception 
    64 } // Gainer