Created
June 16, 2025 15:11
-
-
Save greed9/87c6760b20a2539f7be820df4e3f2cec to your computer and use it in GitHub Desktop.
Send characters via IR Remote protocol
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* TinySender.cpp | |
* | |
* Example for sending using TinyIR. By default sends simultaneously using all supported protocols | |
* To use a single protocol, simply delete or comment out all unneeded protocols in the main loop | |
* Program size is significantly reduced when using a single protocol | |
* For example, sending only 8 bit address and command NEC codes saves 780 bytes program memory and 26 bytes RAM compared to SimpleSender, | |
* which does the same, but uses the IRRemote library (and is therefore much more flexible). | |
* | |
* | |
* The FAST protocol is a proprietary modified JVC protocol without address, with parity and with a shorter header. | |
* FAST Protocol characteristics: | |
* - Bit timing is like NEC or JVC | |
* - The header is shorter, 3156 vs. 12500 | |
* - No address and 16 bit data, interpreted as 8 bit command and 8 bit inverted command, | |
* leading to a fixed protocol length of (6 + (16 * 3) + 1) * 526 = 55 * 526 = 28930 microseconds or 29 ms. | |
* - Repeats are sent as complete frames but in a 50 ms period / with a 21 ms distance. | |
* | |
* | |
* This file is part of IRMP https://github.com/IRMP-org/IRMP. | |
* This file is part of Arduino-IRremote https://github.com/Arduino-IRremote/Arduino-IRremote. | |
* | |
************************************************************************************ | |
* MIT License | |
* | |
* Copyright (c) 2022-2024 Armin Joachimsmeyer | |
* | |
* Permission is hereby granted, free of charge, to any person obtaining a copy | |
* of this software and associated documentation files (the "Software"), to deal | |
* in the Software without restriction, including without limitation the rights | |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
* copies of the Software, and to permit persons to whom the Software is furnished | |
* to do so, subject to the following conditions: | |
* | |
* The above copyright notice and this permission notice shall be included in all | |
* copies or substantial portions of the Software. | |
* | |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, | |
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A | |
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT | |
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF | |
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE | |
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
* | |
************************************************************************************ | |
*/ | |
#include <Arduino.h> | |
#include <SPI.h> | |
#include <Wire.h> | |
#include <Adafruit_GFX.h> | |
#include <Adafruit_SSD1306.h> | |
#define SCREEN_WIDTH 128 // OLED display width, in pixels | |
#define SCREEN_HEIGHT 64 // OLED display height, in pixels | |
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins) | |
// The pins for I2C are defined by the Wire-library. | |
// On an arduino UNO: A4(SDA), A5(SCL) | |
// On an arduino MEGA 2560: 20(SDA), 21(SCL) | |
// On an arduino LEONARDO: 2(SDA), 3(SCL), ... | |
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin) | |
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32 | |
#define MAX_MESSAGE 25 | |
#include "TinyIRSender.hpp" | |
// This object (display) represents the SD1306 screen | |
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); | |
void setup() { | |
pinMode(LED_BUILTIN, OUTPUT); | |
Serial.begin(115200); | |
// Just to know which program is running on my Arduino | |
Serial.println(F("START " __FILE__ " from " __DATE__ "\r\nUsing library version " VERSION_TINYIR)); | |
Serial.print(F("Send IR signals at pin ")); | |
Serial.println(IR_SEND_PIN); | |
// SD1306 initialization | |
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally | |
if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) { | |
Serial.println(F("SSD1306 allocation failed")); | |
for(;;); // Don't proceed, loop forever | |
} | |
// Show initial display buffer contents on the screen -- | |
// the library initializes this with an Adafruit splash screen. | |
display.setTextSize(2); | |
display.setTextColor(WHITE); | |
display.setCursor(0, 17); | |
display.startscrolldiagright(0x0F, 0x0F); | |
display.display(); | |
delay(2000); // Pause for 2 seconds | |
// Clear the buffer | |
display.clearDisplay(); | |
display.display() ; | |
} | |
/* | |
* Set up the data to be sent. | |
* The compiler is intelligent and removes the code for 16 bit address handling if we call it with an uint8_t address :-). | |
* Using an uint16_t address or data requires additional 28 bytes program memory for NEC and 56 bytes program memory for FAST. | |
*/ | |
uint8_t sAddress = 0x02; | |
//uint16_t sAddress = 0x02; | |
uint8_t sCommand = 0x34; | |
//uint16_t sCommand = 0x34; | |
uint8_t sRepeats = 0; | |
void loop() { | |
// Send with NEC | |
// NEC uses 8 bit address and 8 bit command each with 8 bit inverted parity checks | |
sAddress = 99; | |
sRepeats = 0; | |
// clip repeats at 4 | |
if (sRepeats > 4) { | |
sRepeats = 4; | |
} | |
//display.print( "Sending") ; | |
//display.display( ) ; | |
get_and_send_message( ) ; | |
delay(1000); // delay must be greater than 5 ms (RECORD_GAP_MICROS), otherwise the receiver sees it as one long signal | |
} | |
void get_and_send_message (void ) | |
{ | |
// from: https://forum.arduino.cc/t/read-char-over-serial-solved/153697/9 | |
static char buffer[MAX_MESSAGE]; | |
static unsigned char index = 0; | |
char c; | |
while (Serial.available() > 0) | |
{ | |
c = Serial.read(); | |
//Serial.print(c ); | |
if (c == '\r') | |
{ | |
for( int i = 0 ; i < strlen( buffer ) ; i++) | |
{ | |
display.print( buffer[i] ) ; | |
//display.startscrolldiagright(0, 0x10) ; | |
display.display( ) ; | |
//sCommand = send_str[i] ; | |
//Serial.println((char)sCommand) ; | |
sendNEC(IR_SEND_PIN, sAddress, ( char ) buffer[i], 0, 0); | |
delay( 100) ; | |
} | |
sendNEC(0x00, '\r', sRepeats); | |
buffer[0] = 0; | |
index = 0; | |
} else | |
{ | |
if (index < MAX_MESSAGE-1) | |
{ | |
buffer[index++] = c; | |
buffer[index] = 0; | |
} | |
} | |
} | |
display.display() ; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment