123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514 |
- #define TEST 0
-
- #if TEST
- # define SEND_PRONTO 1
- # define PRONTO_ONCE false
- # define PRONTO_REPEAT true
- # define PRONTO_FALLBACK true
- # define PRONTO_NOFALLBACK false
- #endif
-
- #if SEND_PRONTO
-
- //******************************************************************************
- #if TEST
- # include <stdio.h>
- void enableIROut (int freq) { printf("\nFreq = %d KHz\n", freq); }
- void mark (int t) { printf("+%d," , t); }
- void space (int t) { printf("-%d, ", t); }
- #else
- # include "IRremote.h"
- #endif // TEST
-
- //+=============================================================================
- // Check for a valid hex digit
- //
- bool ishex (char ch)
- {
- return ( ((ch >= '0') && (ch <= '9')) ||
- ((ch >= 'A') && (ch <= 'F')) ||
- ((ch >= 'a') && (ch <= 'f')) ) ? true : false ;
- }
-
- //+=============================================================================
- // Check for a valid "blank" ... '\0' is a valid "blank"
- //
- bool isblank (char ch)
- {
- return ((ch == ' ') || (ch == '\t') || (ch == '\0')) ? true : false ;
- }
-
- //+=============================================================================
- // Bypass spaces
- //
- bool byp (char** pcp)
- {
- while (isblank(**pcp)) (*pcp)++ ;
- }
-
- //+=============================================================================
- // Hex-to-Byte : Decode a hex digit
- // We assume the character has already been validated
- //
- uint8_t htob (char ch)
- {
- if ((ch >= '0') && (ch <= '9')) return ch - '0' ;
- if ((ch >= 'A') && (ch <= 'F')) return ch - 'A' + 10 ;
- if ((ch >= 'a') && (ch <= 'f')) return ch - 'a' + 10 ;
- }
-
- //+=============================================================================
- // Hex-to-Word : Decode a block of 4 hex digits
- // We assume the string has already been validated
- // and the pointer being passed points at the start of a block of 4 hex digits
- //
- uint16_t htow (char* cp)
- {
- return ( (htob(cp[0]) << 12) | (htob(cp[1]) << 8) |
- (htob(cp[2]) << 4) | (htob(cp[3]) ) ) ;
- }
-
- //+=============================================================================
- //
- bool sendPronto (char* s, bool repeat, bool fallback)
- {
- int i;
- int len;
- int skip;
- char* cp;
- uint16_t freq; // Frequency in KHz
- uint8_t usec; // pronto uSec/tick
- uint8_t once;
- uint8_t rpt;
-
- // Validate the string
- for (cp = s; *cp; cp += 4) {
- byp(&cp);
- if ( !ishex(cp[0]) || !ishex(cp[1]) ||
- !ishex(cp[2]) || !ishex(cp[3]) || !isblank(cp[4]) ) return false ;
- }
-
- // We will use cp to traverse the string
- cp = s;
-
- // Check mode = Oscillated/Learned
- byp(&cp);
- if (htow(cp) != 0000) return false;
- cp += 4;
-
- // Extract & set frequency
- byp(&cp);
- freq = (int)(1000000 / (htow(cp) * 0.241246)); // Rounding errors will occur, tolerance is +/- 10%
- usec = (int)(((1.0 / freq) * 1000000) + 0.5); // Another rounding error, thank Cod for analogue electronics
- freq /= 1000; // This will introduce a(nother) rounding error which we do not want in the usec calcualtion
- cp += 4;
-
- // Get length of "once" code
- byp(&cp);
- once = htow(cp);
- cp += 4;
-
- // Get length of "repeat" code
- byp(&cp);
- rpt = htow(cp);
- cp += 4;
-
- // Which code are we sending?
- if (fallback) { // fallback on the "other" code if "this" code is not present
- if (!repeat) { // requested 'once'
- if (once) len = once * 2, skip = 0 ; // if once exists send it
- else len = rpt * 2, skip = 0 ; // else send repeat code
- } else { // requested 'repeat'
- if (rpt) len = rpt * 2, skip = 0 ; // if rpt exists send it
- else len = once * 2, skip = 0 ; // else send once code
- }
- } else { // Send what we asked for, do not fallback if the code is empty!
- if (!repeat) len = once * 2, skip = 0 ; // 'once' starts at 0
- else len = rpt * 2, skip = once ; // 'repeat' starts where 'once' ends
- }
-
- // Skip to start of code
- for (i = 0; i < skip; i++, cp += 4) byp(&cp) ;
-
- // Send code
- enableIROut(freq);
- for (i = 0; i < len; i++) {
- byp(&cp);
- if (i & 1) space(htow(cp) * usec);
- else mark (htow(cp) * usec);
- cp += 4;
- }
- }
-
- //+=============================================================================
- #if TEST
-
- int main ( )
- {
- char prontoTest[] =
- "0000 0070 0000 0032 0080 0040 0010 0010 0010 0030 " // 10
- "0010 0010 0010 0010 0010 0010 0010 0010 0010 0010 " // 20
- "0010 0010 0010 0010 0010 0010 0010 0010 0010 0010 " // 30
- "0010 0010 0010 0030 0010 0010 0010 0010 0010 0010 " // 40
- "0010 0010 0010 0010 0010 0010 0010 0010 0010 0010 " // 50
- "0010 0010 0010 0030 0010 0010 0010 0010 0010 0010 " // 60
- "0010 0010 0010 0010 0010 0010 0010 0010 0010 0010 " // 70
- "0010 0010 0010 0030 0010 0010 0010 0030 0010 0010 " // 80
- "0010 0010 0010 0030 0010 0010 0010 0010 0010 0030 " // 90
- "0010 0010 0010 0030 0010 0010 0010 0010 0010 0030 " // 100
- "0010 0030 0010 0aa6"; // 104
-
- sendPronto(prontoTest, PRONTO_ONCE, PRONTO_FALLBACK); // once code
- sendPronto(prontoTest, PRONTO_REPEAT, PRONTO_FALLBACK); // repeat code
- sendPronto(prontoTest, PRONTO_ONCE, PRONTO_NOFALLBACK); // once code
- sendPronto(prontoTest, PRONTO_REPEAT, PRONTO_NOFALLBACK); // repeat code
-
- return 0;
- }
-
- #endif // TEST
-
- #endif // SEND_PRONTO
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- #if 0
- //******************************************************************************
- // Sources:
- // http://www.remotecentral.com/features/irdisp2.htm
- // http://www.hifi-remote.com/wiki/index.php?title=Working_With_Pronto_Hex
- //******************************************************************************
-
- #include <stdint.h>
- #include <stdio.h>
-
- #define IRPRONTO
- #include "IRremoteInt.h" // The Arduino IRremote library defines USECPERTICK
-
- //------------------------------------------------------------------------------
- // Source: https://www.google.co.uk/search?q=DENON+MASTER+IR+Hex+Command+Sheet
- // -> http://assets.denon.com/documentmaster/us/denon%20master%20ir%20hex.xls
- //
- char prontoTest[] =
- "0000 0070 0000 0032 0080 0040 0010 0010 0010 0030 " // 10
- "0010 0010 0010 0010 0010 0010 0010 0010 0010 0010 " // 20
- "0010 0010 0010 0010 0010 0010 0010 0010 0010 0010 " // 30
- "0010 0010 0010 0030 0010 0010 0010 0010 0010 0010 " // 40
- "0010 0010 0010 0010 0010 0010 0010 0010 0010 0010 " // 50
- "0010 0010 0010 0030 0010 0010 0010 0010 0010 0010 " // 60
- "0010 0010 0010 0010 0010 0010 0010 0010 0010 0010 " // 70
- "0010 0010 0010 0030 0010 0010 0010 0030 0010 0010 " // 80
- "0010 0010 0010 0030 0010 0010 0010 0010 0010 0030 " // 90
- "0010 0010 0010 0030 0010 0010 0010 0010 0010 0030 " // 100
- "0010 0030 0010 0aa6"; // 104
-
- //------------------------------------------------------------------------------
- // This is the longest code we can support
- #define CODEMAX 200
-
- //------------------------------------------------------------------------------
- // This is the data we pull out of the pronto code
- typedef
- struct {
- int freq; // Carrier frequency (in Hz)
- int usec; // uSec per tick (based on freq)
-
- int codeLen; // Length of code
- uint16_t code[CODEMAX]; // Code in hex
-
- int onceLen; // Length of "once" transmit
- uint16_t* once; // Pointer to start within 'code'
-
- int rptLen; // Length of "repeat" transmit
- uint16_t* rpt; // Pointer to start within 'code'
- }
- pronto_t;
-
- //------------------------------------------------------------------------------
- // From what I have seen, the only time we go over 8-bits is the 'space'
- // on the end which creates the lead-out/inter-code gap. Assuming I'm right,
- // we can code this up as a special case and otherwise halve the size of our
- // data!
- // Ignoring the first four values (the config data) and the last value
- // (the lead-out), if you find a protocol that uses values greater than 00fe
- // we are going to have to revisit this code!
- //
- //
- // So, the 0th byte will be the carrier frequency in Khz (NOT Hz)
- // " 1st " " " " length of the "once" code
- // " 2nd " " " " length of the "repeat" code
- //
- // Thereafter, odd bytes will be Mark lengths as a multiple of USECPERTICK uS
- // even " " " Space " " " " " " "
- //
- // Any occurence of "FF" in either a Mark or a Space will indicate
- // "Use the 16-bit FF value" which will also be a multiple of USECPERTICK uS
- //
- //
- // As a point of comparison, the test code (prontoTest[]) is 520 bytes
- // (yes, more than 0.5KB of our Arduino's precious 32KB) ... after conversion
- // to pronto hex that goes down to ((520/5)*2) = 208 bytes ... once converted to
- // our format we are down to ((208/2) -1 -1 +2) = 104 bytes
- //
- // In fariness this is still very memory-hungry
- // ...As a rough guide:
- // 10 codes cost 1K of memory (this will vary depending on the protocol).
- //
- // So if you're building a complex remote control, you will probably need to
- // keep the codes on an external memory device (not in the Arduino sketch) and
- // load them as you need them. Hmmm.
- //
- // This dictates that "Oscillated Pronto Codes" are probably NOT the way forward
- //
- // For example, prontoTest[] happens to be: A 48-bit IR code in Denon format
- // So we know it starts with 80/40 (Denon header)
- // and ends with 10/aa6 (Denon leadout)
- // and all (48) bits in between are either 10/10 (Denon 0)
- // or 10/30 (Denon 1)
- // So we could easily store this data in 1-byte ("Denon")
- // + 1-byte (Length=48)
- // + 6-bytes (IR code)
- // At 8-bytes per code, we can store 128 codes in 1KB or memory - that's a lot
- // better than the 2 (two) we started off with!
- //
- // And serendipitously, by reducing the amount of data, our program will run
- // a LOT faster!
- //
- // Again, I repeat, even after you have spent time converting the "Oscillated
- // Pronto Codes" in to IRremote format, it will be a LOT more memory-hungry
- // than using sendDenon() (or whichever) ...BUT these codes are easily
- // available on the internet, so we'll support them!
- //
- typedef
- struct {
- uint16_t FF;
- uint8_t code[CODEMAX];
- }
- irCode_t;
-
- //------------------------------------------------------------------------------
- #define DEBUGF(...) printf(__VA_ARGS__)
-
- //+=============================================================================
- // String must be block of 4 hex digits separated with blanks
- //
- bool validate (char* cp, int* len)
- {
- for (*len = 0; *cp; (*len)++, cp += 4) {
- byp(&cp);
- if ( !ishex(cp[0]) || !ishex(cp[1]) ||
- !ishex(cp[2]) || !ishex(cp[3]) || !isblank(cp[4]) ) return false ;
- }
-
- return true;
- }
-
- //+=============================================================================
- // Hex-to-Byte : Decode a hex digit
- // We assume the character has already been validated
- //
- uint8_t htob (char ch)
- {
- if ((ch >= '0') && (ch <= '9')) return ch - '0' ;
- if ((ch >= 'A') && (ch <= 'F')) return ch - 'A' + 10 ;
- if ((ch >= 'a') && (ch <= 'f')) return ch - 'a' + 10 ;
- }
-
- //+=============================================================================
- // Hex-to-Word : Decode a block of 4 hex digits
- // We assume the string has already been validated
- // and the pointer being passed points at the start of a block of 4 hex digits
- //
- uint16_t htow (char* cp)
- {
- return ( (htob(cp[0]) << 12) | (htob(cp[1]) << 8) |
- (htob(cp[2]) << 4) | (htob(cp[3]) ) ) ;
- }
-
- //+=============================================================================
- // Convert the pronto string in to data
- //
- bool decode (char* s, pronto_t* p, irCode_t* ir)
- {
- int i, len;
- char* cp;
-
- // Validate the Pronto string
- if (!validate(s, &p->codeLen)) {
- DEBUGF("Invalid pronto string\n");
- return false ;
- }
- DEBUGF("Found %d hex codes\n", p->codeLen);
-
- // Allocate memory to store the decoded string
- //if (!(p->code = malloc(p->len))) {
- // DEBUGF("Memory allocation failed\n");
- // return false ;
- //}
-
- // Check in case our code is too long
- if (p->codeLen > CODEMAX) {
- DEBUGF("Code too long, edit CODEMAX and recompile\n");
- return false ;
- }
-
- // Decode the string
- cp = s;
- for (i = 0; i < p->codeLen; i++, cp += 4) {
- byp(&cp);
- p->code[i] = htow(cp);
- }
-
- // Announce our findings
- DEBUGF("Input: |%s|\n", s);
- DEBUGF("Found: |");
- for (i = 0; i < p->codeLen; i++) DEBUGF("%04x ", p->code[i]) ;
- DEBUGF("|\n");
-
- DEBUGF("Form [%04X] : ", p->code[0]);
- if (p->code[0] == 0x0000) DEBUGF("Oscillated (Learned)\n");
- else if (p->code[0] == 0x0100) DEBUGF("Unmodulated\n");
- else DEBUGF("Unknown\n");
- if (p->code[0] != 0x0000) return false ; // Can only handle Oscillated
-
- // Calculate the carrier frequency (+/- 10%) & uSecs per pulse
- // Pronto uses a crystal which generates a timeabse of 0.241246
- p->freq = (int)(1000000 / (p->code[1] * 0.241246));
- p->usec = (int)(((1.0 / p->freq) * 1000000) + 0.5);
- ir->code[0] = p->freq / 1000;
- DEBUGF("Freq [%04X] : %d Hz (%d uS/pluse) -> %d KHz\n",
- p->code[1], p->freq, p->usec, ir->code[0]);
-
- // Set the length & start pointer for the "once" code
- p->onceLen = p->code[2];
- p->once = &p->code[4];
- ir->code[1] = p->onceLen;
- DEBUGF("Once [%04X] : %d\n", p->code[2], p->onceLen);
-
- // Set the length & start pointer for the "repeat" code
- p->rptLen = p->code[3];
- p->rpt = &p->code[4 + p->onceLen];
- ir->code[2] = p->rptLen;
- DEBUGF("Rpt [%04X] : %d\n", p->code[3], p->rptLen);
-
- // Check everything tallies
- if (1 + 1 + 1 + 1 + (p->onceLen * 2) + (p->rptLen * 2) != p->codeLen) {
- DEBUGF("Bad code length\n");
- return false;
- }
-
- // Convert the IR data to our new format
- ir->FF = p->code[p->codeLen - 1];
-
- len = (p->onceLen * 2) + (p->rptLen * 2);
- DEBUGF("Encoded: |");
- for (i = 0; i < len; i++) {
- if (p->code[i+4] == ir->FF) {
- ir->code[i+3] = 0xFF;
- } else if (p->code[i+4] > 0xFE) {
- DEBUGF("\n%04X : Mark/Space overflow\n", p->code[i+4]);
- return false;
- } else {
- ir->code[i+3] = (p->code[i+4] * p->usec) / USECPERTICK;
- }
- DEBUGF("%s%d", !i ? "" : (i&1 ? "," : ", "), ir->code[i+3]);
- }
- DEBUGF("|\n");
-
- ir->FF = (ir->FF * p->usec) / USECPERTICK;
- DEBUGF("FF -> %d\n", ir->FF);
-
- return true;
- }
-
- //+=============================================================================
- //
- void irDump (irCode_t* ir)
- {
- int i, len;
-
- printf("uint8_t buttonName[%d] = {", len);
-
- printf("%d,%d, ", (ir->FF >> 8), ir->FF & 0xFF);
- printf("%d,%d,%d, ", ir->code[0], ir->code[1], ir->code[2]);
-
- len = (ir->code[1] * 2) + (ir->code[2] * 2);
- for (i = 0; i < len; i++) {
- printf("%s%d", !i ? "" : (i&1 ? "," : ", "), ir->code[i+3]);
- }
-
- printf("};\n");
-
- }
-
- //+=============================================================================
- //
- int main ( )
- {
- pronto_t pCode;
- irCode_t irCode;
-
- decode(prontoTest, &pCode, &irCode);
-
- irDump(&irCode);
-
- return 0;
- }
-
- #endif //0
|