Skip to content

Commit

Permalink
Add CI for formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
rauhul committed Dec 12, 2024
1 parent d6f88d3 commit fd013f7
Show file tree
Hide file tree
Showing 13 changed files with 178 additions and 70 deletions.
35 changes: 35 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: Lint

on:
push:
branches: ["main"]
pull_request:
branches: ["main"]

# on:
# pull_request:
# types: [opened, reopened, synchronize]

jobs:
validate_format_config:
name: Validate Format Config
runs-on: ubuntu-latest
steps:
- name: Checkout repo
uses: actions/checkout@v4

- name: Install apt dependencies
run: apt-get -qq update && apt-get -qq -y install curl

- name: Compare against swift-mmio swift-format config
run: |
curl -sL https://raw.githubusercontent.com/apple/swift-mmio/refs/heads/main/SupportingFiles/Tools/swift-format/.swift-format -o .swift-format-mmio
diff .swift-format .swift-format-mmio
soundness:
name: Soundness
uses: swiftlang/github-workflows/.github/workflows/soundness.yml@main
with:
api_breakage_check_enabled: false
docs_check_enabled: false
license_header_check_project_name: "Swift.org"
3 changes: 2 additions & 1 deletion .swift-format
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"lineBreakBeforeControlFlowKeywords" : false,
"lineBreakBeforeEachArgument" : false,
"lineBreakBeforeEachGenericRequirement" : false,
"lineLength" : 160,
"lineLength" : 80,
"maximumBlankLines" : 1,
"multiElementCollectionTrailingCommas" : true,
"noAssignmentInExpressions" : {
Expand Down Expand Up @@ -62,6 +62,7 @@
"UseWhereClausesInForLoops" : false,
"ValidateDocumentationComments" : true
},
"spacesBeforeEndOfLineComments": 2,
"spacesAroundRangeFormationOperators" : false,
"tabWidth" : 2,
"version" : 1
Expand Down
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import PackageDescription
let package = Package(
name: "swift-matter-examples",
products: [
.library(name: "SwiftMatterExamples", targets: ["SwiftMatterExamples"]),
.library(name: "SwiftMatterExamples", targets: ["SwiftMatterExamples"])
],
targets: [
.target(name: "SwiftMatterExamples")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func app_main() {
sleep(1)
led.enabled.toggle()
if led.enabled {
led.color = .hueSaturation(Int.random(in: 0 ..< 360), 100)
led.color = .hueSaturation(Int.random(in: 0..<360), 100)
}
}
}
2 changes: 1 addition & 1 deletion empty-template/main/idf_component.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
dependencies:
espressif/cmake_utilities:
version: 0.*
rules: # will add "optional_component" only when all if clauses are True
rules: # will add "optional_component" only when all if clauses are True
- if: "idf_version >=5.0"
- if: "target in [esp32c2]"
10 changes: 5 additions & 5 deletions led-blink/main/LED.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,22 +53,22 @@ final class LED {
// Hue range is 0 ..< 360.
var hue: Int {
switch self {
case .hueSaturation(let hue, _): return hue
case .temperature: return 0
case .hueSaturation(let hue, _): return hue
case .temperature: return 0
}
}

// Saturation is 0 ... 100.
var saturation: Int {
switch self {
case .hueSaturation(_, let saturation): return saturation
case .temperature: return 0
case .hueSaturation(_, let saturation): return saturation
case .temperature: return 0
}
}
}

var handle: led_driver_handle_t

init() {
var config = led_driver_get_config()
let handle = led_driver_init(&config)
Expand Down
2 changes: 1 addition & 1 deletion led-blink/main/Main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func app_main() {
sleep(1)
led.enabled.toggle()
if led.enabled {
led.color = .hueSaturation(Int.random(in: 0 ..< 360), 100)
led.color = .hueSaturation(Int.random(in: 0..<360), 100)
}
}
}
2 changes: 1 addition & 1 deletion led-blink/main/idf_component.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
dependencies:
espressif/cmake_utilities:
version: 0.*
rules: # will add "optional_component" only when all if clauses are True
rules: # will add "optional_component" only when all if clauses are True
- if: "idf_version >=5.0"
- if: "target in [esp32c2]"
55 changes: 41 additions & 14 deletions smart-light/Matter/Clusters.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ protocol MatterCluster {

extension MatterCluster {
init?(endpoint: some MatterEndpoint, id: UInt32) {
guard let cluster = esp_matter.cluster.get_shim(endpoint.endpoint, id) else {
guard let cluster = esp_matter.cluster.get_shim(endpoint.endpoint, id)
else {
return nil
}
self.init(cluster)
Expand All @@ -35,8 +36,12 @@ struct ClusterID<Cluster: MatterCluster>: RawRepresentable {

static var identify: ClusterID<Identify> { .init(rawValue: 0x0000_0003) }
static var onOff: ClusterID<OnOff> { .init(rawValue: 0x0000_0006) }
static var levelControl: ClusterID<LevelControl> { .init(rawValue: 0x0000_0008) }
static var colorControl: ClusterID<ColorControl> { .init(rawValue: 0x0000_0300) }
static var levelControl: ClusterID<LevelControl> {
.init(rawValue: 0x0000_0008)
}
static var colorControl: ClusterID<ColorControl> {
.init(rawValue: 0x0000_0300)
}
}

struct Cluster: MatterCluster {
Expand Down Expand Up @@ -70,7 +75,9 @@ struct Identify: MatterConcreteCluster {
self.cluster = cluster
}

func attribute<Attribute: MatterAttribute>(_ id: AttributeID<Attribute>) -> Attribute {
func attribute<Attribute: MatterAttribute>(_ id: AttributeID<Attribute>)
-> Attribute
{
Attribute(attribute: esp_matter.attribute.get_shim(cluster, id.rawValue))
}
}
Expand All @@ -91,7 +98,9 @@ struct OnOff: MatterConcreteCluster {
self.cluster = cluster
}

func attribute<Attribute: MatterAttribute>(_ id: AttributeID<Attribute>) -> Attribute {
func attribute<Attribute: MatterAttribute>(_ id: AttributeID<Attribute>)
-> Attribute
{
Attribute(attribute: esp_matter.attribute.get_shim(cluster, id.rawValue))
}

Expand All @@ -105,7 +114,9 @@ struct LevelControl: MatterConcreteCluster {

init(rawValue: UInt32) { self.rawValue = rawValue }

static var currentLevel: AttributeID<CurrentLevel> { .init(rawValue: 0x0000_0000) }
static var currentLevel: AttributeID<CurrentLevel> {
.init(rawValue: 0x0000_0000)
}
}

var cluster: UnsafeMutablePointer<esp_matter.cluster_t>
Expand All @@ -114,7 +125,9 @@ struct LevelControl: MatterConcreteCluster {
self.cluster = cluster
}

func attribute<Attribute: MatterAttribute>(_ id: AttributeID<Attribute>) -> Attribute {
func attribute<Attribute: MatterAttribute>(_ id: AttributeID<Attribute>)
-> Attribute
{
Attribute(attribute: esp_matter.attribute.get_shim(cluster, id.rawValue))
}

Expand All @@ -128,12 +141,20 @@ struct ColorControl: MatterConcreteCluster {

init(rawValue: UInt32) { self.rawValue = rawValue }

static var currentHue: AttributeID<CurrentHue> { .init(rawValue: 0x0000_0000) }
static var currentSaturation: AttributeID<CurrentSaturation> { .init(rawValue: 0x0000_0001) }
static var currentHue: AttributeID<CurrentHue> {
.init(rawValue: 0x0000_0000)
}
static var currentSaturation: AttributeID<CurrentSaturation> {
.init(rawValue: 0x0000_0001)
}
static var currentX: AttributeID<CurrentX> { .init(rawValue: 0x0000_0003) }
static var currentY: AttributeID<CurrentY> { .init(rawValue: 0x0000_0004) }
static var colorTemperatureMireds: AttributeID<ColorTemperatureMireds> { .init(rawValue: 0x0000_0007) }
static var colorMode: AttributeID<ColorMode> { .init(rawValue: 0x0000_0008) }
static var colorTemperatureMireds: AttributeID<ColorTemperatureMireds> {
.init(rawValue: 0x0000_0007)
}
static var colorMode: AttributeID<ColorMode> {
.init(rawValue: 0x0000_0008)
}
}

var cluster: UnsafeMutablePointer<esp_matter.cluster_t>
Expand All @@ -142,18 +163,24 @@ struct ColorControl: MatterConcreteCluster {
self.cluster = cluster
}

func attribute<Attribute: MatterAttribute>(_ id: AttributeID<Attribute>) -> Attribute {
func attribute<Attribute: MatterAttribute>(_ id: AttributeID<Attribute>)
-> Attribute
{
Attribute(attribute: esp_matter.attribute.get_shim(cluster, id.rawValue))
}

var currentHue: CurrentHue { attribute(.currentHue) }
var currentSaturation: CurrentSaturation { attribute(.currentSaturation) }
var currentX: CurrentX { attribute(.currentX) }
var currentY: CurrentY { attribute(.currentY) }
var colorTemperatureMireds: ColorTemperatureMireds { attribute(.colorTemperatureMireds) }
var colorTemperatureMireds: ColorTemperatureMireds {
attribute(.colorTemperatureMireds)
}
var colorMode: ColorMode { attribute(.colorMode) }

func add(_ config: esp_matter.cluster.color_control.feature.hue_saturation.config_t) {
func add(
_ config: esp_matter.cluster.color_control.feature.hue_saturation.config_t
) {
var cfg = config
esp_matter.cluster.color_control.feature.hue_saturation.add(cluster, &cfg)
}
Expand Down
78 changes: 51 additions & 27 deletions smart-light/Matter/Matter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
//
//===----------------------------------------------------------------------===//

enum Matter { }
enum Matter {}

extension Matter {
class Node {
Expand All @@ -31,14 +31,23 @@ extension Matter {
_ = Unmanaged.passRetained(self)

// Create the actual root node object, wire up callbacks.
innerNode = RootNode(attribute: self.eventHandler, identify: { _, _, _, _ in self.identifyHandler?() })!
innerNode = RootNode(
attribute: self.eventHandler,
identify: { _, _, _, _ in self.identifyHandler?() })!
}

func eventHandler(type: MatterAttributeEvent, endpoint: __idf_main.Endpoint, cluster: Cluster, attribute: UInt32, value: UnsafeMutablePointer<esp_matter_attr_val_t>?) {
func eventHandler(
type: MatterAttributeEvent, endpoint: __idf_main.Endpoint,
cluster: Cluster, attribute: UInt32,
value: UnsafeMutablePointer<esp_matter_attr_val_t>?
) {
guard type == .didSet else { return }
guard let e = self.endpoints.first(where: { $0.id == endpoint.id }) else { return }
guard let e = self.endpoints.first(where: { $0.id == endpoint.id }) else {
return
}
let value: Int = Int(value?.pointee.val.u64 ?? 0)
guard let a = Endpoint.Attribute(cluster: cluster, attribute: attribute) else { return }
guard let a = Endpoint.Attribute(cluster: cluster, attribute: attribute)
else { return }
e.eventHandler?(Endpoint.Event(type: type, attribute: a, value: value))
}
}
Expand Down Expand Up @@ -76,27 +85,34 @@ extension Matter {
case OnOff.AttributeID<OnOff.OnOffState>.state.rawValue: self = .onOff
default: return nil
}
}
else
if let _ = cluster.as(LevelControl.self) {
} else if let _ = cluster.as(LevelControl.self) {
switch attribute {
case LevelControl.AttributeID<LevelControl.CurrentLevel>.currentLevel.rawValue: self = .levelControl
case LevelControl.AttributeID<LevelControl.CurrentLevel>.currentLevel
.rawValue:
self = .levelControl
default: return nil
}
}
else
if let _ = cluster.as(ColorControl.self) {
} else if let _ = cluster.as(ColorControl.self) {
switch attribute {
case ColorControl.AttributeID<ColorControl.CurrentHue>.currentHue.rawValue: self = .colorControl(.currentHue)
case ColorControl.AttributeID<ColorControl.CurrentSaturation>.currentSaturation.rawValue: self = .colorControl(.currentSaturation)
case ColorControl.AttributeID<ColorControl.CurrentX>.currentX.rawValue: self = .colorControl(.currentX)
case ColorControl.AttributeID<ColorControl.CurrentY>.currentY.rawValue: self = .colorControl(.currentY)
case ColorControl.AttributeID<ColorControl.ColorTemperatureMireds>.colorTemperatureMireds.rawValue: self = .colorControl(.colorTemperatureMireds)
case ColorControl.AttributeID<ColorControl.ColorMode>.colorMode.rawValue: self = .colorControl(.colorMode)
case ColorControl.AttributeID<ColorControl.CurrentHue>.currentHue
.rawValue:
self = .colorControl(.currentHue)
case ColorControl.AttributeID<ColorControl.CurrentSaturation>
.currentSaturation.rawValue:
self = .colorControl(.currentSaturation)
case ColorControl.AttributeID<ColorControl.CurrentX>.currentX.rawValue:
self = .colorControl(.currentX)
case ColorControl.AttributeID<ColorControl.CurrentY>.currentY.rawValue:
self = .colorControl(.currentY)
case ColorControl.AttributeID<ColorControl.ColorTemperatureMireds>
.colorTemperatureMireds.rawValue:
self = .colorControl(.colorTemperatureMireds)
case ColorControl.AttributeID<ColorControl.ColorMode>.colorMode
.rawValue:
self = .colorControl(.colorMode)
default: return nil
}
}
else {
} else {
self = .unknown(attribute)
}
}
Expand All @@ -119,13 +135,17 @@ extension Matter {
lightConfig.on_off.on_off = true
lightConfig.level_control.current_level = .init(64)
lightConfig.level_control.lighting.start_up_current_level = .init(64)
lightConfig.color_control.color_mode = chip.app.Clusters.ColorControl.ColorMode.colorTemperature.rawValue
lightConfig.color_control.enhanced_color_mode = chip.app.Clusters.ColorControl.ColorMode.colorTemperature.rawValue
lightConfig.color_control.color_mode =
chip.app.Clusters.ColorControl.ColorMode.colorTemperature.rawValue
lightConfig.color_control.enhanced_color_mode =
chip.app.Clusters.ColorControl.ColorMode.colorTemperature.rawValue

let light = MatterExtendedColorLight(node.innerNode, configuration: lightConfig)
let light = MatterExtendedColorLight(
node.innerNode, configuration: lightConfig)
self.id = Int(light.id)

var hsv = esp_matter.cluster.color_control.feature.hue_saturation.config_t()
var hsv = esp_matter.cluster.color_control.feature.hue_saturation
.config_t()
hsv.current_hue = 255
hsv.current_saturation = 255
light.colorControl.add(hsv)
Expand All @@ -143,9 +163,12 @@ extension Matter {
}

func start() {
func callback(event: UnsafePointer<chip.DeviceLayer.ChipDeviceEvent>?, context: Int) {
func callback(
event: UnsafePointer<chip.DeviceLayer.ChipDeviceEvent>?, context: Int
) {
switch Int(event!.pointee.Type) {
case chip.DeviceLayer.DeviceEventType.kFabricRemoved: recomissionFabric()
case chip.DeviceLayer.DeviceEventType.kFabricRemoved:
recomissionFabric()
default: break
}
}
Expand All @@ -165,7 +188,8 @@ func print(_ a: Matter.Endpoint.Attribute) {
case .currentSaturation: print("currentSaturation", terminator: "")
case .currentX: print("currentX", terminator: "")
case .currentY: print("currentY", terminator: "")
case .colorTemperatureMireds: print("colorTemperatureMireds", terminator: "")
case .colorTemperatureMireds:
print("colorTemperatureMireds", terminator: "")
case .colorMode: print("colorMode", terminator: "")
}
print(")")
Expand Down
Loading

0 comments on commit fd013f7

Please sign in to comment.