mirror of
https://github.com/tinygo-org/drivers.git
synced 2026-08-15 20:33:39 +00:00
wifinina: avoid fmt package
This commit is contained in:
@@ -2,9 +2,8 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/binary"
|
|
||||||
"fmt"
|
|
||||||
"machine"
|
"machine"
|
||||||
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"tinygo.org/x/drivers/wifinina"
|
"tinygo.org/x/drivers/wifinina"
|
||||||
@@ -80,7 +79,7 @@ func printRSSI() {
|
|||||||
println("Unknown (error: ", err.Error(), ")")
|
println("Unknown (error: ", err.Error(), ")")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
println(fmt.Sprintf("%d", rssi))
|
println(strconv.Itoa(int(rssi)))
|
||||||
}
|
}
|
||||||
|
|
||||||
func printIPs() {
|
func printIPs() {
|
||||||
@@ -113,20 +112,11 @@ func printTime() {
|
|||||||
|
|
||||||
func printMac() {
|
func printMac() {
|
||||||
print("MAC: ")
|
print("MAC: ")
|
||||||
b := make([]byte, 8)
|
|
||||||
mac, err := adaptor.GetMACAddress()
|
mac, err := adaptor.GetMACAddress()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
println("Unknown (", err.Error(), ")")
|
println("Unknown (", err.Error(), ")")
|
||||||
}
|
}
|
||||||
binary.LittleEndian.PutUint64(b, uint64(mac))
|
println(mac.String())
|
||||||
macAddress := ""
|
|
||||||
for i := 5; i >= 0; i-- {
|
|
||||||
macAddress += fmt.Sprintf("%0X", b[i])
|
|
||||||
if i != 0 {
|
|
||||||
macAddress += ":"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
println(macAddress)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Wait for user to open serial console
|
// Wait for user to open serial console
|
||||||
|
|||||||
+5
-5
@@ -1,7 +1,7 @@
|
|||||||
package wifinina
|
package wifinina
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"errors"
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -104,7 +104,7 @@ func (drv *Driver) connectSocket(addr, portStr string, mode uint8) error {
|
|||||||
func convertPort(portStr string) (uint16, error) {
|
func convertPort(portStr string) (uint16, error) {
|
||||||
p64, err := strconv.ParseUint(portStr, 10, 16)
|
p64, err := strconv.ParseUint(portStr, 10, 16)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, fmt.Errorf("could not convert port to uint16: %w", err)
|
return 0, errors.New("could not convert port to uint16: " + err.Error())
|
||||||
}
|
}
|
||||||
return uint16(p64), nil
|
return uint16(p64), nil
|
||||||
}
|
}
|
||||||
@@ -175,13 +175,13 @@ func (drv *Driver) Write(b []byte) (n int, err error) {
|
|||||||
}
|
}
|
||||||
if drv.proto == ProtoModeUDP {
|
if drv.proto == ProtoModeUDP {
|
||||||
if err := drv.dev.StartClient("", drv.ip, drv.port, drv.sock, drv.proto); err != nil {
|
if err := drv.dev.StartClient("", drv.ip, drv.port, drv.sock, drv.proto); err != nil {
|
||||||
return 0, fmt.Errorf("error in startClient: %w", err)
|
return 0, errors.New("error in startClient: " + err.Error())
|
||||||
}
|
}
|
||||||
if _, err := drv.dev.InsertDataBuf(b, drv.sock); err != nil {
|
if _, err := drv.dev.InsertDataBuf(b, drv.sock); err != nil {
|
||||||
return 0, fmt.Errorf("error in insertDataBuf: %w", err)
|
return 0, errors.New("error in insertDataBuf: " + err.Error())
|
||||||
}
|
}
|
||||||
if _, err := drv.dev.SendUDPData(drv.sock); err != nil {
|
if _, err := drv.dev.SendUDPData(drv.sock); err != nil {
|
||||||
return 0, fmt.Errorf("error in sendUDPData: %w", err)
|
return 0, errors.New("error in sendUDPData: " + err.Error())
|
||||||
}
|
}
|
||||||
return len(b), nil
|
return len(b), nil
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
+23
-9
@@ -8,7 +8,10 @@ package wifinina // import "tinygo.org/x/drivers/wifinina"
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"fmt"
|
"encoding/hex"
|
||||||
|
"fmt" // used only in debug printouts and is optimized out when debugging is disabled
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"machine"
|
"machine"
|
||||||
@@ -213,15 +216,16 @@ func (addr IPAddress) String() string {
|
|||||||
if len(addr) < 4 {
|
if len(addr) < 4 {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
return fmt.Sprintf("%d.%d.%d.%d", addr[0], addr[1], addr[2], addr[3])
|
return strconv.Itoa(int(addr[0])) + "." + strconv.Itoa(int(addr[1])) + "." + strconv.Itoa(int(addr[2])) + "." + strconv.Itoa(int(addr[3]))
|
||||||
}
|
}
|
||||||
|
|
||||||
func ParseIPv4(s string) (IPAddress, error) {
|
func ParseIPv4(s string) (IPAddress, error) {
|
||||||
var v0, v1, v2, v3 uint8
|
v := strings.Split(s, ".")
|
||||||
if _, err := fmt.Sscanf(s, "%d.%d.%d.%d", &v0, &v1, &v2, &v3); err != nil {
|
v0, _ := strconv.Atoi(v[0])
|
||||||
return "", err
|
v1, _ := strconv.Atoi(v[1])
|
||||||
}
|
v2, _ := strconv.Atoi(v[2])
|
||||||
return IPAddress([]byte{v0, v1, v2, v3}), nil
|
v3, _ := strconv.Atoi(v[3])
|
||||||
|
return IPAddress([]byte{byte(v0), byte(v1), byte(v2), byte(v3)}), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (addr IPAddress) AsUint32() uint32 {
|
func (addr IPAddress) AsUint32() uint32 {
|
||||||
@@ -235,13 +239,23 @@ func (addr IPAddress) AsUint32() uint32 {
|
|||||||
type MACAddress uint64
|
type MACAddress uint64
|
||||||
|
|
||||||
func (addr MACAddress) String() string {
|
func (addr MACAddress) String() string {
|
||||||
return fmt.Sprintf("%016X", uint64(addr))
|
b := make([]byte, 8)
|
||||||
|
binary.BigEndian.PutUint64(b, uint64(addr))
|
||||||
|
encoded := hex.EncodeToString(b)
|
||||||
|
result := ""
|
||||||
|
for i := 2; i < 8; i++ {
|
||||||
|
result += encoded[i*2 : i*2+2]
|
||||||
|
if i < 7 {
|
||||||
|
result += ":"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
type Error uint8
|
type Error uint8
|
||||||
|
|
||||||
func (err Error) Error() string {
|
func (err Error) Error() string {
|
||||||
return fmt.Sprintf("wifinina error: 0x%02X", uint8(err))
|
return "wifinina error: 0x" + hex.EncodeToString([]byte{uint8(err)})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Cmd Struct Message */
|
// Cmd Struct Message */
|
||||||
|
|||||||
Reference in New Issue
Block a user