|
@@ -6,6 +6,7 @@ import (
|
|
|
"fmt"
|
|
|
"log"
|
|
|
"os"
|
|
|
+ "strconv"
|
|
|
"strings"
|
|
|
"time"
|
|
|
|
|
@@ -19,6 +20,7 @@ var cur_cmd string
|
|
|
var cmdList map[string][]byte
|
|
|
var tmrCmdTimeout *time.Timer = nil
|
|
|
var chnMain chan string
|
|
|
+var sampFreq int
|
|
|
|
|
|
func init() {
|
|
|
cmdList = map[string][]byte{
|
|
@@ -86,9 +88,11 @@ func shell(device hid.Device) {
|
|
|
switch cur_cmd {
|
|
|
case "read":
|
|
|
FmtPrintBinary(buf)
|
|
|
- temp := binary.BigEndian.Uint16(buf[23:25])
|
|
|
- humidity := binary.BigEndian.Uint16(buf[25:27])
|
|
|
- log.Printf("%d, %d\n", temp, humidity)
|
|
|
+ temp := binary.BigEndian.Uint16(buf[0x23:0x25])
|
|
|
+ humidity := binary.BigEndian.Uint16(buf[0x25:0x27])
|
|
|
+ ts := uint32(time.Now().Unix())
|
|
|
+ log.Printf("%d: %d, %d\n", ts, temp, humidity)
|
|
|
+ ReportTelemty(ts, temp, humidity)
|
|
|
chnMain <- cur_cmd
|
|
|
case "query": // aa3b020000861c5553422d5448000000555342e6b8a9e6b9bfe5baa6e8aeb0e5bd95e4bbaa000000000000000000000000000000000000000000000000006223
|
|
|
num_total = int(buf[6])
|
|
@@ -151,13 +155,16 @@ func shell(device hid.Device) {
|
|
|
|
|
|
timeout := 0
|
|
|
|
|
|
- for _, k := range cmds {
|
|
|
+ //for _, k := range cmds {
|
|
|
+ for k := cmds[0]; ; {
|
|
|
sendcmd(device, k)
|
|
|
tmrCmdTimeout.Stop()
|
|
|
tmrCmdTimeout.Reset(3 * time.Second)
|
|
|
select {
|
|
|
case msg := <-chnMain:
|
|
|
log.Println("Send command [", k, "<>", msg, "] done!")
|
|
|
+ tmrCmdTimeout.Stop()
|
|
|
+ time.Sleep(time.Duration(sampFreq) * time.Second)
|
|
|
case <-tmrCmdTimeout.C:
|
|
|
log.Println("Send command [", k, "] timeout!")
|
|
|
timeout = 1
|
|
@@ -194,7 +201,7 @@ func main() {
|
|
|
if len(os.Args) == 2 && (os.Args[1] == "-h" || os.Args[1] == "--help") {
|
|
|
fmt.Println("USAGE:")
|
|
|
fmt.Printf(" %s list USB HID devices\n", os.Args[0])
|
|
|
- fmt.Printf(" %s <id> open USB HID device shell for the given input report size\n", os.Args[0])
|
|
|
+ fmt.Printf(" %s <id> [dd] open USB HID device shell for the given input report size and each [dd] second sample and report once \n", os.Args[0])
|
|
|
fmt.Printf(" %s -h|--help show this help\n", os.Args[0])
|
|
|
fmt.Println()
|
|
|
return
|
|
@@ -214,6 +221,15 @@ func main() {
|
|
|
return
|
|
|
}
|
|
|
|
|
|
+ sampFreq = 30
|
|
|
+ if len(os.Args) >= 3 {
|
|
|
+ duration, err := strconv.Atoi(os.Args[2])
|
|
|
+ if err == nil {
|
|
|
+ sampFreq = duration
|
|
|
+ fmt.Println("Sample duration:", sampFreq)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
hid.UsbWalk(func(device hid.Device) {
|
|
|
info := device.Info()
|
|
|
id := fmt.Sprintf("%04x:%04x:%04x:%02x", info.Vendor, info.Product, info.Revision, info.Interface)
|