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

Function added to get cpu information of device (refactoring). #98

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions Device.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
006E76C41C3C09DE00656720 /* Version.swift in Sources */ = {isa = PBXBuildFile; fileRef = BE11EF0F1BE4001A00816835 /* Version.swift */; };
006E76C51C3C09DE00656720 /* Size.swift in Sources */ = {isa = PBXBuildFile; fileRef = BE11EF111BE4002800816835 /* Size.swift */; };
006E76C61C3C09DE00656720 /* Type.swift in Sources */ = {isa = PBXBuildFile; fileRef = F8E8F0391C176DB7006B19D2 /* Type.swift */; };
2392B095247F48D0000E4821 /* CPUInfo.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2392B094247F48D0000E4821 /* CPUInfo.swift */; };
BE11EF011BE3FD9200816835 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = BE11EEFC1BE3FD9200816835 /* AppDelegate.swift */; };
BE11EF021BE3FD9200816835 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = BE11EEFD1BE3FD9200816835 /* Assets.xcassets */; };
BE11EF051BE3FD9200816835 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = BE11EF001BE3FD9200816835 /* ViewController.swift */; };
Expand Down Expand Up @@ -78,6 +79,7 @@
001C44691C3C08C000F6599D /* Device.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Device.framework; sourceTree = BUILT_PRODUCTS_DIR; };
001C446C1C3C08C000F6599D /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
001C446D1C3C08C000F6599D /* Device.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Device.h; sourceTree = "<group>"; };
2392B094247F48D0000E4821 /* CPUInfo.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CPUInfo.swift; sourceTree = "<group>"; };
BE11EEE71BE3FC0300816835 /* Example.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Example.app; sourceTree = BUILT_PRODUCTS_DIR; };
BE11EEFC1BE3FD9200816835 /* AppDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = AppDelegate.swift; path = Example/AppDelegate.swift; sourceTree = SOURCE_ROOT; };
BE11EEFD1BE3FD9200816835 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; name = Assets.xcassets; path = Example/Assets.xcassets; sourceTree = SOURCE_ROOT; };
Expand Down Expand Up @@ -205,6 +207,7 @@
E2C023B51D6331A00033AD25 /* iOS */ = {
isa = PBXGroup;
children = (
2392B094247F48D0000E4821 /* CPUInfo.swift */,
E2C023B61D6331A00033AD25 /* Device.swift */,
);
path = iOS;
Expand Down Expand Up @@ -416,6 +419,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
2392B095247F48D0000E4821 /* CPUInfo.swift in Sources */,
006E76C41C3C09DE00656720 /* Version.swift in Sources */,
006E76C51C3C09DE00656720 /* Size.swift in Sources */,
006E76C61C3C09DE00656720 /* Type.swift in Sources */,
Expand Down
98 changes: 98 additions & 0 deletions Source/iOS/CPUInfo.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
//
// CPUInfo.swift
// Device
//
// Created by KWANG HYOUN KIM on 2020/05/28.
// Copyright © 2020 KWANG HYOUN KIM. All rights reserved.
//

public struct CPUInfo {
static public let na = CPUInfo(version: -1, name: .na, clock: .na)
static public let a4_800mhz = CPUInfo(version: 4, name: .a4, clock: .mhz800)
static public let a5_800mhz = CPUInfo(version: 5, name: .a5, clock: .mhz800)
static public let a5x_1000mhz = CPUInfo(version: 5.1, name: .a5x, clock: .mhz1000)
static public let a6_1300mhz = CPUInfo(version: 6, name: .a6, clock: .mhz1300)
static public let a6x_1400mhz = CPUInfo(version: 6.1, name: .a6x, clock: .mhz1400)
static public let a7_1300mhz = CPUInfo(version: 7, name: .a7, clock: .mhz1300)
static public let a7_1400mhz = CPUInfo(version: 7.1, name: .a7, clock: .mhz1400)
static public let a8_1100mhz = CPUInfo(version: 8, name: .a8, clock: .mhz1100)
static public let a8_1400mhz = CPUInfo(version: 8.1, name: .a8, clock: .mhz1400)
static public let a8x_1500mhz = CPUInfo(version: 8.2, name: .a8x, clock: .mhz1500)
static public let a9_1850mhz = CPUInfo(version: 9, name: .a9, clock: .mhz1850)
static public let a9x_2160mhz = CPUInfo(version: 9.1, name: .a9x, clock: .mhz2160)
static public let a9x_2240mhz = CPUInfo(version: 9.2, name: .a9x, clock: .mhz2240)
static public let a10_2340mhz = CPUInfo(version: 10, name: .a10, clock: .mhz2340)
static public let a10x_2340mhz = CPUInfo(version: 10.1, name: .a10x, clock: .mhz2340)
static public let a11_2390mhz = CPUInfo(version: 11, name: .a11, clock: .mhz2390)
static public let a12_2490mhz = CPUInfo(version: 12, name: .a12, clock: .mhz2490)
static public let a12z_2500mhz = CPUInfo(version: 12.1, name: .a12z, clock: .mhz2500)
static public let a13_2650mhz = CPUInfo(version: 13, name: .a13, clock: .mhz2650)

public let version: Float
public let name: Name
public let clock: Clock

public enum Name: String {
case na = "NA"
case a4 = "A4"
case a5 = "A5"
case a5x = "A5X"
case a6 = "A6"
case a6x = "A6X"
case a7 = "A7"
case a8 = "A8"
case a8x = "A8X"
case a9 = "A9"
case a9x = "A9X"
case a10 = "A10 Fusion"
case a10x = "A10X Fusion"
case a11 = "A11 Bionic"
case a12 = "A12 Bionic"
case a12z = "A12Z Bionic"
case a13 = "A13 Bionic"
}

public enum Clock: UInt {
case na = 0
case mhz800 = 800
case mhz1000 = 1000
case mhz1100 = 1100
case mhz1300 = 1300
case mhz1400 = 1400
case mhz1500 = 1500
case mhz1850 = 1850
case mhz2160 = 2160
case mhz2240 = 2240
case mhz2340 = 2340
case mhz2390 = 2390
case mhz2490 = 2490
case mhz2500 = 2500
case mhz2650 = 2650

public var ghz: Float {
return Float(rawValue) / 1000
}

public var stringValue: String {
return rawValue >= 1000 ? "\(ghz) GHz" : "\(rawValue) MHz"
}
}
}

extension CPUInfo: Comparable {
static public func < (lhs: Self, rhs: Self) -> Bool {
return lhs.version < rhs.version
}

static public func <= (lhs: Self, rhs: Self) -> Bool {
return lhs.version <= rhs.version
}

static public func >= (lhs: Self, rhs: Self) -> Bool {
return lhs.version >= rhs.version
}

static public func > (lhs: Self, rhs: Self) -> Bool {
return lhs.version > rhs.version
}
}
82 changes: 82 additions & 0 deletions Source/iOS/Device.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,88 @@ open class Device {
return .unknown
}
}

static public func cpuInfo() -> CPUInfo {
switch version() {
// iPod
case .iPodTouch4Gen:
return .a4_800mhz
case .iPodTouch5Gen:
return .a5_800mhz
case .iPodTouch6Gen:
return .a8_1100mhz

// iPhone
case .iPhone4:
return .a4_800mhz
case .iPhone4S:
return .a5_800mhz
case .iPhone5,
.iPhone5C:
return .a6_1300mhz
case .iPhone5S:
return .a7_1300mhz
case .iPhone6,
.iPhone6Plus:
return .a8_1400mhz
case .iPhone6S,
.iPhone6SPlus,
.iPhoneSE:
return .a9_1850mhz
case .iPhone7,
.iPhone7Plus:
return .a10_2340mhz
case .iPhone8,
.iPhone8Plus,
.iPhoneX:
return .a11_2390mhz
case .iPhoneXS,
.iPhoneXS_Max,
.iPhoneXR:
return .a12_2490mhz
case .iPhone11,
.iPhone11Pro,
.iPhone11Pro_Max:
return .a13_2650mhz

// iPad
case .iPad2:
return .a5x_1000mhz
case .iPad3:
return .a5x_1000mhz
case .iPad4:
return .a6x_1400mhz
case .iPad5:
return .a9_1850mhz
case .iPad6:
return .a10_2340mhz
case .iPadAir:
return .a7_1400mhz
case .iPadAir2:
return .a8x_1500mhz
case .iPadAir3:
return .a12_2490mhz
case .iPadMini:
return .a5x_1000mhz
case .iPadMini2,
.iPadMini3:
return .a7_1300mhz
case .iPadMini4:
return .a8x_1500mhz
case .iPadPro9_7Inch:
return .a9x_2160mhz
case .iPadPro12_9Inch:
return .a9x_2240mhz
case .iPadPro12_9Inch2,
.iPadPro10_5Inch:
return .a10x_2340mhz
case .iPadPro11_0Inch,
.iPadPro12_9Inch3:
return .a12z_2500mhz
default:
return .na
}
}

static public func version() -> Version {
return getVersion(code: getVersionCode())
Expand Down