Changeset 8110 for lang/cplusplus
- Timestamp:
- 03/19/08 01:55:15 (5 years ago)
- Location:
- lang/cplusplus/gainer++
- Files:
-
- 5 modified
-
Makefile (modified) (1 diff)
-
gainer-button.cc (modified) (1 diff)
-
gainer-led.cc (modified) (1 diff)
-
gainer.cc (modified) (7 diffs)
-
gainer.h (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
lang/cplusplus/gainer++/Makefile
r8064 r8110 1 1 CXX = c++ 2 CPPFLAGS = -Wall 2 CPPFLAGS = -Wall -DDEBUG 3 3 CXXFLAGS = -g -O0 4 #LDFLAGS = -ltermios5 4 6 gainer-led: gainer-led.o gainer.o 7 $(CXX) -o $@ $^ $(LDFLAGS) 5 LIB = gainer 6 TARGET_LIB = lib$(LIB).dylib 8 7 9 gainer-button: gainer-button.o gainer.o 10 $(CXX) -o $@ $^ $(LDFLAGS) 8 LIBS = -L. -l$(LIB) 9 10 $(TARGET_LIB): gainer.o 11 $(CXX) -dynamiclib -o $@ $^ 12 13 gainer-led: gainer-led.o 14 $(CXX) -o $@ $^ $(LIBS) 15 16 gainer-button: gainer-button.o 17 $(CXX) -o $@ $^ $(LIBS) 11 18 12 19 clean: 13 rm -f *.o 20 rm -f *.o $(TARGET_LIB) -
lang/cplusplus/gainer++/gainer-button.cc
r8064 r8110 8 8 using namespace std; 9 9 10 Gainer *gainer; 11 10 12 static void on_pressed() { 11 13 cout << "pressed" << endl; 14 //gainer->peek_digital_inputs(); 15 //gainer->peek_analog_inputs(); 12 16 } 13 17 14 18 static void on_released() { 15 19 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; 16 30 } 17 31 18 32 int 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); 22 40 23 41 while (true) { 24 42 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; 26 60 } 61 62 delete gainer; 27 63 28 64 return 0; -
lang/cplusplus/gainer++/gainer-led.cc
r8061 r8110 5 5 #include "gainer.h" 6 6 7 using namespace std;8 9 7 int main(int argc, char **argv) { 10 Gainer ::Serialgainer(argv[1]);8 Gainer gainer(argv[1]); 11 9 12 10 gainer.set_led(true); -
lang/cplusplus/gainer++/gainer.cc
r8064 r8110 6 6 #include <sys/select.h> 7 7 #include "gainer.h" 8 #include <cassert> 9 10 #define ABORT_UNLESS(stmt) \ 11 if (!((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 8 23 9 24 namespace Gainer { 10 25 11 Serial::Serial(const std::string &path, int config) :26 Gainer::Gainer(const std::string &path, int config) : 12 27 led_(false) 13 28 , config_(config) … … 15 30 , on_released(NULL) 16 31 { 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_)); 19 34 setup_port(); 20 35 reboot(); … … 22 37 } 23 38 24 void Serial::set_led(bool flag) { 39 Gainer::~Gainer() { 40 close(io_); 41 } 42 43 void Gainer::set_led(bool flag) { 25 44 command(flag ? "h" : "l"); 26 45 } 27 46 28 void Serial::setup_port() {47 void Gainer::setup_port() { 29 48 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)); 33 52 term.c_cflag |= CS8; 34 tcsetattr(io_, TCSANOW, &term);53 ABORT_UNLESS(0 == tcsetattr(io_, TCSANOW, &term)); 35 54 } 36 55 37 void Serial::reboot() {56 void Gainer::reboot() { 38 57 command("Q", 1); 39 58 } 40 59 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); 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); 47 77 } 48 78 } 49 79 50 // void Serial::set_matrix(ary)80 // void Gainer::set_matrix(ary) 51 81 52 82 53 void Serial::peek_digital_input() {83 void Gainer::peek_digital_inputs() { 54 84 command("R"); 55 85 } 56 86 57 void Serial::peek_analog_input() {87 void Gainer::peek_analog_inputs() { 58 88 command("I"); 59 89 } 60 90 61 void Serial::set_digital_outputl(int n) {91 void Gainer::set_digital_output(int n) { 62 92 std::stringstream ss("D"); 63 93 for (int i(0); i< CONFIG[config_][DOUT]-1; i++) { … … 68 98 } 69 99 70 void Serial::command(const std::string &cmd, int wait) {100 void Gainer::command(const std::string &cmd, int wait) { 71 101 command_send(cmd + '*'); 72 102 process_next_event(wait); 73 103 } 74 104 75 void Serial::process_next_event(int wait) {105 void Gainer::process_next_event(int wait) { 76 106 //std::cerr << "process_next_event" << std::endl; 77 107 std::string reply(next_event()); … … 80 110 } 81 111 82 void Serial::command_send(const std::string &cmd) {112 void Gainer::command_send(const std::string &cmd) { 83 113 write(io_, cmd.c_str(), cmd.size()); 84 114 } 85 115 86 std::string Serial::next_event() {116 std::string Gainer::next_event() { 87 117 while (true) { 88 118 std::cerr << "next_event..." << std::endl; … … 106 136 } 107 137 108 void Serial::process_event(std::string &event) {109 std::cout << "event: " << event << std::endl;138 void Gainer::process_event(std::string &event) { 139 DEBUG_PRINT("event: " << event); 110 140 switch(event[0]) { 111 case '!': 112 throw Exception();113 case 'h': 141 case '!': // something wrong 142 throw GainerInternal::Exception(); 143 case 'h': // led on 114 144 led_ = true; 115 145 break; 116 case 'l': 146 case 'l': // led off 117 147 led_ = false; 118 148 break; 119 case 'N': 149 case 'N': // button pressed 120 150 on_pressed(); 121 151 break; 122 case 'F': 152 case 'F': // button released 123 153 on_released(); 124 154 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 } 128 163 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; 132 173 default: 133 174 break; … … 135 176 } 136 177 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} 178 const 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 145 188 }; 146 189 -
lang/cplusplus/gainer++/gainer.h
r8064 r8110 1 /* 2 * Gainer C++ wrapper 3 */ 1 4 #include <string> 2 5 #include <vector> 3 6 4 /** 5 * Gainer C++ wrapper 6 */ 7 namespace Gainer { 8 class Port; 7 class Gainer; 8 9 namespace GainerInternal { 10 class Exception { 11 }; // Exception 12 } // GainerInternal 9 13 10 14 /** 11 * Serial I/O15 * Gainer Serial I/O. 12 16 */ 13 class Serial{17 class Gainer { 14 18 public: 19 //! event handler calback 15 20 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;19 21 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(); 21 29 22 void set_led(bool flag); 30 void set_led(bool flag); //! set led state (true: on/ false:off) 31 23 32 // 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 27 40 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; 34 49 35 50 private: … … 42 57 std::string next_event(); 43 58 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; 47 62 48 int config_;49 callback_t on_pressed, on_released;50 int io_;51 }; // Serial63 enum pin_t {AIN=0, DIN, AOUT, DOUT}; 64 static const int CONFIG[][4]; 65 static const int MATRIX_LED_CONFIGURATION = 7; 66 }; // Gainer 52 67 53 54 class Port {55 public:56 Port(Serial &b) : instance(b) {}57 58 private:59 Serial &instance;60 }; // Port61 62 class Exception {63 }; // Exception64 } // Gainer
![(please configure the [header_logo] section in trac.ini)](/share/chrome/site/your_project_logo.png)