tinygo.org/x/drivers@v0.27.1-0.20240509133757-7dbca2a54349/examples/max72xx/main.go (about) 1 package main 2 3 import ( 4 "machine" 5 "time" 6 7 "tinygo.org/x/drivers/max72xx" 8 ) 9 10 // example for a 4 digit 7 segment display 11 func main() { 12 // Pins for Arduino Nano 33 IOT 13 err := machine.SPI0.Configure(machine.SPIConfig{ 14 SDO: machine.D11, // default SDO pin 15 SCK: machine.D13, // default sck pin 16 LSBFirst: false, 17 Frequency: 10000000, 18 }) 19 20 if err != nil { 21 println(err.Error()) 22 } 23 24 driver := max72xx.NewDevice(machine.SPI0, machine.D6) 25 driver.Configure() 26 27 digitNumber := 4 28 29 driver.StopDisplayTest() 30 driver.SetDecodeMode(4) 31 driver.SetScanLimit(4) 32 driver.SetIntensity(8) 33 driver.StopShutdownMode() 34 35 for i := 1; i < int(digitNumber); i++ { 36 driver.WriteCommand(byte(i), byte(Blank)) 37 } 38 39 for { 40 for _, character := range characters { 41 println("writing", "characterValue:", character.String()) 42 driver.WriteCommand(byte(4), byte(character)) 43 driver.WriteCommand(byte(3), byte(character)) 44 driver.WriteCommand(byte(2), byte(character)) 45 driver.WriteCommand(byte(1), byte(character)) 46 time.Sleep(500 * time.Millisecond) 47 48 } 49 time.Sleep(time.Second) 50 } 51 52 } 53 54 var characters = []Character{ 55 Zero, 56 One, 57 Two, 58 Three, 59 Four, 60 Five, 61 Six, 62 Seven, 63 Eight, 64 Nine, 65 Dash, 66 E, 67 H, 68 L, 69 P, 70 Blank, 71 Dot, 72 } 73 74 // Each bit translates to a pin, which is driven high or low 75 type Character byte 76 77 func (char Character) String() string { 78 switch char { 79 case Zero: 80 return "0" 81 case One: 82 return "1" 83 case Two: 84 return "2" 85 case Three: 86 return "3" 87 case Four: 88 return "4" 89 case Five: 90 return "5" 91 case Six: 92 return "6" 93 case Seven: 94 return "7" 95 case Eight: 96 return "8" 97 case Nine: 98 return "9" 99 case Dash: 100 return "-" 101 case E: 102 return "E" 103 case H: 104 return "H" 105 case L: 106 return "L" 107 case P: 108 return "P" 109 case Blank: 110 return "" 111 case Dot: 112 return "." 113 } 114 115 return "" 116 } 117 118 const ( 119 Zero Character = 0 //126 120 One Character = 1 //48 121 Two Character = 2 // 109 122 Three Character = 3 // 121 123 Four Character = 4 124 Five Character = 5 125 Six Character = 6 126 Seven Character = 7 127 Eight Character = 8 128 Nine Character = 9 129 Dash Character = 10 130 E Character = 11 131 H Character = 12 132 L Character = 13 133 P Character = 14 134 Blank Character = 15 135 Dot Character = 128 136 )