-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfelicita_interface.swift
45 lines (36 loc) · 1.01 KB
/
felicita_interface.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import Foundation
class FelicitaInterface : ScaleInterface {
private let send_command: ((Data) -> ())?
init(send_command: ((Data) -> ())?) {
self.send_command = send_command
}
func can_send() -> Bool {
return send_command != nil
}
func tare() {
send_command?(Data(bytes: [84], count: 1))
}
func start_timer() {
send_command?(Data(bytes: [82], count: 1))
}
func reset_timer() {
send_command?(Data(bytes: [67], count: 1))
}
func stop_timer() {
send_command?(Data(bytes: [83], count: 1))
}
func restart() {
tare()
reset_timer()
start_timer()
tare()
}
func get_weight(message: Data) -> Double? {
if message.count == 18, let weight_str = String(data: message[2...8], encoding: .ascii) {
if let grams_over_a_hundred = Int(weight_str) {
return Double(grams_over_a_hundred) / 100.0
}
}
return nil
}
}