source stringclasses 1
value | repo stringlengths 5 63 | repo_url stringlengths 24 82 | path stringlengths 5 167 | language stringclasses 1
value | license stringclasses 5
values | stars int64 10 51.4k | ref stringclasses 23
values | size_bytes int64 200 258k | text stringlengths 137 258k |
|---|---|---|---|---|---|---|---|---|---|
github | denko-rb/denko | https://github.com/denko-rb/denko | lib/denko/i2c/peripheral.rb | Ruby | mit | 48 | master | 1,771 | module Denko
module I2C
module Peripheral
include Behaviors::BusPeripheralAddressed
include Behaviors::Reader
include Behaviors::Lifecycle
# Set I2C defaults for including classes by defining these constants in them.
I2C_ADDRESS = nil
I2C_FREQUENCY = 100_000
... |
github | denko-rb/denko | https://github.com/denko-rb/denko | lib/denko/i2c/bus.rb | Ruby | mit | 48 | master | 705 | module Denko
module I2C
class Bus
include Behaviors::MultiPin
include Behaviors::Lifecycle
include BusCommon
def i2c_index
@i2c_index ||= params[:i2c_index] || params[:index] || 0
end
def _search
board.i2c_search(i2c_index)
end
def write(address, ... |
github | denko-rb/denko | https://github.com/denko-rb/denko | lib/denko/i2c/bus_common.rb | Ruby | mit | 48 | master | 1,411 | module Denko
module I2C
module BusCommon
include Behaviors::BusControllerAddressed
include Behaviors::Reader
include Behaviors::Lifecycle
after_initialize do
bubble_callbacks
end
def found_devices
@found_devices ||= []
end
def search
addre... |
github | denko-rb/denko | https://github.com/denko-rb/denko | lib/denko/i2c/bit_bang.rb | Ruby | mit | 48 | master | 970 | module Denko
module I2C
class BitBang
include Behaviors::MultiPin
include Behaviors::Lifecycle
include BusCommon
def initialize_pins
proxy_pin :scl, DigitalIO::CBitBang
proxy_pin :sda, DigitalIO::CBitBang
end
after_initialize do
# Data received for the... |
github | denko-rb/denko | https://github.com/denko-rb/denko | lib/denko/uart/common.rb | Ruby | mit | 48 | master | 480 | module Denko
module UART
module Common
def initialize_buffer
flush
self.add_callback(:buffer) do |data|
@buffer = "#{@buffer}#{data}"
end
end
def gets
line = nil
newline = @buffer.index("\n")
if newline
line = @buffer[0..newl... |
github | denko-rb/denko | https://github.com/denko-rb/denko | lib/denko/uart/bit_bang.rb | Ruby | mit | 48 | master | 891 | module Denko
module UART
class UARTRxPin
include Behaviors::InputPin
include Behaviors::Callbacks
include Behaviors::Lifecycle
end
class BitBang
include Behaviors::MultiPin
include Behaviors::Callbacks
include Behaviors::Lifecycle
include Common
attr_reade... |
github | denko-rb/denko | https://github.com/denko-rb/denko | lib/denko/uart/hardware.rb | Ruby | mit | 48 | master | 1,007 | module Denko
module UART
class Hardware
include Behaviors::SinglePin
include Behaviors::Callbacks
include Behaviors::Lifecycle
include Common
DEFAULT_BAUD = 9600
DEFAULT_CONFIG = "8N1"
attr_reader :index, :baud, :config
before_initialize do
raise Argume... |
github | denko-rb/denko | https://github.com/denko-rb/denko | lib/denko/led/seven_segment_array.rb | Ruby | mit | 48 | master | 1,292 | module Denko
module LED
module SevenSegmentArray
include Behaviors::OutputRegister
include Behaviors::Lifecycle
DIGIT_COUNT = 1
def digit_count
@digit_count = params[:digits] || self.class::DIGIT_COUNT
end
attr_reader :digits, :colon
after_initialize do
... |
github | denko-rb/denko | https://github.com/denko-rb/denko | lib/denko/led/rgb.rb | Ruby | mit | 48 | master | 967 | module Denko
module LED
class RGB
include Behaviors::MultiPin
def initialize_pins
proxy_pin :red, LED::Base
proxy_pin :green, LED::Base
proxy_pin :blue, LED::Base
end
# Format: [R, G, B]
COLORS = {
red: [100, 000, 000],
green: [000, ... |
github | denko-rb/denko | https://github.com/denko-rb/denko | lib/denko/led/seven_segment.rb | Ruby | mit | 48 | master | 4,651 | module Denko
module LED
class SevenSegment
include Behaviors::MultiPin
include Behaviors::Lifecycle
ALL_OFF = [0,0,0,0,0,0,0]
BLANK = " "
def initialize_pins
[:a, :b, :c, :d, :e, :f, :g].each do |symbol|
proxy_pin(symbol, DigitalIO::Output)
end
if... |
github | denko-rb/denko | https://github.com/denko-rb/denko | lib/denko/led/apa102.rb | Ruby | mit | 48 | master | 2,596 | module Denko
module LED
class APA102
include Behaviors::BusPeripheral
include Behaviors::Lifecycle
def length
@length ||= params[:length] || 1
end
# Start frame is always 32 0-bits (4 bytes).
def start_frame
@start_frame ||= Array.new(4) { 0 }
end
... |
github | denko-rb/denko | https://github.com/denko-rb/denko | lib/denko/led/tm1638.rb | Ruby | mit | 48 | master | 1,811 | module Denko
module LED
class TM1638
include TM163x
include Behaviors::Lifecycle
BYTE_COUNT = 16
DIGIT_COUNT = 8
LED_COUNT = 8
def initialize_pins
proxy_pin :clk, DigitalIO::CBitBang
proxy_pin :dio, DigitalIO::CBitBang
proxy_pin :stb, DigitalIO::Out... |
github | denko-rb/denko | https://github.com/denko-rb/denko | lib/denko/led/ws2812.rb | Ruby | mit | 48 | master | 893 | module Denko
module LED
class WS2812
include Behaviors::SinglePin
include Behaviors::Lifecycle
def length
@length ||= params[:length] || 1
end
def bpp
@bpp ||= params[:bpp] || 3
end
attr_writer :length, :bpp
after_initialize do
off
... |
github | denko-rb/denko | https://github.com/denko-rb/denko | lib/denko/led/base.rb | Ruby | mit | 48 | master | 476 | module Denko
module LED
class Base < PulseIO::PWMOutput
def blink(interval=0.5)
self.blink_interval = interval
threaded_loop do
toggle
sleep @blink_interval
end
end
def blink_interval=(interval)
@blink_interval = interval
end
attr_... |
github | denko-rb/denko | https://github.com/denko-rb/denko | lib/denko/led/seven_segment_spi.rb | Ruby | mit | 48 | master | 1,310 | module Denko
module LED
class SevenSegmentSPI < SPI::OutputRegister
# Model 1+ SevenSegment, through 1+ series shift registers, like 4HC595.
include SevenSegmentArray
before_initialize do
params[:bytes] ||= params[:digits]
params[:inverted] ||= params[:invert]
end
... |
github | denko-rb/denko | https://github.com/denko-rb/denko | lib/denko/led/tm1637.rb | Ruby | mit | 48 | master | 2,489 | module Denko
module LED
class TM1637
include TM163x
include Behaviors::Lifecycle
BYTE_COUNT = 4
DIGIT_COUNT = 4
def initialize_pins
proxy_pin :clk, DigitalIO::Output
proxy_pin :dio, DigitalIO::Output
end
after_initialize do
# Bus idle condition... |
github | denko-rb/denko | https://github.com/denko-rb/denko | lib/denko/led/tm163x.rb | Ruby | mit | 48 | master | 1,528 | module Denko
module LED
module TM163x
include Behaviors::MultiPin
include SevenSegmentArray
# Always write all bytes, auto incrementing, starting at address 0.
SET_DATA = 0b01 << 6
SET_ADDRESS = 0b11 << 6
DISPLAY_CONTROL = 0b10 << 6
ON_BIT = 0b1 << 3
BRIGHTNESS... |
github | denko-rb/denko | https://github.com/denko-rb/denko | lib/denko/led/tm1652.rb | Ruby | mit | 48 | master | 4,125 | module Denko
module LED
class TM1652
include Behaviors::Component
include SevenSegmentArray
include Behaviors::Lifecycle
PREFIX_CONTROL = 0b00011000
PREFIX_ADDRESS = 0b00001000
DISPLAY_CONTROL = 0b00000000
DUTY_RATIOS = {
0 => 0b0000,
1 => 0b1000,
... |
github | denko-rb/denko | https://github.com/denko-rb/denko | lib/denko/pulse_io/pwm_output.rb | Ruby | mit | 48 | master | 2,879 | module Denko
module PulseIO
class PWMOutput < DigitalIO::Output
include Behaviors::Lifecycle
interrupt_with :write, :pwm_write, :digital_write, :duty=
# Avoid setting :output mode if on PWM pin, and on a platform that muxes externally.
before_initialize do
b = params[:board]
... |
github | denko-rb/denko | https://github.com/denko-rb/denko | lib/denko/pulse_io/ir_output.rb | Ruby | mit | 48 | master | 683 | module Denko
module PulseIO
class IROutput
include Behaviors::SinglePin
include Behaviors::Lifecycle
def write(pulses=[], frequency: 38)
if pulses.length > 255 || pulses.length < 1
raise ArgumentError, 'wrong number of IR pulses (expected 1 to 255)'
end
pulses... |
github | denko-rb/denko | https://github.com/denko-rb/denko | lib/denko/pulse_io/buzzer.rb | Ruby | mit | 48 | master | 488 | module Denko
module PulseIO
class Buzzer < PWMOutput
include Behaviors::Lifecycle
before_initialize do
params[:mode] ||= :output_pwm
end
after_initialize do
board.no_tone(pin)
end
# Duration is in mills
def tone(frequency, duration=nil)
board.to... |
github | denko-rb/denko | https://github.com/denko-rb/denko | lib/denko/one_wire/bus.rb | Ruby | mit | 48 | master | 1,885 | module Denko
module OneWire
class Bus < DigitalIO::CBitBang
include Behaviors::BusControllerAddressed
include Behaviors::Reader
include Behaviors::Lifecycle
include BusEnumerator
include Constants
attr_reader :parasite_power
after_initialize do
read_power_supply... |
github | denko-rb/denko | https://github.com/denko-rb/denko | lib/denko/one_wire/bus_enumerator.rb | Ruby | mit | 48 | master | 3,264 | module Denko
module OneWire
module BusEnumerator
include Constants
def _search(branch_mask)
reset
write(SEARCH_ROM)
board.one_wire_search(pin, branch_mask)
end
def found_devices
@found_devices ||= []
end
def search
@found_devices = []
... |
github | denko-rb/denko | https://github.com/denko-rb/denko | lib/denko/one_wire/peripheral.rb | Ruby | mit | 48 | master | 1,256 | module Denko
module OneWire
class Peripheral
include Behaviors::BusPeripheralAddressed
include Behaviors::Poller
include Constants
def read_scratch(num_bytes, &block)
atomically do
match
bus.write(READ_SCRATCH)
bus.read(num_bytes)
block.call... |
github | denko-rb/denko | https://github.com/denko-rb/denko | lib/denko/one_wire/constants.rb | Ruby | mit | 48 | master | 410 | module Denko
module OneWire
module Constants
READ_POWER_SUPPLY = 0xB4
CONVERT_T = 0x44
SEARCH_ROM = 0xF0
READ_ROM = 0x33
SKIP_ROM = 0xCC
MATCH_ROM = 0x55
ALARM_SEARCH = 0xEC
READ_SCRATCH = 0xBE
WRITE_SCRATCH ... |
github | denko-rb/denko | https://github.com/denko-rb/denko | lib/denko/one_wire/helper.rb | Ruby | mit | 48 | master | 847 | module Denko
module OneWire
class Helper
def self.address_to_bytes(address)
bytes = []
8.times { |i| bytes[i] = address >> (8*i) & 0xFF }
bytes
end
def self.crc(data)
calculated, received = self.calculate_crc(data)
calculated == received
end
... |
github | denko-rb/denko | https://github.com/denko-rb/denko | lib/denko/board/uart.rb | Ruby | mit | 48 | master | 1,777 | module Denko
class Board
UART_BAUDS = [
300, 600, 750, 1200, 2400, 4800, 9600, 19200, 31250, 38400, 57600, 74880, 115200, 230400
]
UART_CONFIGS = [ "8N1", "8E1", "8O1", "8N2", "8E2", "8O2" ]
# CMD = 14
def uart_start(index, baud=9600, config="8N1", listening=true)
raise ArgumentError... |
github | denko-rb/denko | https://github.com/denko-rb/denko | lib/denko/board/led_array.rb | Ruby | mit | 48 | master | 580 | module Denko
class Board
def show_ws2812(pin, pixel_buffer)
# ALWAYS have first 4 bytes set to 0. ESP32 crashes without this!
# Last 4 bytes will be settings, but not yet. Just 24-bit 800 kHz for now.
settings = pack :uint8, [0, 0, 0, 0, 0, 0, 0, 0]
pixel_bytes = pack :uint8, pixel_buf... |
github | denko-rb/denko | https://github.com/denko-rb/denko | lib/denko/board/eeprom.rb | Ruby | mit | 48 | master | 552 | module Denko
class Board
# CMD = 7
def eeprom_read(address, num_bytes)
address = pack :uint16, address
write Message.encode command: 7,
value: num_bytes,
aux_message: address
end
# CMD = 8
def eeprom_write(address, bytes)
... |
github | denko-rb/denko | https://github.com/denko-rb/denko | lib/denko/board/spi.rb | Ruby | mit | 48 | master | 3,225 | module Denko
class Board
# Use all of the board's aux buffer, except 8 bytes for header, or default to 512.
# NOTE: SPI lengths are sent as 12-bit, so hard upper limit of 4095.
def spi_limit
@spi_limit ||= aux_limit ? aux_limit-8 : 512
end
def spi_header_generic(select_pin, write, read, mod... |
github | denko-rb/denko | https://github.com/denko-rb/denko | lib/denko/board/i2c_bit_bang.rb | Ruby | mit | 48 | master | 2,023 | module Denko
class Board
def i2c_bb_setup(scl, sda)
# Stub method. Other implementations may call this.
end
# CMD = 30
def i2c_bb_search(scl, sda)
write Message.encode command: 30,
pin: scl,
value: sda
end
# CMD = 31
... |
github | denko-rb/denko | https://github.com/denko-rb/denko | lib/denko/board/map.rb | Ruby | mit | 48 | master | 1,638 | require 'yaml'
module Denko
class Board
MAPS_FOLDER = File.join(Denko.root, "vendor/board-maps/yaml")
attr_reader :map
def substitute_zero_pins
["SDA", "SCL", "MOSI", "MISO", "SCK", "SS"].each do |name|
symbol = name.to_sym
zero_symbol = (name + "0").to_sym
@map[symbo... |
github | denko-rb/denko | https://github.com/denko-rb/denko | lib/denko/board/uart_bit_bang.rb | Ruby | mit | 48 | master | 994 | module Denko
class Board
# CMD = 12
def uart_bb_start(tx, rx, baud, listening=true)
config = 0b01000000
config |= 0b10000000 if listening
self.write Message.encode command: 12,
pin: tx,
value: rx,
... |
github | denko-rb/denko | https://github.com/denko-rb/denko | lib/denko/board/servo.rb | Ruby | mit | 48 | master | 524 | module Denko
class Board
# CMD = 10
def servo_toggle(pin, value=:off, min: 544, max: 2400)
write Message.encode command: 10,
pin: pin,
value: (value == :off) ? 0 : 1,
aux_message: pack(:uint16, [min, max])
end
... |
github | denko-rb/denko | https://github.com/denko-rb/denko | lib/denko/board/tone.rb | Ruby | mit | 48 | master | 752 | module Denko
class Board
# CMD = 17
def tone(pin, frequency, duration=nil)
raise ArgumentError, "Tone cannot generate frequencies lower than 31Hz" if frequency < 31
raise ArgumentError, "Tone duration cannot be more than 65535 milliseconds" if (duration && (duration > 0xFFFF))
# Pack th... |
github | denko-rb/denko | https://github.com/denko-rb/denko | lib/denko/board/i2c.rb | Ruby | mit | 48 | master | 2,417 | module Denko
class Board
I2C_FREQUENCIES = {
100000 => 0x00,
400000 => 0x01,
1000000 => 0x02,
3400000 => 0x03,
}
attr_reader :i2c_limit
def i2c_convert_frequency(freq)
# Default to 100 kHz.
freq = 100000 unless freq
unless I2C_FREQUENCIES.include?(freq)
... |
github | denko-rb/denko | https://github.com/denko-rb/denko | lib/denko/board/infrared.rb | Ruby | mit | 48 | master | 574 | module Denko
class Board
def infrared_emit(pin, frequency, pulses)
#
# Limit to 255 marks/spaces (not pairs).
#
# Make length uint16 as well, for aligned memory access on ESP8266.
# Pulses max is 2x255 bytes long since each is 2 bytes.
length = pack :uint16, pulses.length, max... |
github | denko-rb/denko | https://github.com/denko-rb/denko | lib/denko/board/spi_bit_bang.rb | Ruby | mit | 48 | master | 1,834 | module Denko
class Board
def spi_bb_header(clock, input, output, select_pin, write, read, mode=0, bit_order=:msbfirst)
raise ArgumentError, "either output or input pin required" unless (input || output)
raise ArgumentError, "clock pin required" unless clock
# Set the other to disabled if only o... |
github | denko-rb/denko | https://github.com/denko-rb/denko | lib/denko/board/bit_bang_io.rb | Ruby | mit | 48 | master | 1,708 | module Denko
class Board
# CMD = 9
def pulse_read(pin, reset: false, reset_time: 0, pulse_limit: 100, timeout: 200)
# Validation
raise ArgumentError, "error in reset: #{reset}. Should be either #{high} or #{low}" if reset && ![high, low].include?(reset)
raise ArgumentError, "errror i... |
github | denko-rb/denko | https://github.com/denko-rb/denko | lib/denko/board/one_wire.rb | Ruby | mit | 48 | master | 1,068 | module Denko
class Board
def one_wire_reset(pin, read_presence=false)
write Message.encode command: 41,
pin: pin,
value: read_presence ? 1 : 0
end
def one_wire_search(pin, branch_mask)
write Message.encode command: 42,
... |
github | denko-rb/denko | https://github.com/denko-rb/denko | lib/denko/board/core.rb | Ruby | mit | 48 | master | 5,572 | module Denko
class Board
DIVIDERS = [1, 2, 4, 8, 16, 32, 64, 128]
PIN_MODES = {
output: 0b0000,
output_pwm: 0b0010,
output_dac: 0b0100,
output_open_drain: 0b0110,
output_open_source: 0b1000,... |
github | denko-rb/denko | https://github.com/denko-rb/denko | lib/denko/sensor/bme280.rb | Ruby | mit | 48 | master | 12,667 | module Denko
module Sensor
class BME280
include I2C::Peripheral
include Behaviors::Poller
include Behaviors::Lifecycle
include TemperatureHelper
include PressureHelper
include HumidityHelper
I2C_ADDRESS = 0x76
# Reading Mode Settings
SLEEP_MODE = 0b00
... |
github | denko-rb/denko | https://github.com/denko-rb/denko | lib/denko/sensor/hdc1080.rb | Ruby | mit | 48 | master | 4,643 | module Denko
module Sensor
class HDC1080
include I2C::Peripheral
include Behaviors::Poller
include Behaviors::Lifecycle
include TemperatureHelper
include HumidityHelper
I2C_ADDRESS = 0x40
# Config Register
CONFIG_ADDRESS = 0x02
CONFIG_DEFAULT = 0x10
RE... |
github | denko-rb/denko | https://github.com/denko-rb/denko | lib/denko/sensor/alspt19.rb | Ruby | mit | 48 | master | 1,973 | module Denko
module Sensor
class ALSPT19 < AnalogIO::Input
# 0.52 uA per lux (CIE standard illuminant)
# Taken from datasheet table: Electrical and Optical Characteristics (page 4).
LUX_PER_AMP = 1_000_000 / 0.520
# From datasheet Fig. 4, approximated to linear
VCC_COMP_GRADIENT = 0... |
github | denko-rb/denko | https://github.com/denko-rb/denko | lib/denko/sensor/vl53l0x.rb | Ruby | mit | 48 | master | 1,859 | module Denko
module Sensor
class VL53L0X
include I2C::Peripheral
include Behaviors::Poller
include Behaviors::Lifecycle
include Denko::AnalogIO::InputHelper
I2C_ADDRESS = 0x29
REFERENCE_REGISTER_START = 0xC0
REFERENCE_VALUES = [0xEE, 0xAA, 0x10]
SYSRANGE_START = ... |
github | denko-rb/denko | https://github.com/denko-rb/denko | lib/denko/sensor/tsl2561.rb | Ruby | mit | 48 | master | 3,724 | module Denko
module Sensor
class TSL2561
include I2C::Peripheral
include Behaviors::Poller
include Behaviors::Lifecycle
I2C_ADDRESS = 0x39
# These combine the appropriate write command (upper 4 bits), with the register address (lower 4 bits).
TIMING_REGISTER_ADDR = 0x81
... |
github | denko-rb/denko | https://github.com/denko-rb/denko | lib/denko/sensor/htu21d.rb | Ruby | mit | 48 | master | 4,569 | module Denko
module Sensor
class HTU21D
include I2C::Peripheral
include Behaviors::Poller
include Behaviors::Lifecycle
include TemperatureHelper
include HumidityHelper
I2C_ADDRESS = 0x40
# Commands
SOFT_RESET = 0xFE
WRITE_CONFIG =... |
github | denko-rb/denko | https://github.com/denko-rb/denko | lib/denko/sensor/helper.rb | Ruby | mit | 48 | master | 618 | module Denko
module Sensor
module TemperatureHelper
def temperature
@state[:temperature]
end
def temperature_f
(@state[:temperature] * 1.8 + 32).round(4)
end
def temperature_k
@state[:temperature] + 273.15
end
end
module PressureHelper
d... |
github | denko-rb/denko | https://github.com/denko-rb/denko | lib/denko/sensor/jsnsr04t.rb | Ruby | mit | 48 | master | 1,178 | module Denko
module Sensor
#
# For JSN-SR04T sensor in mode 2 ONLY.
#
class JSNSR04T
include Behaviors::Component
include Behaviors::Lifecycle
include Behaviors::Poller
UART_CLASSES = [Denko::UART::Hardware, Denko::UART::BitBang]
TIMEOUT = 0.500
attr_reader :... |
github | denko-rb/denko | https://github.com/denko-rb/denko | lib/denko/sensor/rcwl9620.rb | Ruby | mit | 48 | master | 642 | module Denko
module Sensor
class RCWL9620
include I2C::Peripheral
include Behaviors::Poller
I2C_ADDRESS = 0x57
def _read
i2c_write(0x01)
i2c_write(0x01)
sleep(0.120)
i2c_read(3)
end
def pre_callback_filter(bytes)
# Data is in micromete... |
github | denko-rb/denko | https://github.com/denko-rb/denko | lib/denko/sensor/htu31d.rb | Ruby | mit | 48 | master | 5,648 | module Denko
module Sensor
class HTU31D
include I2C::Peripheral
include Behaviors::Poller
include Behaviors::Lifecycle
include TemperatureHelper
include HumidityHelper
I2C_ADDRESS = 0x40
# Commands
RESET = 0x1E
RESET_TIME = 0.015 # Reset time ... |
github | denko-rb/denko | https://github.com/denko-rb/denko | lib/denko/sensor/qmp6988.rb | Ruby | mit | 48 | master | 9,796 | module Denko
module Sensor
class QMP6988
include Behaviors::Lifecycle
include I2C::Peripheral
include Behaviors::Poller
include TemperatureHelper
include PressureHelper
I2C_ADDRESS = 0x70
UPDATE_TIME = 0.020
RESET_REGISTER = 0xE0
RESET_COMMA... |
github | denko-rb/denko | https://github.com/denko-rb/denko | lib/denko/sensor/sht3x.rb | Ruby | mit | 48 | master | 3,189 | module Denko
module Sensor
class SHT3X
include I2C::Peripheral
include Behaviors::Poller
include Behaviors::Lifecycle
include TemperatureHelper
include HumidityHelper
I2C_ADDRESS = 0x44
RESET = 0x30A2
RESET_TIME = 0.002
HEATER_OF... |
github | denko-rb/denko | https://github.com/denko-rb/denko | lib/denko/sensor/sht4x.rb | Ruby | mit | 48 | master | 3,113 | module Denko
module Sensor
class SHT4X
include I2C::Peripheral
include Behaviors::Poller
include Behaviors::Lifecycle
include TemperatureHelper
include HumidityHelper
I2C_ADDRESS = 0x44
RESET = 0x94
RESET_TIME = 0.001
READ_SERIAL_NUMBER = 0x89
... |
github | denko-rb/denko | https://github.com/denko-rb/denko | lib/denko/sensor/temt6000.rb | Ruby | mit | 48 | master | 734 | module Denko
module Sensor
class TEMT6000 < AnalogIO::Input
# 2 lux per micro amp, from datasheet: Typical Characteristics
LUX_PER_AMP = 2_000_000
attr_accessor :loading_resistance
after_initialize do
raise ArgumentError, "missing :vcc in params for ALSPT19" unless params[:vcc]
... |
github | denko-rb/denko | https://github.com/denko-rb/denko | lib/denko/sensor/hcsr04.rb | Ruby | mit | 48 | master | 840 | module Denko
module Sensor
class HCSR04
include Behaviors::Lifecycle
include Behaviors::MultiPin
include Behaviors::Poller
# Speed of sound in meters per second.
SPEED_OF_SOUND = 343.0
def initialize_pins
proxy_pin :trigger, DigitalIO::Output
proxy_pin :echo, ... |
github | denko-rb/denko | https://github.com/denko-rb/denko | lib/denko/sensor/bmp180.rb | Ruby | mit | 48 | master | 6,220 | module Denko
module Sensor
class BMP180
include I2C::Peripheral
include Behaviors::Poller
include Behaviors::Lifecycle
include TemperatureHelper
include PressureHelper
I2C_ADDRESS = 0x77
# Write this to register 0xE0 for soft reset
SOFT_RESET = 0xB6
#
... |
github | denko-rb/denko | https://github.com/denko-rb/denko | lib/denko/sensor/dht.rb | Ruby | mit | 48 | master | 1,617 | module Denko
module Sensor
class DHT
include Behaviors::InputPin
include Behaviors::Poller
include Behaviors::Lifecycle
include TemperatureHelper
include HumidityHelper
after_initialize do
@state = { temperature: nil, humidity: nil }
@reading = { temperatur... |
github | denko-rb/denko | https://github.com/denko-rb/denko | lib/denko/sensor/ds18b20.rb | Ruby | mit | 48 | master | 1,847 | module Denko
module Sensor
class DS18B20 < OneWire::Peripheral
include Behaviors::Lifecycle
include TemperatureHelper
FAMILY_CODE = 0x28
after_initialize do
@state = { temperature: nil }
@reading = { temperature: nil }
end
def _read
convert
... |
github | denko-rb/denko | https://github.com/denko-rb/denko | lib/denko/sensor/aht.rb | Ruby | mit | 48 | master | 4,214 | module Denko
module Sensor
class AHT1X
include I2C::Peripheral
include Behaviors::Poller
include Behaviors::Lifecycle
include TemperatureHelper
include HumidityHelper
I2C_ADDRESS = 0x38
# Commands
INIT_AND_CALIBRATE = [0xE1, 0x08, 0x00]
READ_STATUS_REGISTE... |
github | denko-rb/denko | https://github.com/denko-rb/denko | lib/denko/motor/a3967.rb | Ruby | mit | 48 | master | 1,482 | module Denko
module Motor
class A3967
include Behaviors::MultiPin
include Behaviors::Lifecycle
def initialize_pins
proxy_pin :step, DigitalIO::Output
proxy_pin :direction, DigitalIO::Output
proxy_pin :ms1, DigitalIO::Output, optional: true
proxy_pin :ms2... |
github | denko-rb/denko | https://github.com/denko-rb/denko | lib/denko/motor/servo.rb | Ruby | mit | 48 | master | 1,654 | module Denko
module Motor
class Servo
FREQUENCY = 50
PERIOD_NS = 20_000_000
include Behaviors::SinglePin
include Behaviors::Lifecycle
before_initialize do
params[:mode] = :output_pwm
end
after_initialize do
attach
end
def min
@min |... |
github | denko-rb/denko | https://github.com/denko-rb/denko | lib/denko/motor/l298.rb | Ruby | mit | 48 | master | 943 | module Denko
module Motor
class L298
include Behaviors::MultiPin
include Behaviors::Lifecycle
attr_reader :speed
def initialize_pins
proxy_pin :direction1, DigitalIO::Output
proxy_pin :direction2, DigitalIO::Output
proxy_pin :enable, PulseIO::PWMOutput
... |
github | denko-rb/denko | https://github.com/denko-rb/denko | lib/denko/spi/bus_common.rb | Ruby | mit | 48 | master | 949 | module Denko
module SPI
module BusCommon
include Behaviors::BusController
include Behaviors::Reader
# Add peripheral to self and the board. It gets callbacks directly from the board.
def add_component(component)
# Don't check for select pin uniqueness. Board handles that.
... |
github | denko-rb/denko | https://github.com/denko-rb/denko | lib/denko/spi/output_register.rb | Ruby | mit | 48 | master | 339 | module Denko
module SPI
class OutputRegister
include Register
include Behaviors::OutputRegister
include Behaviors::Lifecycle
after_initialize do
write
end
# Called by Beaviors::OutputRegister to write @state.
def _write(bytes)
spi_write(@state)
end... |
github | denko-rb/denko | https://github.com/denko-rb/denko | lib/denko/spi/register.rb | Ruby | mit | 48 | master | 313 | module Denko
module SPI
module Register
include SPI::Peripheral
include Behaviors::Register
include Behaviors::Lifecycle
def platform
:spi_register
end
# Select pin is active-low. Disable.
after_initialize do
self.high
end
end
end
end |
github | denko-rb/denko | https://github.com/denko-rb/denko | lib/denko/spi/bit_bang.rb | Ruby | mit | 48 | master | 1,674 | module Denko
module SPI
class BitBang
include Behaviors::MultiPin
include Behaviors::Lifecycle
include BusCommon
before_initialize do
param_pins = params[:pins]
# Allow pin aliases.
param_pins[:input] = param_pins[:input] || param_pins[:poci] || param_pins[:miso]... |
github | denko-rb/denko | https://github.com/denko-rb/denko | lib/denko/spi/input_register.rb | Ruby | mit | 48 | master | 344 | module Denko
module SPI
class InputRegister
include Register
include Behaviors::InputRegister
include Behaviors::Poller
include Behaviors::Listener
def _read
spi_read(bytes)
end
def listen
spi_listen(bytes)
end
def stop
spi_stop
... |
github | denko-rb/denko | https://github.com/denko-rb/denko | lib/denko/spi/bus.rb | Ruby | mit | 48 | master | 731 | module Denko
module SPI
class Bus
include Behaviors::MultiPin
include BusCommon
# Board expects all components to have #pin.
attr_reader :pin
def spi_index
@spi_index ||= params[:spi_index] || params[:index] || 0
end
# Prepend spi_index to these and forward to ... |
github | denko-rb/denko | https://github.com/denko-rb/denko | lib/denko/spi/peripheral.rb | Ruby | mit | 48 | master | 3,196 | module Denko
module SPI
class ChipSelect
include Behaviors::SinglePin
include Behaviors::Callbacks
include Behaviors::Lifecycle
before_initialize do
# It actually functions as an output.
params[:mode] = :output
# But we can't claim it on Linux (the SPI hardware ha... |
github | denko-rb/denko | https://github.com/denko-rb/denko | lib/denko/digital_io/rotary_encoder.rb | Ruby | mit | 48 | master | 2,919 | module Denko
module DigitalIO
class RotaryEncoder
include Behaviors::MultiPin
include Behaviors::Callbacks
include Behaviors::Lifecycle
before_initialize do
# Allow pins to be given as printed on common parts.
unless params[:pins][:a]
params[:pins][:a] = params[:... |
github | denko-rb/denko | https://github.com/denko-rb/denko | lib/denko/digital_io/c_bit_bang.rb | Ruby | mit | 48 | master | 361 | module Denko
module DigitalIO
class CBitBang
include Behaviors::SinglePin
include Behaviors::Callbacks
include Behaviors::Lifecycle
# This is purely to force initialize validation for any pins
# being used by C libraries that bit-bang.
before_initialize do
params[:mode... |
github | denko-rb/denko | https://github.com/denko-rb/denko | lib/denko/digital_io/input.rb | Ruby | mit | 48 | master | 1,365 | module Denko
module DigitalIO
class Input
include Behaviors::InputPin
include Behaviors::Reader
include Behaviors::Poller
include Behaviors::Listener
include Behaviors::Lifecycle
after_initialize do
listen(params[:divider] || 4)
end
def _read
board... |
github | denko-rb/denko | https://github.com/denko-rb/denko | lib/denko/digital_io/pcf8574.rb | Ruby | mit | 48 | master | 1,111 | module Denko
module DigitalIO
class PCF8574
include I2C::Peripheral
include Behaviors::OutputRegister
include Behaviors::Lifecycle
# Default I2C address. Override with i2c_address: key in initialize hash.
I2C_ADDRESS = 0x3F
I2C_FREQUENCY = 400_000
# 8 bit I/O expander... |
github | denko-rb/denko | https://github.com/denko-rb/denko | lib/denko/digital_io/output.rb | Ruby | mit | 48 | master | 941 | module Denko
module DigitalIO
class Output
include Behaviors::OutputPin
include Behaviors::Callbacks
include Behaviors::Threaded
include Behaviors::Lifecycle
interrupt_with :digital_write
after_initialize do
board.digital_read(pin) unless @board.platform == :linux_mil... |
github | denko-rb/denko | https://github.com/denko-rb/denko | lib/denko/behaviors/board_proxy.rb | Ruby | mit | 48 | master | 2,948 | module Denko
module Behaviors
#
# Provides a **bare-minimum** interface matching {Board}, so peripheral instances may
# transparently use a `BoardProxy` (eg. a shift register) in place of a `Board`.
#
# @example LED through a shift register
# # Board, SPI bus, and output shift register.
... |
github | denko-rb/denko | https://github.com/denko-rb/denko | lib/denko/behaviors/poller.rb | Ruby | mit | 48 | master | 722 | module Denko
module Behaviors
module Poller
include Reader
include Threaded
def poll_using(reader_method, interval, &block)
mruby_thread_check
unless [Integer, Float].include? interval.class
raise ArgumentError, "wrong interval given to #poll : #{interval.inspect}"
... |
github | denko-rb/denko | https://github.com/denko-rb/denko | lib/denko/behaviors/subcomponents.rb | Ruby | mit | 48 | master | 5,299 | module Denko
module Behaviors
#
# Keeps track of multiple downstream components connected to the including
# component's physical interfaces. This mixin is typically included in a {Board},
# {BoardProxy} or {BusController}.
#
# Subcomponents are indexed by the GPIO pin numbers they connect to,... |
github | denko-rb/denko | https://github.com/denko-rb/denko | lib/denko/behaviors/threaded.rb | Ruby | mit | 48 | master | 1,768 | module Denko
module Behaviors
module Threaded
attr_reader :thread, :interrupts_enabled
module ClassMethods
def interrupt_with(*args)
interrupts = class_variable_defined?(:@@interrupts) ? class_variable_get(:@@interrupts) : []
interrupts = [] unless interrupts
int... |
github | denko-rb/denko | https://github.com/denko-rb/denko | lib/denko/behaviors/lifecycle.rb | Ruby | mit | 48 | master | 3,646 | module Denko
module Behaviors
#
# Provides callback hooks for hierarchical initialization in {Component} subclasses.
#
# @see Denko::Behaviors::Component#initialize
#
# @example Using Lifecycle callbacks
# class BaseSensor
# include Denko::Behaviors::Component
# include D... |
github | denko-rb/denko | https://github.com/denko-rb/denko | lib/denko/behaviors/state.rb | Ruby | mit | 48 | master | 1,971 | module Denko
module Behaviors
#
# Provides a standardized state management interface for {Component}.
# It typically stores things like sensor readings or the present level
# (high or low) of a digital output.
#
# @example Digital Output
# class Output
# include Behaviors::Output... |
github | denko-rb/denko | https://github.com/denko-rb/denko | lib/denko/behaviors/bus_peripheral_addressed.rb | Ruby | mit | 48 | master | 395 | module Denko
module Behaviors
module BusPeripheralAddressed
include BusPeripheral
include Lifecycle
def address
@address ||= params[:address]
end
# Validate address presence after initialization.
after_initialize do
raise ArgumentError, "no address set for for... |
github | denko-rb/denko | https://github.com/denko-rb/denko | lib/denko/behaviors/bus_controller_addressed.rb | Ruby | mit | 48 | master | 384 | module Denko
module Behaviors
module BusControllerAddressed
include BusController
def add_component(component)
addresses = components.map { |c| c.address }
if addresses.include? component.address
raise ArgumentError, "duplicate peripheral address for #{component}"
en... |
github | denko-rb/denko | https://github.com/denko-rb/denko | lib/denko/behaviors/reader.rb | Ruby | mit | 48 | master | 2,453 | module Denko
module Behaviors
module Reader
include Lifecycle
include Callbacks
READ_WAIT_TIME = 0.001
attr_reader :state_mutex
after_initialize do
@read_type = :idle
@read_result = nil
@state_mutex = Denko.mruby? ? Denko::MutexStub.new : Mutex.new
... |
github | denko-rb/denko | https://github.com/denko-rb/denko | lib/denko/behaviors/input_register.rb | Ruby | mit | 48 | master | 1,561 | module Denko
module Behaviors
module InputRegister
include Register
include Lifecycle
after_initialize do
@reading_pins = Array.new(bytes*8) { false }
@listening_pins = Array.new(bytes*8) { false }
self.add_callback(:board_proxy) do |byte_array|
byte_array.e... |
github | denko-rb/denko | https://github.com/denko-rb/denko | lib/denko/behaviors/bus_peripheral.rb | Ruby | mit | 48 | master | 347 | module Denko
module Behaviors
module BusPeripheral
include Component
include Lifecycle
alias :bus :board
before_initialize do
params[:board] = params[:bus] if params[:bus]
end
def atomically(&block)
bus.mutex.synchronize do
block.call
end
... |
github | denko-rb/denko | https://github.com/denko-rb/denko | lib/denko/behaviors/register.rb | Ruby | mit | 48 | master | 493 | module Denko
module Behaviors
module Register
include BoardProxy
include Lifecycle
BYTE_COUNT = 1
def bytes
@bytes = params[:bytes] || self.class::BYTE_COUNT
end
after_initialize do
old_state
state
end
def old_state
@old_state ||=... |
github | denko-rb/denko | https://github.com/denko-rb/denko | lib/denko/behaviors/multi_pin.rb | Ruby | mit | 48 | master | 2,510 | module Denko
module Behaviors
module MultiPin
#
# Model complex components, using multiple pins, by using proxy components
# with one pin each.
#
include Component
include Lifecycle
attr_reader :pin, :pins, :proxies
def proxies
@proxies ||= {}
end
... |
github | denko-rb/denko | https://github.com/denko-rb/denko | lib/denko/behaviors/output_register.rb | Ruby | mit | 48 | master | 794 | module Denko
module Behaviors
module OutputRegister
include Register
include Lifecycle
def write
if @state != @old_state
# Including classes must implement #_write.
_write(@state)
@state.each_with_index { |byte, i| @old_state[i] = byte }
end
... |
github | denko-rb/denko | https://github.com/denko-rb/denko | lib/denko/behaviors/output_pin.rb | Ruby | mit | 48 | master | 481 | module Denko
module Behaviors
module OutputPin
include Component
include SinglePin
include Lifecycle
OUTPUT_MODES = [:output, :output_pwm, :output_dac, :output_open_drain, :output_open_source]
before_initialize do
params[:mode] ||= :output
unless OUTPUT_MODES.includ... |
github | denko-rb/denko | https://github.com/denko-rb/denko | lib/denko/behaviors/component.rb | Ruby | mit | 48 | master | 974 | module Denko
module Behaviors
module Component
attr_reader :board, :params
include State
def initialize(options={})
@params = options
run_before_initialize_cbs
initialize_board
convert_pins
initialize_pins
register
run_after_initialize_c... |
github | denko-rb/denko | https://github.com/denko-rb/denko | lib/denko/behaviors/input_pin.rb | Ruby | mit | 48 | master | 607 | module Denko
module Behaviors
module InputPin
include Component
include SinglePin
include Lifecycle
INPUT_MODES = [:input, :input_pulldown, :input_pullup, :input_adc]
before_initialize do
params[:mode] ||= :input
unless INPUT_MODES.include?(params[:mode])
... |
github | denko-rb/denko | https://github.com/denko-rb/denko | lib/denko/behaviors/callbacks.rb | Ruby | mit | 48 | master | 1,112 | module Denko
module Behaviors
module Callbacks
include Lifecycle
include State
attr_reader :callbacks
after_initialize do
@callbacks = {}
end
def add_callback(key=:persistent, &block)
@callbacks[key] ||= []
@callbacks[key] << block
end
de... |
github | denko-rb/denko | https://github.com/denko-rb/denko | lib/denko/behaviors/listener.rb | Ruby | mit | 48 | master | 602 | module Denko
module Behaviors
module Listener
include Callbacks
attr_reader :divider
#
# These delegate to #_listen and #_stop_listener,
# which should be defined in the including class.
#
def listen(divider=nil, &block)
@divider = divider
stop
a... |
github | denko-rb/denko | https://github.com/denko-rb/denko | lib/denko/behaviors/single_pin.rb | Ruby | mit | 48 | master | 510 | module Denko
module Behaviors
module SinglePin
include Component
attr_reader :pin, :mode
def mode=(mode)
board.set_pin_mode(pin, mode)
@mode = mode
end
def convert_pins
raise ArgumentError, 'a pin is required for this component' unless params[:pin]
p... |
github | denko-rb/denko | https://github.com/denko-rb/denko | lib/denko/behaviors/bus_controller.rb | Ruby | mit | 48 | master | 287 | module Denko
module Behaviors
module BusController
include Component
include Subcomponents
def mutex
# mruby doesn't have Thread or Mutex, so only stub there.
@mutex ||= Denko.mruby? ? Denko::MutexStub.new : Mutex.new
end
end
end
end |
github | denko-rb/denko | https://github.com/denko-rb/denko | tutorial/03-potentiometer/potentiometer.rb | Ruby | mit | 48 | master | 2,230 | #
# Example 3: Potentiometer and Analog Inputs
#
require 'bundler/setup'
require 'denko'
# Set up the board, connecting with serial over USB.
board = Denko::Board.new(Denko::Connection::Serial.new)
#
# Connect the potentiometer's outer 2 pins to Ground and Vcc respectively.
# The center is the wiper, and should be co... |
github | denko-rb/denko | https://github.com/denko-rb/denko | tutorial/04-pwm_led/pwm_led.rb | Ruby | mit | 48 | master | 2,080 | #
# Example 4: LED Brightness and PWM
#
require 'bundler/setup'
require 'denko'
# Set up the board, connecting with serial over USB
board = Denko::Board.new(Denko::Connection::Serial.new)
#
# LEDs don't change brightness much with voltage, but we can vary it with a
# technique called PWM. PWM sets up a fast (500Hz+) ... |
github | denko-rb/denko | https://github.com/denko-rb/denko | tutorial/01-led/led.rb | Ruby | mit | 48 | master | 2,178 | #
# Example 1: Controlling an LED
#
require 'bundler/setup'
require 'denko'
# If the board is plugged into a USB port, we can connect with serial over USB.
connection = Denko::Connection::Serial.new
# Create a new board object, giving the connection.
board = Denko::Board.new(connection)
#
# Create an LED object, giv... |
github | denko-rb/denko | https://github.com/denko-rb/denko | tutorial/05-rgb_led/rgb_mapping.rb | Ruby | mit | 48 | master | 1,795 | # Maximum and minimum PWM values.
OUT_MAX = 100
OUT_MIN = 0
def map_value(value, input_steps, reverse=false)
# Map it to a 0-1 range first.
fraction = value / input_steps.to_f
# Reverse if needed.
fraction = 1 - fraction if reverse
# Map to the output range
value = (fraction * OUT_MAX).floor
# Clamp w... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.