172 lines
4.8 KiB
C++
172 lines
4.8 KiB
C++
/*--------------------------------------------------------------*/
|
|
/* Projet: OFFOK */
|
|
/* File : offok.ino */
|
|
/* Author: JeARZ 02/14/21 */
|
|
/*--------------------------------------------------------------*/
|
|
/*
|
|
This gets IR codes send by a TvBeGone & displays results on OLED
|
|
02/14/21: First proof of IR sensor & Oled
|
|
02/17/21: Moved to new lib U8g2lib, works OK on UNO
|
|
04/05/21: OK on NANO
|
|
04/22/21: new logo, display hex code, test piezo OK
|
|
05/19/21: added offok_tunes.h for rtttl libraries & ringtones
|
|
logo.h renamed to offok_logo.h
|
|
TODO: adjust parameters, use protothreads ?
|
|
*/
|
|
#include <Arduino.h>
|
|
/*
|
|
Graphics settings
|
|
*/
|
|
#include <U8g2lib.h>
|
|
#ifdef U8X8_HAVE_HW_SPI
|
|
#include <SPI.h>
|
|
#endif
|
|
#ifdef U8X8_HAVE_HW_I2C
|
|
#include <Wire.h>
|
|
#endif
|
|
U8G2_SH1106_128X64_NONAME_1_HW_I2C u8g2\
|
|
(U8G2_R0, /* RST=*/ U8X8_PIN_NONE);
|
|
#include "offok_logo.h" // Home logo :-)
|
|
/*
|
|
IR settings
|
|
*/
|
|
#include <IRremote.h>
|
|
/* the function tone() uses timer2 by default, so does IRremote lib.
|
|
* File IRremoteBoardDefs.h, in lib IRremote define wich timer to use */
|
|
// #define IR_USE_TIMER1 // use Timer 1 instead of Timer 2
|
|
const int RECV_PIN = 2;
|
|
IRrecv irrecv(RECV_PIN);
|
|
decode_results results;
|
|
/*
|
|
* Sound settings
|
|
*/
|
|
#define PIEZO 7 // Loudspeaker pin
|
|
#include "offok_tunes.h" // predefined ringtones juke-box :-)
|
|
|
|
//#define DEBUG
|
|
#define SOUND_ON // comment if you can't stand
|
|
#define NBCODES 120 // min number of codes to qualify a kit
|
|
#define ACQUINONE 4000 // no IR signal timeout in millisecond
|
|
|
|
// ==================== Functions ====================
|
|
void showTrace(); // defined after main ifdef DEBUG
|
|
void showCount(int val) ; // defined after main not used anymore
|
|
|
|
// play a rtttl ringtone definef in offok_tunes.h
|
|
void play_r3tl (const char * rtune){
|
|
#ifdef SOUND_ON
|
|
anyrtttl::blocking::play(PIEZO, rtune);
|
|
#endif
|
|
}
|
|
void drawLogo(void) { // draw the logo screen
|
|
u8g2.firstPage();
|
|
do {
|
|
u8g2.drawXBMP( 0, 0, logo_width, logo_height, logo_bits);
|
|
} while ( u8g2.nextPage() );
|
|
}
|
|
// Oled brightness dimmer, slope up or down
|
|
void constratSlope(int depart, int aim, int inc, int lag) {
|
|
int i=depart;
|
|
// at first, if increasing then
|
|
if (inc > 0) u8g2.setPowerSave(0); // disable oled
|
|
do {
|
|
i=constrain(i,0,254);
|
|
u8g2.setContrast(i); // 0 (no contrast) to 255
|
|
delay (lag);
|
|
i=i+inc;
|
|
} while (i != aim);
|
|
// at end, if decreasing then
|
|
if (inc < 0) u8g2.setPowerSave(0); // disable oled
|
|
}
|
|
// & at the end ... the winner is
|
|
void showResult(int val){
|
|
char l1[8];
|
|
sprintf(l1, "%4d",val);
|
|
u8g2.firstPage();
|
|
do {
|
|
u8g2.setFont(u8g2_font_profont17_tr);
|
|
u8g2.drawStr(10,24, "Total recus :");
|
|
u8g2.setFont(u8g2_font_profont22_tr);
|
|
u8g2.drawStr(30,54, l1);
|
|
} while ( u8g2.nextPage() );
|
|
if (val > NBCODES)
|
|
play_r3tl(_rCountOK_);
|
|
else
|
|
play_r3tl(_rCountKO_);
|
|
noTone(PIEZO);
|
|
}
|
|
// display incoming IR frame code
|
|
void showCode(unsigned long val) {
|
|
char buf[16];
|
|
play_r3tl(_rGetIR_);
|
|
u8g2.firstPage();
|
|
do {
|
|
u8g2.setFont(u8g2_font_profont17_tr);
|
|
u8g2.drawStr(16,24, "Code recu :");
|
|
sprintf(buf, "%08X", val ) ;
|
|
u8g2.setFont(u8g2_font_profont22_tr);
|
|
u8g2.drawStr(16,54,buf);
|
|
} while ( u8g2.nextPage() );
|
|
}
|
|
|
|
void setup(void) {
|
|
noTone(PIEZO);
|
|
u8g2.begin();
|
|
drawLogo();
|
|
#ifdef DEBUG
|
|
Serial.begin(9600);
|
|
#endif
|
|
play_r3tl(_rIntro_);
|
|
irrecv.enableIRIn(); // Start the receiver
|
|
delay(1000);
|
|
}
|
|
// ==================== Main ====================
|
|
// Global parameters
|
|
int cl=0; unsigned long lastReading=0, currentt=0;
|
|
|
|
void loop(void) {
|
|
noTone(PIEZO);
|
|
if (irrecv.decode(&results)) { // if we keep getting signals, it is running
|
|
lastReading=millis();
|
|
cl += 1;
|
|
showCode(results.value); //showCount(cl);
|
|
irrecv.resume();
|
|
}
|
|
else // got no more reading
|
|
{ // if we keep getting nothing, then it is over
|
|
currentt = millis();
|
|
if ((lastReading + ACQUINONE < currentt) && ( cl > 0)) { // lastReading too old
|
|
showTrace();
|
|
showResult(cl); cl = 0;
|
|
constratSlope(255, 0, -1, 50);
|
|
delay(3000);
|
|
drawLogo();
|
|
constratSlope(0, 255, 1, 20);
|
|
}
|
|
}
|
|
} // loop
|
|
|
|
// debugging
|
|
void showTrace() {
|
|
#ifdef DEBUG
|
|
Serial.print("OK cl : ");
|
|
Serial.print(cl);
|
|
Serial.print(" currentt : ");
|
|
Serial.print(currentt);
|
|
Serial.print(" lastReading : ");
|
|
Serial.println(lastReading);
|
|
#endif
|
|
}
|
|
// show IR frames count, replaced by showResult
|
|
void showCount(int val) {
|
|
char buf[16];
|
|
u8g2.firstPage();
|
|
do {
|
|
u8g2.setFont(u8g2_font_fub14_tr);
|
|
u8g2.drawStr(12,28,"Codes lus :");
|
|
sprintf(buf, "%3d", val ) ;
|
|
u8g2.setFont(u8g2_font_fub20_tr);
|
|
u8g2.drawStr(40,60,buf);
|
|
} while ( u8g2.nextPage() );
|
|
}
|