Guest User

Untitled

a guest
Jun 21st, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.30 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "fmt"
  5. "time"
  6.  
  7. "github.com/davecheney/gpio"
  8. )
  9.  
  10. // AdcRead represents the data needed to perform a read operation on the ADC component
  11. type AdcRead struct {
  12. Cs gpio.Pin
  13. Clock gpio.Pin
  14. Miso gpio.Pin
  15. NumBits int
  16. ResultsChan chan uint32
  17. }
  18.  
  19. // Exec reads the current value stored in the ADC register
  20. func (reader AdcRead) Exec() {
  21. // Start the CS Low to begin the read
  22. reader.Cs.Clear()
  23.  
  24. // Initialize an impty uint32 to store the value we are reading
  25. var result uint32
  26.  
  27. // Loop over each bit the the component sends back (The number depends varies from
  28. // component to component, read the datasheet)
  29. for i := 0; i < reader.NumBits; i++ {
  30.  
  31. // Set the clock to logic high
  32. reader.Clock.Set()
  33.  
  34. // Read 1 bit in, if it is high, then add a "1" to our rightmost bit
  35. bit := reader.Miso.Get()
  36. if bit {
  37. result |= 0x1
  38. }
  39.  
  40. // Shift Left to get to the next bit to be read
  41. if i != reader.NumBits-1 {
  42. result <<= 1
  43. }
  44.  
  45. // The clock will pulse low, then high again to get the next bit
  46. reader.Clock.Clear()
  47. }
  48.  
  49. // Set chip select low to end the read
  50. reader.Cs.Set()
  51.  
  52. // Send the result back through the channel to whatever part of our
  53. // application cares about it
  54. reader.ResultsChan <- result
  55. }
  56.  
  57. func main() {
  58. // Open necessary pins. The numbers here are examples, they should be changed based
  59. // on which pins you use
  60. const csPinNumber = 5
  61. const clockPinNumber = 6
  62. const misoPinNumber = 7
  63.  
  64. csPin, err := gpio.OpenPin(csPinNumber, gpio.ModeOutput)
  65. if err != nil {
  66. fmt.Printf("Error opening cs pin: %v\n", err)
  67. }
  68. clockPin, err := gpio.OpenPin(clockPinNumber, gpio.ModeOutput)
  69. if err != nil {
  70. fmt.Printf("Error opening clock pin: %v\n", err)
  71. }
  72. misoPin, err := gpio.OpenPin(misoPinNumber, gpio.ModeInput)
  73. if err != nil {
  74. fmt.Printf("Error opening miso pin: %v\n", err)
  75. }
  76.  
  77. resultsChan := make(chan uint32, 1)
  78.  
  79. adcReader := AdcRead{
  80. Cs: csPin,
  81. Clock: clockPin,
  82. Miso: misoPin,
  83. NumBits: 32, // Our ADC component sends a 32 bit value
  84. ResultsChan: resultsChan,
  85. }
  86.  
  87. // Execute each read at 10 Hz
  88. c := time.Tick(time.Duration(100) * time.Millisecond)
  89. go func() {
  90. for range c {
  91. adcReader.Exec()
  92. }
  93. }()
  94.  
  95. // Print everything that comes through the results channel
  96. for true {
  97. result := <-resultsChan
  98. fmt.Println(result)
  99. }
  100. }
Add Comment
Please, Sign In to add comment