Ajouter 'offok/offok.ino'

First valid issue of main program
This commit is contained in:
JeArz 2021-04-22 05:47:38 +02:00
parent a9b54c5e05
commit d7844b7014
1 changed files with 173 additions and 0 deletions

173
offok/offok.ino Normal file
View File

@ -0,0 +1,173 @@
/*--------------------------------------------------------------*/
/* Projet: OFFOK */
/* File : offok.ino */
/* Author: ukjent@HPP 04/05/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
TODO: adjust delays, play nice ringstones, 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, /* reset=*/ U8X8_PIN_NONE);
#include "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;
//#define DEBUG
#define NBCODES 130 // mininum codes to qualify a kit
#define ACQUINONE 4000 // maximum time allowed without signal in millisecond
/*
* Sound settings
*/
#define PIEZO 7
unsigned int sounds[] = {262,294,330,349,392,440,494}; // just to test piezo
#define BEATTIME 400
void drawLogo(void) { // draw the logo screen
u8g2.firstPage();
do {
u8g2.drawXBMP( 0, 0, logo_width, logo_height, logo_bits);
} while ( u8g2.nextPage() );
}
void setup(void) {
noTone(PIEZO); // WIP musical intro needed :-)
u8g2.begin();
drawLogo();
#ifdef DEBUG
Serial.begin(9600);
#endif
irrecv.enableIRIn(); // Start the receiver
delay(1000);
}
void showTrace();
void constratSlope(int depart, int aim, int inc, int lag) { // Dimmer slope up or down
int i=depart;
if (inc > 0) u8g2.setPowerSave(0);
do {
i=constrain(i,0,254);
u8g2.setContrast(i);
delay (lag);
i=i+inc;
} while (i != aim);
if (inc < 0) u8g2.setPowerSave(1);
}
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() );
for (int i=0; i<=6; i++) {
tone(PIEZO,sounds[i]);
delay(BEATTIME);
noTone(PIEZO);
}
}
void showResultOLD(int val){
char l1[16],l2[16];
if (val < NBCODES) {
sprintf(l1, "C'est KO :-(") ;
sprintf(l2, " DESOLE");
} else {
sprintf(l1, "C'est OK :-)") ;
sprintf(l2, "FELICITATION");
}
u8g2.firstPage();
do {
u8g2.setFont(u8g2_font_fub14_tr);
u8g2.drawStr(4,24, l1);
u8g2.setFont(u8g2_font_fub11_tr);
u8g2.drawStr(8,54, l2);
} while ( u8g2.nextPage() );
}
void showCode(unsigned long val) {
char buf[16];
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 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() );
}
// 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
void showTrace() {
#ifdef DEBUG
Serial.print("OK cl : ");
Serial.print(cl);
Serial.print(" currentt : ");
Serial.print(currentt);
Serial.print(" lastReading : ");
Serial.println(lastReading);
#endif
}