gobot.io/x/gobot/v2@v2.1.0/drivers/gpio/max7219_driver.go (about) 1 package gpio 2 3 import ( 4 "gobot.io/x/gobot/v2" 5 ) 6 7 // Access and command constants for the driver 8 const ( 9 MAX7219Digit0 = 0x01 10 MAX7219Digit1 = 0x02 11 MAX7219Digit2 = 0x03 12 MAX7219Digit3 = 0x04 13 MAX7219Digit4 = 0x05 14 MAX7219Digit5 = 0x06 15 MAX7219Digit6 = 0x07 16 MAX7219Digit7 = 0x08 17 18 MAX7219DecodeMode = 0x09 19 MAX7219Intensity = 0x0a 20 MAX7219ScanLimit = 0x0b 21 MAX7219Shutdown = 0x0c 22 MAX7219DisplayTest = 0x0f 23 ) 24 25 // MAX7219Driver is the gobot driver for the MAX7219 LED driver 26 // 27 // Datasheet: https://datasheets.maximintegrated.com/en/ds/MAX7219-MAX7221.pdf 28 type MAX7219Driver struct { 29 pinClock *DirectPinDriver 30 pinData *DirectPinDriver 31 pinCS *DirectPinDriver 32 name string 33 count uint 34 connection gobot.Connection 35 gobot.Commander 36 } 37 38 // NewMAX7219Driver return a new MAX7219Driver given a gobot.Connection, pins and how many chips are chained 39 func NewMAX7219Driver(a gobot.Connection, clockPin string, dataPin string, csPin string, count uint) *MAX7219Driver { 40 t := &MAX7219Driver{ 41 name: gobot.DefaultName("MAX7219Driver"), 42 pinClock: NewDirectPinDriver(a, clockPin), 43 pinData: NewDirectPinDriver(a, dataPin), 44 pinCS: NewDirectPinDriver(a, csPin), 45 count: count, 46 connection: a, 47 Commander: gobot.NewCommander(), 48 } 49 50 /* TODO : Add commands */ 51 52 return t 53 } 54 55 // Start initializes the max7219, it uses a SPI-like communication protocol 56 func (a *MAX7219Driver) Start() (err error) { 57 a.pinData.On() 58 a.pinClock.On() 59 a.pinCS.On() 60 61 a.All(MAX7219ScanLimit, 0x07) 62 a.All(MAX7219DecodeMode, 0x00) 63 a.All(MAX7219Shutdown, 0x01) 64 a.All(MAX7219DisplayTest, 0x00) 65 a.ClearAll() 66 a.All(MAX7219Intensity, 0x0f) 67 68 return 69 } 70 71 // Halt implements the Driver interface 72 func (a *MAX7219Driver) Halt() (err error) { return } 73 74 // Name returns the MAX7219Drivers name 75 func (a *MAX7219Driver) Name() string { return a.name } 76 77 // SetName sets the MAX7219Drivers name 78 func (a *MAX7219Driver) SetName(n string) { a.name = n } 79 80 // Connection returns the MAX7219Driver Connection 81 func (a *MAX7219Driver) Connection() gobot.Connection { 82 return a.connection 83 } 84 85 // SetIntensity changes the intensity (from 1 to 7) of the display 86 func (a *MAX7219Driver) SetIntensity(level byte) { 87 if level > 15 { 88 level = 15 89 } 90 a.All(MAX7219Intensity, level) 91 } 92 93 // ClearAll turns off all LEDs of all modules 94 func (a *MAX7219Driver) ClearAll() { 95 for i := 1; i <= 8; i++ { 96 a.All(byte(i), 0) 97 } 98 } 99 100 // ClearOne turns off all LEDs of the given module 101 func (a *MAX7219Driver) ClearOne(which uint) { 102 for i := 1; i <= 8; i++ { 103 a.One(which, byte(i), 0) 104 } 105 } 106 107 // sendData is an auxiliary function to send data to the MAX7219Driver module 108 func (a *MAX7219Driver) sendData(address byte, data byte) { 109 a.pinCS.Off() 110 a.send(address) 111 a.send(data) 112 a.pinCS.On() 113 } 114 115 // send writes data on the module 116 func (a *MAX7219Driver) send(data byte) { 117 var i byte 118 for i = 8; i > 0; i-- { 119 mask := byte(0x01 << (i - 1)) 120 121 a.pinClock.Off() 122 if data&mask > 0 { 123 a.pinData.On() 124 } else { 125 a.pinData.Off() 126 } 127 a.pinClock.On() 128 } 129 } 130 131 // All sends the same data to all the modules 132 func (a *MAX7219Driver) All(address byte, data byte) { 133 a.pinCS.Off() 134 var c uint 135 for c = 0; c < a.count; c++ { 136 a.send(address) 137 a.send(data) 138 } 139 a.pinCS.On() 140 } 141 142 // One sends data to a specific module 143 func (a *MAX7219Driver) One(which uint, address byte, data byte) { 144 a.pinCS.Off() 145 var c uint 146 for c = 0; c < a.count; c++ { 147 if c == which { 148 a.send(address) 149 a.send(data) 150 } else { 151 a.send(0) 152 a.send(0) 153 } 154 } 155 a.pinCS.On() 156 }