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

     1  //go:build cortexm
     2  
     3  package ws2812
     4  
     5  // This file implements the WS2812 protocol for various Cortex-M
     6  // microcontrollers. It is intended to work with various variants: M0, M0+, M3,
     7  // and M4. Because machine.CPUFrequency() is usually a constant, the value will
     8  // usually be constant-propagated and the switch below will be a direct
     9  // (inlinable function) - thus there is usually no code size penalty over build
    10  // tags per CPU speed.
    11  
    12  import (
    13  	"machine"
    14  )
    15  
    16  // Send a single byte using the WS2812 protocol.
    17  func (d Device) WriteByte(c byte) error {
    18  	switch machine.CPUFrequency() {
    19  	case 16_000_000: // 16MHz
    20  		d.writeByte16(c)
    21  		return nil
    22  	case 48_000_000: // 48MHz
    23  		d.writeByte48(c)
    24  		return nil
    25  	case 64_000_000: // 64MHz
    26  		d.writeByte64(c)
    27  		return nil
    28  	case 120_000_000: // 120MHz
    29  		d.writeByte120(c)
    30  		return nil
    31  	case 125_000_000: // 125 MHz e.g. rp2040
    32  		d.writeByte125(c)
    33  		return nil
    34  	case 168_000_000: // 168MHz, e.g. stm32f405
    35  		d.writeByte168(c)
    36  		return nil
    37  	default:
    38  		return errUnknownClockSpeed
    39  	}
    40  }