Skip to content

Instantly share code, notes, and snippets.

@kelvie
Created July 31, 2021 17:42
Show Gist options
  • Save kelvie/891fc8bfc8353594136ee1ac790e60f7 to your computer and use it in GitHub Desktop.
Save kelvie/891fc8bfc8353594136ee1ac790e60f7 to your computer and use it in GitHub Desktop.
lookup table for converting NEC commands/addresses to the esphome32 remote_transmitter.transmit_nec format
#ifndef UTIL_H
#define UTIL_H
int nec_format(unsigned char b) {
static const int nec_code_table[256] = {
0x00ff, 0x807f, 0x40bf, 0xc03f, 0x20df, 0xa05f, 0x609f, 0xe01f, 0x10ef,
0x906f, 0x50af, 0xd02f, 0x30cf, 0xb04f, 0x708f, 0xf00f, 0x08f7, 0x8877,
0x48b7, 0xc837, 0x28d7, 0xa857, 0x6897, 0xe817, 0x18e7, 0x9867, 0x58a7,
0xd827, 0x38c7, 0xb847, 0x7887, 0xf807, 0x04fb, 0x847b, 0x44bb, 0xc43b,
0x24db, 0xa45b, 0x649b, 0xe41b, 0x14eb, 0x946b, 0x54ab, 0xd42b, 0x34cb,
0xb44b, 0x748b, 0xf40b, 0x0cf3, 0x8c73, 0x4cb3, 0xcc33, 0x2cd3, 0xac53,
0x6c93, 0xec13, 0x1ce3, 0x9c63, 0x5ca3, 0xdc23, 0x3cc3, 0xbc43, 0x7c83,
0xfc03, 0x02fd, 0x827d, 0x42bd, 0xc23d, 0x22dd, 0xa25d, 0x629d, 0xe21d,
0x12ed, 0x926d, 0x52ad, 0xd22d, 0x32cd, 0xb24d, 0x728d, 0xf20d, 0x0af5,
0x8a75, 0x4ab5, 0xca35, 0x2ad5, 0xaa55, 0x6a95, 0xea15, 0x1ae5, 0x9a65,
0x5aa5, 0xda25, 0x3ac5, 0xba45, 0x7a85, 0xfa05, 0x06f9, 0x8679, 0x46b9,
0xc639, 0x26d9, 0xa659, 0x6699, 0xe619, 0x16e9, 0x9669, 0x56a9, 0xd629,
0x36c9, 0xb649, 0x7689, 0xf609, 0x0ef1, 0x8e71, 0x4eb1, 0xce31, 0x2ed1,
0xae51, 0x6e91, 0xee11, 0x1ee1, 0x9e61, 0x5ea1, 0xde21, 0x3ec1, 0xbe41,
0x7e81, 0xfe01, 0x01fe, 0x817e, 0x41be, 0xc13e, 0x21de, 0xa15e, 0x619e,
0xe11e, 0x11ee, 0x916e, 0x51ae, 0xd12e, 0x31ce, 0xb14e, 0x718e, 0xf10e,
0x09f6, 0x8976, 0x49b6, 0xc936, 0x29d6, 0xa956, 0x6996, 0xe916, 0x19e6,
0x9966, 0x59a6, 0xd926, 0x39c6, 0xb946, 0x7986, 0xf906, 0x05fa, 0x857a,
0x45ba, 0xc53a, 0x25da, 0xa55a, 0x659a, 0xe51a, 0x15ea, 0x956a, 0x55aa,
0xd52a, 0x35ca, 0xb54a, 0x758a, 0xf50a, 0x0df2, 0x8d72, 0x4db2, 0xcd32,
0x2dd2, 0xad52, 0x6d92, 0xed12, 0x1de2, 0x9d62, 0x5da2, 0xdd22, 0x3dc2,
0xbd42, 0x7d82, 0xfd02, 0x03fc, 0x837c, 0x43bc, 0xc33c, 0x23dc, 0xa35c,
0x639c, 0xe31c, 0x13ec, 0x936c, 0x53ac, 0xd32c, 0x33cc, 0xb34c, 0x738c,
0xf30c, 0x0bf4, 0x8b74, 0x4bb4, 0xcb34, 0x2bd4, 0xab54, 0x6b94, 0xeb14,
0x1be4, 0x9b64, 0x5ba4, 0xdb24, 0x3bc4, 0xbb44, 0x7b84, 0xfb04, 0x07f8,
0x8778, 0x47b8, 0xc738, 0x27d8, 0xa758, 0x6798, 0xe718, 0x17e8, 0x9768,
0x57a8, 0xd728, 0x37c8, 0xb748, 0x7788, 0xf708, 0x0ff0, 0x8f70, 0x4fb0,
0xcf30, 0x2fd0, 0xaf50, 0x6f90, 0xef10, 0x1fe0, 0x9f60, 0x5fa0, 0xdf20,
0x3fc0, 0xbf40, 0x7f80, 0xff00};
return nec_code_table[b];
}
#endif // UTIL_H
@kelvie
Copy link
Author

kelvie commented Jul 31, 2021

Generated using:

#include "stdio.h"


int nec_format(unsigned char b) {
  // Reverse bits as NEC sends LSB first
  b = (b & 0xF0) >> 4 | (b & 0x0F) << 4;
  b = (b & 0xCC) >> 2 | (b & 0x33) << 2;
  b = (b & 0xAA) >> 1 | (b & 0x55) << 1;

  // Calculate inverse in lower bytes
  return (b << 8) + (~b & 0xff);
}


int main(int argc, char **argv) {
    printf("static const int nec_code_table[256] = {\n    ");
    for (unsigned int i=0; i <= 0xff; i++) {
        printf("0x%.4x", nec_format(i));

        if (i != 0xff) {
            printf(", ");
        }

        if ((i % 8 == 7)) {
            printf("\n");
            if (i != 0xff) {
              printf("    ");
            }
        }
    }
    printf("};\n");

    return 0;
}

@robin-thoni
Copy link

From: https://esphome.io/components/remote_transmitter.html#remote-transmitter-transmit-nec-action

Note
In version 2021.12, the order of transferring bits was corrected from MSB to LSB in accordance with the NEC standard.
Therefore, if the configuration file has come from an earlier version of ESPhome,
it is necessary to reverse the order of the address and command bits when moving to 2021.12 or above.
For example, address: 0x84ED, command: 0x13EC becomes 0xB721 and 0x37C8 respectively.

Thus, you can now use something as simple as this:

#ifndef UTIL_H

#define UTIL_H

uint16_t nec_format(uint8_t c) {
    return (~c << 8) | c;
}

#endif
button:
  - platform: template
    name: Radiator Silvercrest Power
    id: radiator_silvercrest_power
    on_press:
      - remote_transmitter.transmit_nec:
          address: !lambda |-
            return nec_format(0x80);
          command: !lambda |-
            return nec_format(0x12);

Here's a good reading on the NEC protocol

@kelvie
Copy link
Author

kelvie commented Jan 28, 2023

Yeah, I fixed this last year in my own code, but forgot about this gist! thanks for the update.

@Pastitas
Copy link

Pastitas commented May 2, 2024

What about making this the default with the upstream? is there any problem with that?

@kelvie
Copy link
Author

kelvie commented May 2, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment