-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
282 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
/* | ||
* macrodevice-serial.cpp | ||
* | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation; either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, | ||
* MA 02110-1301, USA. | ||
* | ||
*/ | ||
|
||
#include "macrodevice-serial.h" | ||
|
||
/** | ||
* @copydoc macrodevice::device_serial::load_settings | ||
*/ | ||
int macrodevice::device_serial::load_settings( const std::map< std::string, std::string > &settings ) | ||
{ | ||
|
||
// read settings | ||
try | ||
{ | ||
m_port_path = settings.at( "port" ); | ||
|
||
} | ||
catch( std::exception &e ) | ||
{ | ||
return 1; | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
/** | ||
* @copydoc macrodevice::device_serial::open_device | ||
*/ | ||
int macrodevice::device_serial::open_device() | ||
{ | ||
// open eventfile | ||
m_filedesc = open( m_port_path.c_str(), O_RDONLY|O_NOCTTY|O_SYNC ); | ||
if( m_filedesc < 0 ) | ||
{ | ||
return m_filedesc; | ||
} | ||
|
||
// set up polling | ||
m_pollfd[0].fd = m_filedesc; | ||
m_pollfd[0].events = POLLIN; | ||
|
||
return 0; | ||
} | ||
|
||
/** | ||
* @copydoc macrodevice::device_serial::close_device | ||
*/ | ||
int macrodevice::device_serial::close_device() | ||
{ | ||
// close the serial port | ||
close( m_filedesc ); | ||
|
||
return 0; | ||
} | ||
|
||
/** | ||
* @copydoc macrodevice::device_serial::wait_for_event | ||
*/ | ||
int macrodevice::device_serial::wait_for_event( std::vector< std::string > &event ) | ||
{ | ||
|
||
std::string received_message = ""; | ||
char received_char[1]; | ||
int num_received = 0; | ||
|
||
// wait for change in /dev/input/event* (no timeout) | ||
num_received = read( m_filedesc, received_char, 1 ); | ||
if( num_received == 0 ) // read single byte | ||
{ | ||
// poll if no byte received | ||
if( poll( m_pollfd, 1, -1 ) < 0 ) | ||
{ | ||
return 1; | ||
} | ||
} | ||
else if( num_received == 1 ) // single byte received | ||
{ | ||
received_message.push_back( received_char[0] ); | ||
} | ||
else | ||
{ | ||
return -1; // read failure | ||
} | ||
|
||
event.clear(); | ||
|
||
// get whole message | ||
while( ( num_received = read( m_filedesc, received_char, 1 ) ) == 1 ) // while receiving bytes | ||
{ | ||
if( received_char[0] == '\n' ) // break if newline is received | ||
{ | ||
break; | ||
} | ||
|
||
received_message.push_back( received_char[0] ); | ||
} | ||
|
||
// if read failure | ||
if( num_received < 0 ) | ||
{ | ||
return 1; | ||
} | ||
|
||
event.push_back( received_message ); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
* macrodevice-serial.h | ||
* | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation; either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, | ||
* MA 02110-1301, USA. | ||
* | ||
*/ | ||
|
||
/// Header guard | ||
#ifndef MACRODEVICE_SERIAL | ||
#define MACRODEVICE_SERIAL | ||
|
||
#include <vector> | ||
#include <map> | ||
#include <string> | ||
#include <exception> | ||
|
||
#include <sys/types.h> // for open() | ||
#include <sys/stat.h> // for open() | ||
#include <fcntl.h> // for open() | ||
#include <poll.h> | ||
#include <unistd.h> // for read() | ||
|
||
#include "helpers.h" | ||
|
||
namespace macrodevice | ||
{ | ||
class device_serial; | ||
} | ||
|
||
/** | ||
* The class for the serial backend | ||
*/ | ||
class macrodevice::device_serial | ||
{ | ||
|
||
private: | ||
|
||
/// path for the serial port | ||
std::string m_port_path; | ||
|
||
/// file descriptor for the serial port | ||
int m_filedesc; | ||
|
||
struct pollfd m_pollfd[1]; | ||
|
||
public: | ||
|
||
/** | ||
* Loads the device settings, e.g. serial port | ||
* Valid settings keys are: port | ||
* @param settings A map of settings keys to their values | ||
* @return 0 if successful, >0 if required settings are missing or invalid | ||
*/ | ||
int load_settings( const std::map< std::string, std::string > &settings ); | ||
|
||
/** | ||
* Opens the device specified through load_settings | ||
* @return 0 if successful, !=0 if unsuccessful | ||
* @see load_settings | ||
*/ | ||
int open_device(); | ||
|
||
/** | ||
* Closes the device opened by open_device | ||
* @return 0 if successful, !=0 if unsuccessful | ||
* @see open_device | ||
*/ | ||
int close_device(); | ||
|
||
/** | ||
* Waits for an event, i.e. keypress to occur | ||
* @param event The received event, typically of size == 1 | ||
* @return 0 if successful, !=0 if unsuccessful | ||
*/ | ||
int wait_for_event( std::vector< std::string > &event ); | ||
|
||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* This sends notifications over serial when a pushbutton connected betwen Pin 10 and GND gets pressed or released. | ||
*/ | ||
|
||
const int button_pin = 10; | ||
int button_state_old = HIGH, button_state_new = HIGH; | ||
|
||
void setup() | ||
{ | ||
Serial.begin(9600); | ||
pinMode( button_pin, INPUT_PULLUP ); | ||
} | ||
|
||
void loop() | ||
{ | ||
|
||
// read button state | ||
button_state_old = button_state_new; | ||
button_state_new = digitalRead( button_pin ); | ||
|
||
// button is pressed | ||
if( button_state_old == HIGH && button_state_new == LOW ) | ||
{ | ||
Serial.print( "pressed\n" ); // don't use println as that would end the line with \r\n | ||
} | ||
// button is released | ||
else if( button_state_old == LOW && button_state_new == HIGH ) | ||
{ | ||
Serial.print( "released\n" ); | ||
} | ||
|
||
// wait for debouncing | ||
delay( 10 ); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters