Skip to content

Commit

Permalink
Added 'none' EOL to UART. Fixed logic of EOL
Browse files Browse the repository at this point in the history
  • Loading branch information
NickKibish committed Oct 2, 2023
1 parent 84aa419 commit c2c5405
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 34 deletions.
59 changes: 55 additions & 4 deletions nRF Toolbox/Profiles/UART/Model/UARTCommandModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,57 @@ protocol XMLRepresentable {
var xml: AEXMLElement { get }
}

enum EOL: Codable {
static let CR: UInt8 = 0x0d
static let LF: UInt8 = 0x0a

case cr, lf, crlf, none

var data: Data {
switch self {
case .cr:
Data([Self.CR])
case .lf:
Data([Self.LF])
case .crlf:
Data([Self.CR, Self.LF])
case .none:
Data()
}
}

var name: String {
switch self {
case .cr:
return "CR"
case .lf:
return "LF"
case .crlf:
return "CR+LF"
case .none:
return "None"
}
}

init(data: Data) {
switch data {
case Data([Self.CR]): self = .cr
case Data([Self.LF]): self = .lf
case Data([Self.CR, Self.LF]): self = .crlf
default: self = .none
}
}

init(name: String) {
switch name {
case EOL.cr.name: self = .cr
case EOL.lf.name: self = .lf
case EOL.crlf.name: self = .crlf
default: self = .none
}
}
}

struct EmptyModel: UARTCommandModel, Equatable {
var xml: AEXMLElement {
AEXMLElement(name: "command")
Expand All @@ -75,7 +126,7 @@ struct TextCommand: UARTCommandModel, Equatable {
AEXMLElement(name: "command", value: text, attributes: [
"icon":image.name,
"active":"true",
"eol":"CR",
"eol":eol.name,
"type":"text",
"system_icon":image.systemIcon?.name ?? ""
])
Expand All @@ -84,20 +135,20 @@ struct TextCommand: UARTCommandModel, Equatable {
var title: String { text.split(whereSeparator: \.isNewline).joined() }

var data: Data {
text.data(using: .utf8)!
text.data(using: .utf8)! + eol.data
}

let text: String
let image: CommandImage
var eol: String = "\n"
var eol: EOL = .none
}

struct DataCommand: UARTCommandModel, Equatable {
var xml: AEXMLElement {
AEXMLElement(name: "command", value: data.hexString, attributes: [
"icon":image.name,
"active":"true",
"eol":"CR",
"eol":"none",
"type":"data",
"system_icon":image.systemIcon?.name ?? ""
])
Expand Down
9 changes: 8 additions & 1 deletion nRF Toolbox/Profiles/UART/Model/UARTPreset.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,19 @@ struct UARTPreset {
continue
}

let eol: EOL
if let eolName = node.attributes["eol"] {
eol = EOL(name: eolName)
} else {
eol = .none
}

let image = CommandImage(name: (node.attributes["icon"] ?? ""), modernIcon: node.attributes["system_icon"].map({ModernIcon(name: $0)}))

if let type = node.attributes["type"], type == "data" {
commands.append(DataCommand(data: Data(text.hexa), image: image))
} else {
commands.append(TextCommand(text: text, image: image))
commands.append(TextCommand(text: text, image: image, eol: eol))
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ class UARTNewCommandViewController: UIViewController {
private var command: UARTCommandModel?
private var index: Int

private let CR: UInt8 = 0x0D
private let LF: UInt8 = 0x0A

init(command: UARTCommandModel?, index: Int) {
self.command = command
self.index = index
Expand Down Expand Up @@ -116,10 +119,9 @@ class UARTNewCommandViewController: UIViewController {
let image = CommandImage.allCases[selectedItem]

if typeSegmentControl.selectedSegmentIndex == 0 {
let slices = textView.text.split(omittingEmptySubsequences: false, whereSeparator: \.isNewline)
let text = slices.joined(separator: eolSymbol())
let text = textView.text ?? ""

command = TextCommand(text: text, image: image, eol: self.eolSymbol())
command = TextCommand(text: text, image: image, eol: self.eol())
} else {
command = DataCommand(data: Data(valueTextField.text!.hexa), image: image)
}
Expand Down Expand Up @@ -180,9 +182,9 @@ extension UARTNewCommandViewController {
}
}

private func updateEOLSegment(eol: String) {
let symbols = ["\n", "\r", "\n\r"]
eolSegment.selectedSegmentIndex = symbols.enumerated().first(where: { eol == $0.element })?.offset ?? 0
private func updateEOLSegment(eol: EOL) {
let arr: [EOL] = [.lf, .cr, .crlf, .none]
self.eolSegment.selectedSegmentIndex = arr.firstIndex(of: eol) ?? 3
}

private func setupTextField() {
Expand Down Expand Up @@ -211,13 +213,12 @@ extension UARTNewCommandViewController {
return selectedItem && dataIsReady
}

private func eolSymbol() -> String {
private func eol() -> EOL {
switch eolSegment.selectedSegmentIndex {
case 0: return "\n"
case 1: return "\r"
case 2: return "\n\r"
default:
return ""
case 0: return .lf
case 1: return .cr
case 2: return .crlf
default: return .none
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="16096" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="22154" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES">
<device id="retina6_1" orientation="portrait" appearance="light"/>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="16087"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="22130"/>
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
<capability name="System colors in document resources" minToolsVersion="11.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<customFonts key="customFonts">
Expand Down Expand Up @@ -32,15 +33,15 @@
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<stackView opaque="NO" contentMode="scaleToFill" axis="vertical" spacing="16" translatesAutoresizingMaskIntoConstraints="NO" id="PSb-Wd-33j">
<rect key="frame" x="20" y="60" width="374" height="786"/>
<rect key="frame" x="20" y="64" width="374" height="782"/>
<subviews>
<stackView opaque="NO" contentMode="scaleToFill" axis="vertical" spacing="8" translatesAutoresizingMaskIntoConstraints="NO" id="T7h-tB-R7a">
<rect key="frame" x="0.0" y="0.0" width="374" height="257"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Command value:" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="0uP-1L-yMb">
<rect key="frame" x="0.0" y="0.0" width="374" height="17.5"/>
<fontDescription key="fontDescription" name="GTEestiDisplay-Regular" family="GT Eesti Display" pointSize="15"/>
<color key="textColor" systemColor="secondaryLabelColor" red="0.23529411759999999" green="0.23529411759999999" blue="0.26274509800000001" alpha="0.59999999999999998" colorSpace="custom" customColorSpace="sRGB"/>
<color key="textColor" systemColor="secondaryLabelColor"/>
<nil key="highlightedColor"/>
</label>
<segmentedControl opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="top" segmentControlStyle="plain" selectedSegmentIndex="0" translatesAutoresizingMaskIntoConstraints="NO" id="xej-mh-BNi">
Expand All @@ -64,18 +65,18 @@
</textField>
<textView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" textAlignment="natural" translatesAutoresizingMaskIntoConstraints="NO" id="zML-1J-9HH" customClass="AutoReszableTextView" customModule="nRF_Toolbox" customModuleProvider="target">
<rect key="frame" x="0.0" y="64.5" width="374" height="128"/>
<color key="backgroundColor" systemColor="systemBackgroundColor" cocoaTouchSystemColor="whiteColor"/>
<color key="backgroundColor" systemColor="systemBackgroundColor"/>
<constraints>
<constraint firstAttribute="height" constant="128" placeholder="YES" id="Ynw-7b-cez"/>
</constraints>
<color key="textColor" systemColor="labelColor" cocoaTouchSystemColor="darkTextColor"/>
<color key="textColor" systemColor="labelColor"/>
<fontDescription key="fontDescription" type="system" pointSize="14"/>
<textInputTraits key="textInputTraits" autocapitalizationType="sentences"/>
</textView>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="EOL" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="dfu-h9-JX4">
<rect key="frame" x="0.0" y="200.5" width="374" height="17.5"/>
<fontDescription key="fontDescription" name="GTEestiDisplay-Regular" family="GT Eesti Display" pointSize="15"/>
<color key="textColor" systemColor="secondaryLabelColor" red="0.23529411759999999" green="0.23529411759999999" blue="0.26274509800000001" alpha="0.59999999999999998" colorSpace="custom" customColorSpace="sRGB"/>
<color key="textColor" systemColor="secondaryLabelColor"/>
<nil key="highlightedColor"/>
</label>
<segmentedControl opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="top" segmentControlStyle="plain" selectedSegmentIndex="0" translatesAutoresizingMaskIntoConstraints="NO" id="DOf-x4-x0q">
Expand All @@ -84,22 +85,23 @@
<segment title="LF"/>
<segment title="CR"/>
<segment title="CR+LF"/>
<segment title="None"/>
</segments>
</segmentedControl>
</subviews>
</stackView>
<stackView opaque="NO" contentMode="scaleToFill" axis="vertical" spacing="8" translatesAutoresizingMaskIntoConstraints="NO" id="JFw-qV-DnZ">
<rect key="frame" x="0.0" y="273" width="374" height="467"/>
<rect key="frame" x="0.0" y="273" width="374" height="463"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Chose image for the command:" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="ues-WN-aeO">
<rect key="frame" x="0.0" y="0.0" width="374" height="17.5"/>
<fontDescription key="fontDescription" name="GTEestiDisplay-Regular" family="GT Eesti Display" pointSize="15"/>
<color key="textColor" systemColor="secondaryLabelColor" red="0.23529411759999999" green="0.23529411759999999" blue="0.26274509800000001" alpha="0.59999999999999998" colorSpace="custom" customColorSpace="sRGB"/>
<color key="textColor" systemColor="secondaryLabelColor"/>
<nil key="highlightedColor"/>
</label>
<collectionView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" verticalHuggingPriority="249" verticalCompressionResistancePriority="749" dataMode="none" translatesAutoresizingMaskIntoConstraints="NO" id="jZ7-mp-DGi">
<rect key="frame" x="0.0" y="25.5" width="374" height="441.5"/>
<color key="backgroundColor" systemColor="systemBackgroundColor" cocoaTouchSystemColor="whiteColor"/>
<rect key="frame" x="0.0" y="25.5" width="374" height="437.5"/>
<color key="backgroundColor" systemColor="systemBackgroundColor"/>
<collectionViewFlowLayout key="collectionViewLayout" minimumLineSpacing="10" minimumInteritemSpacing="10" id="1Y8-Xl-e9p">
<size key="itemSize" width="50" height="50"/>
<size key="headerReferenceSize" width="0.0" height="0.0"/>
Expand All @@ -113,15 +115,15 @@
</collectionView>
</subviews>
</stackView>
<button opaque="NO" contentMode="scaleToFill" enabled="NO" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="u5n-VG-gti" customClass="NordicButton" customModule="nRF_Toolbox" customModuleProvider="target">
<rect key="frame" x="0.0" y="756" width="374" height="30"/>
<button opaque="NO" contentMode="scaleToFill" enabled="NO" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="u5n-VG-gti" customClass="NordicButton" customModule="nRF_Toolbox" customModuleProvider="target">
<rect key="frame" x="0.0" y="752" width="374" height="30"/>
<state key="normal" title="Create"/>
<connections>
<action selector="createCommand" destination="-1" eventType="touchUpInside" id="ZF0-GP-H4B"/>
</connections>
</button>
<button hidden="YES" opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="yA1-Ak-ptR" customClass="NordicButton" customModule="nRF_Toolbox" customModuleProvider="target">
<rect key="frame" x="0.0" y="786" width="374" height="0.0"/>
<button hidden="YES" opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="yA1-Ak-ptR" customClass="NordicButton" customModule="nRF_Toolbox" customModuleProvider="target">
<rect key="frame" x="0.0" y="782" width="374" height="0.0"/>
<state key="normal" title="Delete"/>
<connections>
<action selector="deleteBtnPressed" destination="-1" eventType="touchUpInside" id="KFc-vK-1FG"/>
Expand All @@ -130,20 +132,31 @@
</subviews>
</stackView>
</subviews>
<viewLayoutGuide key="safeArea" id="fnl-2z-Ty3"/>
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<constraints>
<constraint firstItem="fnl-2z-Ty3" firstAttribute="trailing" secondItem="PSb-Wd-33j" secondAttribute="trailing" constant="20" id="87d-jq-6Kf"/>
<constraint firstItem="PSb-Wd-33j" firstAttribute="top" secondItem="fnl-2z-Ty3" secondAttribute="top" constant="16" id="mvu-hj-h23"/>
<constraint firstItem="fnl-2z-Ty3" firstAttribute="bottom" secondItem="PSb-Wd-33j" secondAttribute="bottom" constant="16" id="y43-DW-0ho"/>
<constraint firstItem="PSb-Wd-33j" firstAttribute="leading" secondItem="fnl-2z-Ty3" secondAttribute="leading" constant="20" id="yxD-WA-mzY"/>
</constraints>
<viewLayoutGuide key="safeArea" id="fnl-2z-Ty3"/>
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="color" keyPath="borderColor">
<color key="value" systemColor="systemBackgroundColor" cocoaTouchSystemColor="whiteColor"/>
<color key="value" systemColor="systemBackgroundColor"/>
</userDefinedRuntimeAttribute>
</userDefinedRuntimeAttributes>
<point key="canvasLocation" x="139" y="154"/>
</view>
</objects>
<resources>
<systemColor name="labelColor">
<color white="0.0" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
</systemColor>
<systemColor name="secondaryLabelColor">
<color red="0.23529411764705882" green="0.23529411764705882" blue="0.2627450980392157" alpha="0.59999999999999998" colorSpace="custom" customColorSpace="sRGB"/>
</systemColor>
<systemColor name="systemBackgroundColor">
<color white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
</systemColor>
</resources>
</document>

0 comments on commit c2c5405

Please sign in to comment.