tinygo.org/x/drivers@v0.27.1-0.20240509133757-7dbca2a54349/cmd/convert2bin/convert2bin.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"log"
     7  	"os"
     8  	"strings"
     9  )
    10  
    11  // See ../../image/README.md for the usage.
    12  
    13  func main() {
    14  	err := run(os.Args)
    15  	if err != nil {
    16  		log.Fatal(err)
    17  	}
    18  }
    19  
    20  func run(args []string) error {
    21  	if len(args) < 2 {
    22  		return fmt.Errorf("usage: %s FILE")
    23  	}
    24  
    25  	b, err := ioutil.ReadFile(args[1])
    26  	if err != nil {
    27  		return err
    28  	}
    29  
    30  	fmt.Printf("const %s = \"\" +\n", strings.Replace(args[1], ".", "_", -1))
    31  
    32  	i := 0
    33  	max := 32
    34  	for i = 0; i < len(b); i++ {
    35  		bb := b[i]
    36  		if (i % max) == 0 {
    37  			fmt.Printf("	\"")
    38  		}
    39  		fmt.Printf("\\x%02X", bb)
    40  		if (i%max) == max-1 && i != len(b)-1 {
    41  			fmt.Printf("\" + \n")
    42  		}
    43  	}
    44  	if (i % max) < max-1 {
    45  		fmt.Printf("\"\n")
    46  	}
    47  
    48  	return nil
    49  }