misc.go 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. // Copyright 2013 Google Inc. All rights reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package usb
  15. import (
  16. "fmt"
  17. )
  18. type BCD uint16
  19. const (
  20. USB_2_0 BCD = 0x0200
  21. USB_1_1 BCD = 0x0110
  22. USB_1_0 BCD = 0x0100
  23. )
  24. func (d BCD) Int() (i int) {
  25. ten := 1
  26. for o := uint(0); o < 4; o++ {
  27. n := ((0xF << (o * 4)) & d) >> (o * 4)
  28. i += int(n) * ten
  29. ten *= 10
  30. }
  31. return
  32. }
  33. func (d BCD) String() string {
  34. return fmt.Sprintf("%02x.%02x", int(d>>8), int(d&0xFF))
  35. }
  36. type ID uint16
  37. func (id ID) String() string {
  38. return fmt.Sprintf("%04x", int(id))
  39. }