-
Notifications
You must be signed in to change notification settings - Fork 2
/
util.go
executable file
·51 lines (43 loc) · 1.26 KB
/
util.go
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
46
47
48
49
50
51
/*
Go Language Raspberry Pi Interface
(c) Copyright David Thorpe 2018
All Rights Reserved
Documentation http://djthorpe.github.io/gopi/
For Licensing and Usage information, please see LICENSE.md
*/
package pwm
import (
"regexp"
"strconv"
"strings"
"time"
// Frameworks
"github.com/djthorpe/gopi"
)
////////////////////////////////////////////////////////////////////////////////
// CONSTANTS
var (
regexpFrequency = regexp.MustCompile("^([0-9]*\\.?[0-9]+)\\s*(hz|khz|mhz)$")
)
////////////////////////////////////////////////////////////////////////////////
// PUBLIC METHODS
// ParseFrequency converts a string into a period. It will return an
// error if the string could not be parsed.
func ParseFrequency(value string) (time.Duration, error) {
if parts := regexpFrequency.FindStringSubmatch(strings.ToLower(value)); len(parts) != 3 {
return 0, gopi.ErrBadParameter
} else if num, err := strconv.ParseFloat(parts[1], 64); err != nil {
return 0, err
} else {
switch parts[2] {
case "hz":
return time.Nanosecond * time.Duration((1E9 / num)), nil
case "khz":
return time.Nanosecond * time.Duration((1E6 / num)), nil
case "mhz":
return time.Nanosecond * time.Duration((1E3 / num)), nil
default:
return 0, gopi.ErrBadParameter
}
}
}