tinygo.org/x/drivers@v0.27.1-0.20240509133757-7dbca2a54349/examples/ili9341/slideshow/main.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"image/color"
     6  	"strings"
     7  	"time"
     8  
     9  	"tinygo.org/x/drivers/examples/ili9341/initdisplay"
    10  	"tinygo.org/x/drivers/ili9341"
    11  	"tinygo.org/x/drivers/image/jpeg"
    12  	"tinygo.org/x/drivers/image/png"
    13  )
    14  
    15  var (
    16  	black = color.RGBA{0, 0, 0, 255}
    17  	white = color.RGBA{255, 255, 255, 255}
    18  	red   = color.RGBA{255, 0, 0, 255}
    19  	blue  = color.RGBA{0, 0, 255, 255}
    20  	green = color.RGBA{0, 255, 0, 255}
    21  )
    22  
    23  var (
    24  	display *ili9341.Device
    25  )
    26  
    27  func main() {
    28  	err := run()
    29  	for err != nil {
    30  		errorMessage(err)
    31  	}
    32  }
    33  
    34  func run() error {
    35  	display = initdisplay.InitDisplay()
    36  
    37  	width, height := display.Size()
    38  	if width < 320 || height < 240 {
    39  		display.SetRotation(ili9341.Rotation270)
    40  	}
    41  
    42  	display.FillScreen(black)
    43  
    44  	for {
    45  		err := drawJpeg(display)
    46  		if err != nil {
    47  			return err
    48  		}
    49  		time.Sleep(time.Second)
    50  
    51  		err = drawPng(display)
    52  		if err != nil {
    53  			return err
    54  		}
    55  		time.Sleep(time.Second)
    56  	}
    57  
    58  	return nil
    59  }
    60  
    61  // Define the buffer required for the callback. In most cases, this setting
    62  // should be sufficient.  For jpeg, the callback will always be called every
    63  // 3*8*8*4 pix. png will be called every line, i.e. every width pix.
    64  var buffer [3 * 8 * 8 * 4]uint16
    65  
    66  func drawPng(display *ili9341.Device) error {
    67  	p := strings.NewReader(pngImage)
    68  	png.SetCallback(buffer[:], func(data []uint16, x, y, w, h, width, height int16) {
    69  		err := display.DrawRGBBitmap(x, y, data[:w*h], w, h)
    70  		if err != nil {
    71  			errorMessage(fmt.Errorf("error drawPng: %s", err))
    72  		}
    73  	})
    74  
    75  	_, err := png.Decode(p)
    76  	return err
    77  }
    78  
    79  func drawJpeg(display *ili9341.Device) error {
    80  	p := strings.NewReader(jpegImage)
    81  	jpeg.SetCallback(buffer[:], func(data []uint16, x, y, w, h, width, height int16) {
    82  		err := display.DrawRGBBitmap(x, y, data[:w*h], w, h)
    83  		if err != nil {
    84  			errorMessage(fmt.Errorf("error drawJpeg: %s", err))
    85  		}
    86  	})
    87  
    88  	_, err := jpeg.Decode(p)
    89  	return err
    90  }
    91  
    92  func errorMessage(err error) {
    93  	for {
    94  		fmt.Printf("%s\r\n", err.Error())
    95  		time.Sleep(5 * time.Second)
    96  	}
    97  }