|
| 1 | +#include "IRremote.h" |
| 2 | +#include <math.h> |
| 3 | + |
| 4 | +// Based off the Magiquest fork of Arduino-IRremote by mpflaga |
| 5 | +// https://github.com/mpflaga/Arduino-IRremote/ |
| 6 | + |
| 7 | +//============================================================================== |
| 8 | +// |
| 9 | +// |
| 10 | +// M A G I Q U E S T |
| 11 | +// |
| 12 | +// |
| 13 | +//============================================================================== |
| 14 | + |
| 15 | +// MagiQuest packet is both Wand ID and magnitude of swish and flick |
| 16 | +union magiquest_t { |
| 17 | + unsigned long long llword; |
| 18 | + unsigned char byte[8]; |
| 19 | + struct { |
| 20 | + unsigned int magnitude; |
| 21 | + unsigned long wand_id; |
| 22 | + char padding; |
| 23 | + char scrap;// just to pad the struct out to 64 bits so we can union with llword |
| 24 | + } cmd; |
| 25 | +}; |
| 26 | + |
| 27 | +#define MAGIQUEST_BITS 50 // The number of bits in the command itself |
| 28 | +#define MAGIQUEST_PERIOD 1150 // Length of time a full MQ "bit" consumes (1100 - 1200 usec) |
| 29 | +/* |
| 30 | + * 0 = 25% mark & 75% space across 1 period |
| 31 | + * 1150 * 0.25 = 288 usec mark |
| 32 | + * 1150 - 288 = 862 usec space |
| 33 | + * 1 = 50% mark & 50% space across 1 period |
| 34 | + * 1150 * 0.5 = 575 usec mark |
| 35 | + * 1150 - 575 = 575 usec space |
| 36 | + */ |
| 37 | +#define MAGIQUEST_ONE_MARK 575 |
| 38 | +#define MAGIQUEST_ONE_SPACE 575 |
| 39 | +#define MAGIQUEST_ZERO_MARK 288 |
| 40 | +#define MAGIQUEST_ZERO_SPACE 862 |
| 41 | + |
| 42 | +//+============================================================================= |
| 43 | +// |
| 44 | +#if SEND_MAGIQUEST |
| 45 | +void IRsend::sendMagiQuest(unsigned long wand_id, unsigned int magnitude) { |
| 46 | + magiquest_t data; |
| 47 | + |
| 48 | + data.llword = 0; |
| 49 | + data.cmd.wand_id = wand_id; |
| 50 | + data.cmd.magnitude = magnitude; |
| 51 | + |
| 52 | + // Set IR carrier frequency |
| 53 | + enableIROut(38); |
| 54 | + |
| 55 | + // Data |
| 56 | + for (unsigned long long mask = (unsigned long long)pow(2, MAGIQUEST_BITS-1); mask > 0; mask >>= 1) { |
| 57 | + if (data.llword & mask) { |
| 58 | + DBG_PRINT("1"); |
| 59 | + mark(MAGIQUEST_ONE_MARK); |
| 60 | + space(MAGIQUEST_ONE_SPACE); |
| 61 | + } else { |
| 62 | + DBG_PRINT("0"); |
| 63 | + mark(MAGIQUEST_ZERO_MARK); |
| 64 | + space(MAGIQUEST_ZERO_SPACE); |
| 65 | + } |
| 66 | + } |
| 67 | +DBG_PRINTLN(""); |
| 68 | + |
| 69 | + // Footer |
| 70 | + mark(MAGIQUEST_ZERO_MARK); |
| 71 | + space(0); // Always end with the LED off |
| 72 | +} |
| 73 | +#endif |
| 74 | + |
| 75 | +//+============================================================================= |
| 76 | +// |
| 77 | +#if DECODE_MAGIQUEST |
| 78 | +bool IRrecv::decodeMagiQuest(decode_results *results) { |
| 79 | + magiquest_t data; // Somewhere to build our code |
| 80 | + unsigned int offset = 1; // Skip the Gap reading |
| 81 | + |
| 82 | + unsigned int mark_; |
| 83 | + unsigned int space_; |
| 84 | + unsigned int ratio_; |
| 85 | + |
| 86 | +#if DEBUG |
| 87 | + char bitstring[MAGIQUEST_BITS*2]; |
| 88 | + memset(bitstring, 0, sizeof(bitstring)); |
| 89 | +#endif |
| 90 | + |
| 91 | + // Check we have enough data |
| 92 | + if (irparams.rawlen < 2 * MAGIQUEST_BITS) { |
| 93 | + DBG_PRINT("Not enough bits to be a MagiQuest packet ("); |
| 94 | + DBG_PRINT(irparams.rawlen); |
| 95 | + DBG_PRINT(" < "); |
| 96 | + DBG_PRINT(MAGIQUEST_BITS*2); |
| 97 | + DBG_PRINTLN(")"); |
| 98 | + return false; |
| 99 | + } |
| 100 | + |
| 101 | + // Read the bits in |
| 102 | + data.llword = 0; |
| 103 | + while (offset+1 < irparams.rawlen) { |
| 104 | + mark_ = results->rawbuf[offset++]; |
| 105 | + space_ = results->rawbuf[offset++]; |
| 106 | + ratio_ = space_ / mark_; |
| 107 | + |
| 108 | + DBG_PRINT("mark="); |
| 109 | + DBG_PRINT(mark_ * MICROS_PER_TICK); |
| 110 | + DBG_PRINT(" space="); |
| 111 | + DBG_PRINT(space_ * MICROS_PER_TICK); |
| 112 | + DBG_PRINT(" ratio="); |
| 113 | + DBG_PRINTLN(ratio_); |
| 114 | + |
| 115 | + if (MATCH_MARK(space_ + mark_, MAGIQUEST_PERIOD)) { |
| 116 | + if (ratio_ > 1) { |
| 117 | + // It's a 0 |
| 118 | + data.llword <<= 1; |
| 119 | +#if DEBUG |
| 120 | + bitstring[(offset/2)-1] = '0'; |
| 121 | +#endif |
| 122 | + } else { |
| 123 | + // It's a 1 |
| 124 | + data.llword = (data.llword << 1) | 1; |
| 125 | +#if DEBUG |
| 126 | + bitstring[(offset/2)-1] = '1'; |
| 127 | +#endif |
| 128 | + } |
| 129 | + } else { |
| 130 | + DBG_PRINTLN("MATCH_MARK failed"); |
| 131 | + return false; |
| 132 | + } |
| 133 | + } |
| 134 | +#if DEBUG |
| 135 | + DBG_PRINTLN(bitstring); |
| 136 | +#endif |
| 137 | + |
| 138 | + // Success |
| 139 | + results->decode_type = MAGIQUEST; |
| 140 | + results->bits = offset / 2; |
| 141 | + results->value = data.cmd.wand_id; |
| 142 | + results->magnitude = data.cmd.magnitude; |
| 143 | + |
| 144 | + DBG_PRINT("MQ: bits="); |
| 145 | + DBG_PRINT(results->bits); |
| 146 | + DBG_PRINT(" value="); |
| 147 | + DBG_PRINT(results->value); |
| 148 | + DBG_PRINT(" magnitude="); |
| 149 | + DBG_PRINTLN(results->magnitude); |
| 150 | + |
| 151 | + return true; |
| 152 | +} |
| 153 | +#endif |
0 commit comments