Skip to content

Commit

Permalink
Organising/refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
notjosh committed Aug 7, 2020
1 parent 8967d4e commit d1d09a9
Show file tree
Hide file tree
Showing 12 changed files with 205 additions and 152 deletions.
56 changes: 50 additions & 6 deletions Aureal.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
42F4D44324D178100031C650 /* HIDDeviceMonitor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 42F4D44224D178100031C650 /* HIDDeviceMonitor.swift */; };
42F4D44524D178200031C650 /* HIDDevice.swift in Sources */ = {isa = PBXBuildFile; fileRef = 42F4D44424D178200031C650 /* HIDDevice.swift */; };
42FE50D724DC903000E4B00A /* GradientView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 42FE50D624DC903000E4B00A /* GradientView.swift */; };
42FE50D924DCDF9F00E4B00A /* Effect.swift in Sources */ = {isa = PBXBuildFile; fileRef = 42FE50D824DCDF9F00E4B00A /* Effect.swift */; };
42FE50DC24DCE2A200E4B00A /* CommandColor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 42FE50DB24DCE2A200E4B00A /* CommandColor.swift */; };
42FE50DE24DCE2CF00E4B00A /* Int+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 42FE50DD24DCE2CF00E4B00A /* Int+Extensions.swift */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand All @@ -50,6 +53,9 @@
42F4D44224D178100031C650 /* HIDDeviceMonitor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HIDDeviceMonitor.swift; sourceTree = "<group>"; };
42F4D44424D178200031C650 /* HIDDevice.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HIDDevice.swift; sourceTree = "<group>"; };
42FE50D624DC903000E4B00A /* GradientView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GradientView.swift; sourceTree = "<group>"; };
42FE50D824DCDF9F00E4B00A /* Effect.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Effect.swift; sourceTree = "<group>"; };
42FE50DB24DCE2A200E4B00A /* CommandColor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CommandColor.swift; sourceTree = "<group>"; };
42FE50DD24DCE2CF00E4B00A /* Int+Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Int+Extensions.swift"; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -86,18 +92,17 @@
children = (
42F4D43624D0D44E0031C650 /* External */,
42F4D42124D0D3330031C650 /* AppDelegate.swift */,
42F4D42324D0D3330031C650 /* ViewController.swift */,
427A570324D9CB9800760111 /* DeviceViewController.swift */,
42FE50E024DCE33200E4B00A /* View */,
42FE50E124DCE33C00E4B00A /* View Controller */,
42FE50DA24DCE29600E4B00A /* Command */,
42FE50DF24DCE30B00E4B00A /* Extensions */,
4277A04524D4379D005AB0DA /* USBWatcher.swift */,
42F4D43C24D0DCD50031C650 /* AuraUSBController.swift */,
42F4D43E24D0DD430031C650 /* Command.swift */,
42F4D44024D0DE1D0031C650 /* Aura.swift */,
4277A04724D43B1E005AB0DA /* AuraDevice.swift */,
426A743F24D2ECD500749F38 /* Array+Extensions.swift */,
427A570524D9D26F00760111 /* NSView+Extensions.swift */,
427A570124D9985A00760111 /* DeviceManager.swift */,
42FE50D824DCDF9F00E4B00A /* Effect.swift */,
427A570724D9D89100760111 /* EffectRunner.swift */,
42FE50D624DC903000E4B00A /* GradientView.swift */,
42F4D42524D0D3340031C650 /* Assets.xcassets */,
42F4D42724D0D3340031C650 /* Main.storyboard */,
42F4D42A24D0D3340031C650 /* Info.plist */,
Expand Down Expand Up @@ -132,6 +137,42 @@
path = USBDeviceSwift;
sourceTree = "<group>";
};
42FE50DA24DCE29600E4B00A /* Command */ = {
isa = PBXGroup;
children = (
42F4D43E24D0DD430031C650 /* Command.swift */,
42FE50DB24DCE2A200E4B00A /* CommandColor.swift */,
);
path = Command;
sourceTree = "<group>";
};
42FE50DF24DCE30B00E4B00A /* Extensions */ = {
isa = PBXGroup;
children = (
426A743F24D2ECD500749F38 /* Array+Extensions.swift */,
42FE50DD24DCE2CF00E4B00A /* Int+Extensions.swift */,
427A570524D9D26F00760111 /* NSView+Extensions.swift */,
);
path = Extensions;
sourceTree = "<group>";
};
42FE50E024DCE33200E4B00A /* View */ = {
isa = PBXGroup;
children = (
42FE50D624DC903000E4B00A /* GradientView.swift */,
);
path = View;
sourceTree = "<group>";
};
42FE50E124DCE33C00E4B00A /* View Controller */ = {
isa = PBXGroup;
children = (
42F4D42324D0D3330031C650 /* ViewController.swift */,
427A570324D9CB9800760111 /* DeviceViewController.swift */,
);
path = "View Controller";
sourceTree = "<group>";
};
/* End PBXGroup section */

/* Begin PBXNativeTarget section */
Expand Down Expand Up @@ -205,7 +246,9 @@
42F4D42424D0D3330031C650 /* ViewController.swift in Sources */,
427A570224D9985A00760111 /* DeviceManager.swift in Sources */,
42FE50D724DC903000E4B00A /* GradientView.swift in Sources */,
42FE50DE24DCE2CF00E4B00A /* Int+Extensions.swift in Sources */,
426A744024D2ECD500749F38 /* Array+Extensions.swift in Sources */,
42FE50D924DCDF9F00E4B00A /* Effect.swift in Sources */,
4277A04624D4379D005AB0DA /* USBWatcher.swift in Sources */,
427A570824D9D89100760111 /* EffectRunner.swift in Sources */,
42F4D43D24D0DCD50031C650 /* AuraUSBController.swift in Sources */,
Expand All @@ -217,6 +260,7 @@
42F4D43F24D0DD430031C650 /* Command.swift in Sources */,
42F4D44324D178100031C650 /* HIDDeviceMonitor.swift in Sources */,
42F4D44124D0DE1D0031C650 /* Aura.swift in Sources */,
42FE50DC24DCE2A200E4B00A /* CommandColor.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
7 changes: 6 additions & 1 deletion Aureal/AuraDevice.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@ class AuraUSBDevice: CustomStringConvertible {
}

var description: String {
"<firmware: \(firmware), connectionState: \(connectionState), root: \(rgbDevice), addressables: \(addressables)>"
"<" +
"firmware: \(String(describing: firmware)), " +
"connectionState: \(connectionState), " +
"root: \(String(describing: rgbDevice)), " +
"addressables: \(addressables)" +
">"
}
}

Expand Down
4 changes: 0 additions & 4 deletions Aureal/AuraUSBController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ public extension Notification.Name {
}

enum AuraUSBControllerError: Error {
case InterfaceUnavailable
case CommandTooLong
case InvalidResponse(code: IOReturn)
}
Expand Down Expand Up @@ -36,8 +35,6 @@ class AuraUSBController {
}

func getFirmwareVersion(from device: HIDDevice) throws {
print("get firmware")

try send(commandBytes: [
AuraCommand,
0x82,
Expand All @@ -53,7 +50,6 @@ class AuraUSBController {
}

func setDirect(_ rgbs: [CommandColor], startLED: UInt8, channel: UInt8, apply: Bool, to device: HIDDevice) throws {
// print(rgbs.count)
try send(
commandBytes: [
AuraCommand,
Expand Down
53 changes: 2 additions & 51 deletions Aureal/Command.swift → Aureal/Command/Command.swift
Original file line number Diff line number Diff line change
@@ -1,42 +1,4 @@
import Cocoa

struct CommandColor {
let r: UInt8
let g: UInt8
let b: UInt8

init(r: UInt8, g: UInt8, b: UInt8) {
self.r = r
self.g = g
self.b = b
}

init(color: NSColor) {
let rgb = color.usingColorSpace(NSColorSpace.sRGB)!
// let rgb = color

let r: UInt8 = UInt8(rgb.redComponent * 255)
let g: UInt8 = UInt8(rgb.greenComponent * 255)
let b: UInt8 = UInt8(rgb.blueComponent * 255)

self.init(r: r, g: g, b: b)
}

static let red = CommandColor(r: 0xff, g: 0x00, b: 0x00)
static let blue = CommandColor(r: 0x00, g: 0x00, b: 0xff)
static let green = CommandColor(r: 0x00, g: 0xff, b: 0x00)
static let white = CommandColor(r: 0xff, g: 0xff, b: 0xff)
static let lwhite = CommandColor(r: 0x40, g: 0x40, b: 0x40)
static let black = CommandColor(r: 0x00, g: 0x00, b: 0x00)

static var random: CommandColor {
CommandColor(
r: UInt8.random(in: 0...255),
g: UInt8.random(in: 0...255),
b: UInt8.random(in: 0...255)
)
}
}
import Foundation

protocol Command {
var isAnimated: Bool { get }
Expand Down Expand Up @@ -153,7 +115,7 @@ class GradientDirectCommand: DirectCommand {
let step = step % colors.count

return (0..<capacity).map { idx in
let offset = (idx * (steps / 10) - step) %% colors.count
let offset = (step - idx * (steps / 10)) %% colors.count
return colors[offset]
}
}
Expand Down Expand Up @@ -191,14 +153,3 @@ struct Patriotism🦅DirectCommand: DirectCommand {
return Array(out)
}
}

// essentially "%", but also works on negative numbers
// via https://stackoverflow.com/a/59461073
infix operator %%
extension Int {
static func %% (_ left: Int, _ right: Int) -> Int {
if left >= 0 { return left % right }
if left >= -right { return (left + right) }
return ((left % right) + right) % right
}
}
41 changes: 41 additions & 0 deletions Aureal/Command/CommandColor.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import Cocoa

struct CommandColor {
let r: UInt8
let g: UInt8
let b: UInt8

init(r: UInt8, g: UInt8, b: UInt8) {
self.r = r
self.g = g
self.b = b
}

init(color: NSColor) {
let rgb = color.usingColorSpace(NSColorSpace.sRGB)!
// let rgb = color

let r: UInt8 = UInt8(rgb.redComponent * 255)
let g: UInt8 = UInt8(rgb.greenComponent * 255)
let b: UInt8 = UInt8(rgb.blueComponent * 255)

self.init(r: r, g: g, b: b)
}
}

extension CommandColor {
static let red = CommandColor(r: 0xff, g: 0x00, b: 0x00)
static let blue = CommandColor(r: 0x00, g: 0x00, b: 0xff)
static let green = CommandColor(r: 0x00, g: 0xff, b: 0x00)
static let white = CommandColor(r: 0xff, g: 0xff, b: 0xff)
static let lwhite = CommandColor(r: 0x40, g: 0x40, b: 0x40)
static let black = CommandColor(r: 0x00, g: 0x00, b: 0x00)

static var random: CommandColor {
CommandColor(
r: UInt8.random(in: 0...255),
g: UInt8.random(in: 0...255),
b: UInt8.random(in: 0...255)
)
}
}
70 changes: 70 additions & 0 deletions Aureal/Effect.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import Foundation

enum EffectType {
case builtInEffect
case direct
}

enum EffectColorMode: Equatable {
case none
case count(Int)
case dynamic

var count: Int {
switch self {
case .none:
return 0
case .count(let count):
return count
case .dynamic:
return 3
}
}
}

protocol Effect {
var name: String { get }
var type: EffectType { get }
var colorMode: EffectColorMode { get }

func command(for colors: [CommandColor]) -> Command
}

struct BuiltInEffect: Effect {
let mode: AuraEffect

var name: String {
"Built-in: \(mode.name)"
}

var colorMode: EffectColorMode {
mode.isColorable ? .count(1) : .none
}

var type: EffectType {
.builtInEffect
}

func command(for colors: [CommandColor]) -> Command {
let command = EffectCommand(
mode,
color: mode.isColorable ? colors.first ?? .black : .black
)

return command
}
}

struct DirectEffect: Effect {
let name: String
let builder: (([CommandColor]) -> DirectCommand)
let colorMode: EffectColorMode

var type: EffectType {
.direct
}

func command(for colors: [CommandColor]) -> Command {
return builder(colors)
}
}
File renamed without changes.
12 changes: 12 additions & 0 deletions Aureal/Extensions/Int+Extensions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import Foundation

// essentially "%", but also works on negative numbers
// via https://stackoverflow.com/a/59461073
infix operator %%
extension Int {
static func %% (_ left: Int, _ right: Int) -> Int {
if left >= 0 { return left % right }
if left >= -right { return (left + right) }
return ((left % right) + right) % right
}
}
File renamed without changes.
Loading

0 comments on commit d1d09a9

Please sign in to comment.