Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added hs.serial:rts() and hs.serial:dtr() #3314

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 97 additions & 5 deletions extensions/serial/libserial.m
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ @interface HSSerialPort : NSObject <ORSSerialPortDelegate>
@property BOOL usesDTRDSRFlowControl;
@property BOOL usesDCDOutputFlowControl;
@property BOOL allowsNonStandardBaudRates;
@property BOOL rts;
@property BOOL dtr;

@end

Expand Down Expand Up @@ -134,6 +136,8 @@ - (instancetype)init
_usesDTRDSRFlowControl = NO;
_usesDCDOutputFlowControl = NO;
_allowsNonStandardBaudRates = NO;
_rts = NO;
_dtr = NO;
}
return self;
}
Expand Down Expand Up @@ -230,7 +234,7 @@ - (void)serialPort:(ORSSerialPort *)serialPort didReceiveData:(NSData *)data

- (void)serialPort:(ORSSerialPort *)serialPort didReceivePacket:(NSData *)packetData matchingDescriptor:(ORSSerialPacketDescriptor *)descriptor
{
// TODO: Impliment `ORSSerialPacketDescriptor` functionality
// TODO: Implement `ORSSerialPacketDescriptor` functionality
}

- (void)serialPortWasRemovedFromSystem:(ORSSerialPort *)serialPort;
Expand Down Expand Up @@ -409,6 +413,8 @@ - (BOOL)open
self.serialPort.usesRTSCTSFlowControl = self.usesRTSCTSFlowControl;
self.serialPort.usesDTRDSRFlowControl = self.usesDTRDSRFlowControl;
self.serialPort.usesDCDOutputFlowControl = self.usesDCDOutputFlowControl;
self.serialPort.RTS = self.rts;
self.serialPort.DTR = self.dtr;
[self.serialPort open];
}
return self.serialPort && self.serialPort.isOpen;
Expand Down Expand Up @@ -447,6 +453,22 @@ - (void)changeNumberOfDataBits:(NSUInteger)numberOfDataBits
}
}

- (void)changeRTS:(BOOL)rtsEnabled
{
self.rts = rtsEnabled;
if ([self isOpen]) {
self.serialPort.RTS = rtsEnabled;
}
}

- (void)changeDTR:(BOOL)dtrEnabled
{
self.dtr = dtrEnabled;
if ([self isOpen]) {
self.serialPort.DTR = dtrEnabled;
}
}

- (void)changeUsesRTSCTSFlowControl:(BOOL)usesRTSCTSFlowControl
{
self.usesRTSCTSFlowControl = usesRTSCTSFlowControl;
Expand Down Expand Up @@ -872,10 +894,12 @@ static int serial_parity(lua_State *L) {
///
/// Returns:
/// * If a value is specified, then this method returns the serial port object. Otherwise this method returns a boolean.
///
/// Notes:
/// * The default value is `false`.
static int serial_usesDCDOutputFlowControl(lua_State *L) {
LuaSkin *skin = [LuaSkin sharedWithState:L];
[skin checkArgs:LS_TUSERDATA, USERDATA_TAG, LS_TNUMBER | LS_TOPTIONAL, LS_TBREAK];
[skin checkArgs:LS_TUSERDATA, USERDATA_TAG, LS_TBOOLEAN | LS_TOPTIONAL, LS_TBREAK];
HSSerialPort *serialPort = [skin toNSObjectAtIndex:1];
if (lua_gettop(L) == 1) {
// Get:
Expand All @@ -898,10 +922,12 @@ static int serial_usesDCDOutputFlowControl(lua_State *L) {
///
/// Returns:
/// * If a value is specified, then this method returns the serial port object. Otherwise this method returns a boolean.
///
/// Notes:
/// * The default value is `false`.
static int serial_usesDTRDSRFlowControl(lua_State *L) {
LuaSkin *skin = [LuaSkin sharedWithState:L];
[skin checkArgs:LS_TUSERDATA, USERDATA_TAG, LS_TNUMBER | LS_TOPTIONAL, LS_TBREAK];
[skin checkArgs:LS_TUSERDATA, USERDATA_TAG, LS_TBOOLEAN | LS_TOPTIONAL, LS_TBREAK];
HSSerialPort *serialPort = [skin toNSObjectAtIndex:1];
if (lua_gettop(L) == 1) {
// Get:
Expand All @@ -924,10 +950,12 @@ static int serial_usesDTRDSRFlowControl(lua_State *L) {
///
/// Returns:
/// * If a value is specified, then this method returns the serial port object. Otherwise this method returns a boolean.
///
/// Notes:
/// * The default value is `false`.
static int serial_usesRTSCTSFlowControl(lua_State *L) {
LuaSkin *skin = [LuaSkin sharedWithState:L];
[skin checkArgs:LS_TUSERDATA, USERDATA_TAG, LS_TNUMBER | LS_TOPTIONAL, LS_TBREAK];
[skin checkArgs:LS_TUSERDATA, USERDATA_TAG, LS_TBOOLEAN | LS_TOPTIONAL, LS_TBREAK];
HSSerialPort *serialPort = [skin toNSObjectAtIndex:1];
if (lua_gettop(L) == 1) {
// Get:
Expand All @@ -941,6 +969,64 @@ static int serial_usesRTSCTSFlowControl(lua_State *L) {
return 1;
}

/// hs.serial:dtr([value]) -> boolean | serialPortObject
/// Method
/// Gets or sets the state of the serial port's DTR (Data Terminal Ready) pin.
///
/// Parameters:
/// * value - An optional boolean.
///
/// Returns:
/// * If a value is specified, then this method returns the serial port object. Otherwise this method returns a boolean.
///
/// Notes:
/// * The default value is `false`.
/// * Setting this to `true` is most likely required for Arduino devices prior to opening the serial port.
static int serial_dtr(lua_State *L) {
LuaSkin *skin = [LuaSkin sharedWithState:L];
[skin checkArgs:LS_TUSERDATA, USERDATA_TAG, LS_TBOOLEAN | LS_TOPTIONAL, LS_TBREAK];
HSSerialPort *serialPort = [skin toNSObjectAtIndex:1];
if (lua_gettop(L) == 1) {
// Get:
BOOL dtrEnabled = serialPort.dtr;
lua_pushboolean(L, dtrEnabled);
} else {
BOOL dtrEnabled = lua_toboolean(L, 2);
[serialPort changeDTR:dtrEnabled];
lua_pushvalue(L, 1);
}
return 1;
}

/// hs.serial:rts([value]) -> boolean | serialPortObject
/// Method
/// Gets or sets the state of the serial port's RTS (Request to Send) pin.
///
/// Parameters:
/// * value - An optional boolean.
///
/// Returns:
/// * If a value is specified, then this method returns the serial port object. Otherwise this method returns a boolean.
///
/// Notes:
/// * The default value is `false`.
/// * Setting this to `true` is most likely required for Arduino devices prior to opening the serial port.
static int serial_rts(lua_State *L) {
LuaSkin *skin = [LuaSkin sharedWithState:L];
[skin checkArgs:LS_TUSERDATA, USERDATA_TAG, LS_TBOOLEAN | LS_TOPTIONAL, LS_TBREAK];
HSSerialPort *serialPort = [skin toNSObjectAtIndex:1];
if (lua_gettop(L) == 1) {
// Get:
BOOL rtsEnabled = serialPort.rts;
lua_pushboolean(L, rtsEnabled);
} else {
BOOL rtsEnabled = lua_toboolean(L, 2);
[serialPort changeRTS:rtsEnabled];
lua_pushvalue(L, 1);
}
return 1;
}

/// hs.serial:shouldEchoReceivedData([value]) -> boolean | serialPortObject
/// Method
/// Gets or sets whether the port should echo received data.
Expand All @@ -950,10 +1036,12 @@ static int serial_usesRTSCTSFlowControl(lua_State *L) {
///
/// Returns:
/// * If a value is specified, then this method returns the serial port object. Otherwise this method returns a boolean.
///
/// Notes:
/// * The default value is `false`.
static int serial_shouldEchoReceivedData(lua_State *L) {
LuaSkin *skin = [LuaSkin sharedWithState:L];
[skin checkArgs:LS_TUSERDATA, USERDATA_TAG, LS_TNUMBER | LS_TOPTIONAL, LS_TBREAK];
[skin checkArgs:LS_TUSERDATA, USERDATA_TAG, LS_TBOOLEAN | LS_TOPTIONAL, LS_TBREAK];
HSSerialPort *serialPort = [skin toNSObjectAtIndex:1];
if (lua_gettop(L) == 1) {
// Get:
Expand All @@ -976,6 +1064,8 @@ static int serial_shouldEchoReceivedData(lua_State *L) {
///
/// Returns:
/// * If a value is specified, then this method returns the serial port object. Otherwise this method returns the number of stop bits as a number.
///
/// Notes:
/// * The default value is 1.
static int serial_numberOfStopBits(lua_State *L) {
LuaSkin *skin = [LuaSkin sharedWithState:L];
Expand Down Expand Up @@ -1227,6 +1317,8 @@ static int meta_gc(lua_State* L) {
{"shouldEchoReceivedData", serial_shouldEchoReceivedData},
{"usesRTSCTSFlowControl", serial_usesRTSCTSFlowControl},
{"usesDTRDSRFlowControl", serial_usesDTRDSRFlowControl},
{"rts", serial_rts},
{"dtr", serial_dtr},
{"usesDCDOutputFlowControl", serial_usesDCDOutputFlowControl},
{"__tostring", userdata_tostring},
{"__eq", userdata_eq},
Expand Down