tinygo.org/x/drivers@v0.27.1-0.20240509133757-7dbca2a54349/examples/shiftregister/main.go (about) 1 package main 2 3 import ( 4 "machine" 5 "time" 6 7 "tinygo.org/x/drivers/shiftregister" 8 ) 9 10 func main() { 11 d := shiftregister.New( 12 shiftregister.EIGHT_BITS, 13 machine.PA6, // D12 Pin latch connected to ST_CP of 74HC595 (12) 14 machine.PA7, // D11 Pin clock connected to SH_CP of 74HC595 (11) 15 machine.PB6, // D10 Pin data connected to DS of 74HC595 (14) 16 ) 17 d.Configure() 18 19 for { 20 21 // Examples using masks. This method writes all pins state at once. 22 23 // All pins High 24 d.WriteMask(0xFF) 25 delay() 26 27 // All pins Low 28 d.WriteMask(0x00) 29 delay() 30 31 // Some fun with masks 32 for _, pattern := range patterns { 33 d.WriteMask(pattern) 34 shortDelay() 35 } 36 delay() 37 d.WriteMask(0x00) 38 39 // Examples using individually addressable pin API. This method is slower than using mask 40 // because all register's pins state are send for is p.Set() call. 41 42 // Set register's pin #4 43 d.GetShiftPin(4).High() 44 delay() 45 d.GetShiftPin(4).Low() 46 delay() 47 48 // Get an individual pin and use it 49 pin := d.GetShiftPin(7) 50 pin.High() 51 delay() 52 pin.Low() 53 delay() 54 55 // Prepare an array of pin attached to the register 56 pins := [8]*shiftregister.ShiftPin{} 57 for p := 0; p < 8; p++ { 58 pins[p] = d.GetShiftPin(p) 59 } 60 61 for p := 7; p >= 0; p-- { 62 pins[p].Low() 63 shortDelay() 64 pins[p].High() 65 } 66 67 for p := 7; p >= 0; p-- { 68 pins[p].High() 69 time.Sleep(100 * time.Millisecond) 70 pins[p].Low() 71 } 72 delay() 73 } 74 } 75 76 func delay() { 77 time.Sleep(500 * time.Millisecond) 78 } 79 func shortDelay() { 80 time.Sleep(100 * time.Millisecond) 81 } 82 83 var patterns = []uint32{ 84 0b00000001, 85 0b00000010, 86 0b00000100, 87 0b00001000, 88 0b00010000, 89 0b00100000, 90 0b01000000, 91 0b10000000, 92 0b10000001, 93 0b10000010, 94 0b10000100, 95 0b10001000, 96 0b10010000, 97 0b10100000, 98 0b11000000, 99 0b11000001, 100 0b11000010, 101 0b11000100, 102 0b11001000, 103 0b11010000, 104 0b11100000, 105 0b11100001, 106 0b11100010, 107 0b11100100, 108 0b11101000, 109 0b11110000, 110 0b11110001, 111 0b11110010, 112 0b11110100, 113 0b11111000, 114 0b11111001, 115 0b11111010, 116 0b11111100, 117 0b11111101, 118 0b11111110, 119 0b11111111, 120 0b00000000, 121 0b11111111, 122 0b00000000, 123 0b11111111, 124 }