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