Skip to content

Commit

Permalink
Infrared: Use regular lib + fix memory alignment on ESP8266. Fix leng…
Browse files Browse the repository at this point in the history
…th validation
  • Loading branch information
vickash committed Sep 21, 2024
1 parent df18270 commit f70acb8
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 50 deletions.
6 changes: 3 additions & 3 deletions lib/denko/board/infrared.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ module Denko
class Board
def infrared_emit(pin, frequency, pulses)
#
# Limit to 255 marks/spaces (not pairs) for now.
# Limit to 255 marks/spaces (not pairs).
#
# Length must be 1-byte long, not literally 1
# Make length uint16 as well, for aligned memory access on ESP8266.
# Pulses max is 2x255 bytes long since each is 2 bytes.
length = pack :uint8, pulses.length, max: 1
length = pack :uint16, pulses.length, max: 2
bytes = pack :uint16, pulses, min: 1, max: 510

write Message.encode command: 16,
Expand Down
4 changes: 2 additions & 2 deletions lib/denko/pulse_io/ir_transmitter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ def initialize_pins(options={})
end

def emit(pulses=[], frequency: 38)
if pulses.length > 256 || pulses.length < 1
raise ArgumentError, 'wrong number of IR pulses (expected 1 to 256)'
if pulses.length > 255 || pulses.length < 1
raise ArgumentError, 'wrong number of IR pulses (expected 1 to 255)'
end

pulses.each_with_index do |pulse, index|
Expand Down
9 changes: 0 additions & 9 deletions lib/denko_cli/packages.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,19 +90,10 @@ class DenkoCLI::Generator
ir_out: {
description: "Transmit infrared signals",
directive: "DENKO_IR_OUT",
exclude: [:esp8266],
files: [
"DenkoIROut.cpp",
]
},
ir_out_esp: {
description: "Transmit infrared signals with the ESP8266 and ESP32",
directive: "DENKO_IR_OUT",
only: [:esp8266],
files: [
"DenkoIROutESP.cpp",
]
},
led_array: {
description: "Support for various protocols that control (RGB) LED arrays.",
directive: "DENKO_LED_ARRAY",
Expand Down
2 changes: 1 addition & 1 deletion lib/denko_cli/targets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class DenkoCLI::Generator
ra4m1: STANDARD_PACKAGES - [:ir_out, :led_array],

# ESP8266 uses an IR library specific to it.
esp8266: STANDARD_PACKAGES - [:ir_out] + [:ir_out_esp],
esp8266: STANDARD_PACKAGES,
esp32: STANDARD_PACKAGES,

# RP2040 can't use WS2812 yet.
Expand Down
19 changes: 12 additions & 7 deletions src/lib/DenkoIROut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// This file adds to the Denko class only if DENKO_IR_OUT is defined in Denko.h.
//
#include "Denko.h"
#if defined(DENKO_IR_OUT) && !defined(ESP8266)
#if defined(DENKO_IR_OUT)

// Save memory by disabling receiver.
#undef RAW_BUFFER_LENGTH
Expand All @@ -20,14 +20,19 @@
// CMD = 16
// Send an infrared signal.
void Denko::irSend(){
// Byte 1+ of auxMsg is already little-endian uint16 pulses.
uint16_t *pulseArray = reinterpret_cast<uint16_t *>(auxMsg + 1);

// Dynamically set the sending pin. Needs to be PWM capable.
// Change send pin per call. Must be PWM capable.
IrSender.setSendPin(pin);

// auxMsg[0] contains number of uint16_t
// Byte 2+ of auxMsg is already little-endian uint16 pulses.
//
// WARNING: This offset must always be an even number, for aligned
// memory access on the ESP8266, or it breaks.
uint16_t *pulseArray = reinterpret_cast<uint16_t *>(auxMsg + 2);

// auxMsg[0..1] contains number of pulses, also uint16.
uint16_t length = *reinterpret_cast<uint16_t *>(auxMsg);

// Val contains frequency
IrSender.sendRaw(pulseArray, auxMsg[0], val);
IrSender.sendRaw(pulseArray, length, val);
}
#endif
26 changes: 0 additions & 26 deletions src/lib/DenkoIROutESP.cpp

This file was deleted.

2 changes: 1 addition & 1 deletion test/board/infrared_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def board

def test_infrared_emit
board
aux = pack(:uint8, 4) + pack(:uint16, [255,0,255,0])
aux = pack(:uint16, 4) + pack(:uint16, [255,0,255,0])
message = Denko::Message.encode command: 16, pin: 8, value: 38, aux_message: aux

mock = Minitest::Mock.new.expect :call, nil, [message]
Expand Down
2 changes: 1 addition & 1 deletion test/pulse_io/ir_transmitter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def part

def test_pulse_count_validation
assert_raises(ArgumentError) do
part.emit Array.new(257) { 0 }
part.emit Array.new(256) { 0 }
end
end

Expand Down

0 comments on commit f70acb8

Please sign in to comment.