Skip to content

Commit

Permalink
Update all Display examples, and ADS1118
Browse files Browse the repository at this point in the history
  • Loading branch information
vickash committed Oct 2, 2024
1 parent 8a1fb46 commit 57b53b9
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 63 deletions.
14 changes: 3 additions & 11 deletions examples/analog_io/ads1118.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,8 @@
require 'denko'

board = Denko::Board.new(Denko::Connection::Serial.new)

# Connect the ADS1118 pins to the board's default SPI pins.
bus = Denko::SPI::Bus.new(board: board)

# Or use a 2-way bit-bang SPI bus on any pins.
# SPI_BIT_BANG_PINS = { clock: 13, input: 12, output: 11 }
# bus = Denko::SPI::BitBang.new(board: board, pins: SPI_BIT_BANG_PINS)

# Connect chip select/enable pin of the ADS1118 to pin 9.
ads = Denko::AnalogIO::ADS1118.new(bus: bus, pin: 9)
bus = Denko::SPI::Bus.new(board: board)
ads = Denko::AnalogIO::ADS1118.new(bus: bus, pin: 10)

# Helper method so readings look nice.
def print_reading(name, raw, voltage)
Expand All @@ -29,7 +21,7 @@ def print_reading(name, raw, voltage)
#
# Read the ADS1118 internal temperature sensor.
# This always uses the 128 SPS mode, and there is no polling method for it.
#
#
temperature = ads.temperature_read
puts "ADS1118 Temperature: #{temperature} \xC2\xB0C"
puts
Expand Down
18 changes: 13 additions & 5 deletions examples/display/hd44780.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,19 @@
require 'bundler/setup'
require 'denko'

# These pins match the SainSmart LCD Keypad Shield for Arduino boards.
RS = 8
EN = 9
D4 = 4
D5 = 5
D6 = 6
D7 = 7

board = Denko::Board.new(Denko::Connection::Serial.new)
lcd = Denko::Display::HD44780.new board: board,
pins: { rs: 8, enable: 9, d4: 4, d5: 5, d6: 6, d7: 7 },
cols: 16,
rows: 2
lcd = Denko::Display::HD44780.new board: board,
pins: { rs: RS, enable: EN, d4: D4, d5: D5, d6: D6, d7: D7 },
cols: 16,
rows: 2

# Bitmap for a custom character. 5 bits wide x 8 high.
# Useful for generating these: https://omerk.github.io/lcdchargen/
Expand All @@ -20,7 +28,7 @@
0b01110,
0b00100,
0b00000 ]

# Define the character in CGRAM address 2. 0-7 are usable.
lcd.create_char(2, heart)

Expand Down
41 changes: 0 additions & 41 deletions examples/display/sh1106.rb

This file was deleted.

4 changes: 3 additions & 1 deletion examples/display/ssd1306.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@

# I2C OLED, connected to I2C SDA and SCL.
oled = Denko::Display::SSD1306.new(bus: bus, rotate: true) # address: 0x3C is default
# oled = Denko::Display::SH1106.new(bus: bus,rotate: true) # address: 0x3C is default

# SPI OLED, connected to SPI CLK and MOSI pins.
# select: and dc: pins must be given. reset is optional (can be pulled high instead).
# oled = Denko::Display::SSD1306.new(bus: bus, pins: {select: 10, dc: 7, reset: 8}, rotate: true)
# oled = Denko::Display::SSD1306.new(bus: bus, pins: { select: 10, dc: 7, reset: 8 }, rotate: true)
# oled = Denko::Display::SH1106.new(bus: bus, pins: { select: 10, dc: 7, reset: 8}, rotate: true)

# Draw some text on the OLED's canvas (a Ruby memory buffer).
canvas = oled.canvas
Expand Down
4 changes: 2 additions & 2 deletions examples/display/ssd1306_s2_pico.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
reset = Denko::DigitalIO::Output.new(board: board, pin: 18)
reset.high

bus = Denko::I2C::Bus.new(board: board, pin: :SDA)
oled = Denko::Display::SSD1306.new(bus: bus, width: 128, height: 32)
bus = Denko::I2C::Bus.new(board: board, pin: :SDA)
oled = Denko::Display::SSD1306.new(bus: bus, width: 128, height: 32)
canvas = oled.canvas

# Draw some text on the OLED's canvas (a Ruby memory buffer).
Expand Down
15 changes: 12 additions & 3 deletions lib/denko/spi/peripheral.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def spi_stop
bus.stop(select_pin)
end

def pre_callback_filter(message)
def string_to_bytes(message)
if message.class == Array
# Byte array coming from PiBoard.
return message
Expand All @@ -51,6 +51,11 @@ def pre_callback_filter(message)
return message.split(",").map { |b| b.to_i }
end
end

def update(message)
puts message
super(string_to_bytes(message))
end
end

module ChipSelectBehavior
Expand Down Expand Up @@ -91,11 +96,15 @@ module MultiPin
include Behaviors::Lifecycle

def select_pin
select.pin
self.select.pin
end

after_initialize do
def initialize_pins(params={})
super(params)
proxy_pin :select, ChipSelect, { board: bus.board, mode: :output }
end

after_initialize do
select.add_callback(:peripheral_forwarder) { |data| self.update(data) }
end
end
Expand Down

0 comments on commit 57b53b9

Please sign in to comment.