Final Project로 실제 점검에 사용된 코드
Dependencies: mbed Adafruit_GFX
RemoteIR/TransmitterIR.cpp@0:22391cd705e2, 2019-06-15 (annotated)
- Committer:
- 21400688
- Date:
- Sat Jun 15 20:52:15 2019 +0000
- Revision:
- 0:22391cd705e2
vb
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
21400688 | 0:22391cd705e2 | 1 | /** |
21400688 | 0:22391cd705e2 | 2 | * IR transmitter (Version 0.0.4) |
21400688 | 0:22391cd705e2 | 3 | * |
21400688 | 0:22391cd705e2 | 4 | * Copyright (C) 2010 Shinichiro Nakamura (CuBeatSystems) |
21400688 | 0:22391cd705e2 | 5 | * http://shinta.main.jp/ |
21400688 | 0:22391cd705e2 | 6 | */ |
21400688 | 0:22391cd705e2 | 7 | |
21400688 | 0:22391cd705e2 | 8 | #include "TransmitterIR.h" |
21400688 | 0:22391cd705e2 | 9 | |
21400688 | 0:22391cd705e2 | 10 | #define LOCK() |
21400688 | 0:22391cd705e2 | 11 | #define UNLOCK() |
21400688 | 0:22391cd705e2 | 12 | |
21400688 | 0:22391cd705e2 | 13 | /** |
21400688 | 0:22391cd705e2 | 14 | * Constructor. |
21400688 | 0:22391cd705e2 | 15 | * |
21400688 | 0:22391cd705e2 | 16 | * @param txpin Pin for transmit IR signal. |
21400688 | 0:22391cd705e2 | 17 | */ |
21400688 | 0:22391cd705e2 | 18 | TransmitterIR::TransmitterIR(PinName txpin) : tx(txpin) { |
21400688 | 0:22391cd705e2 | 19 | tx.write(0.0); |
21400688 | 0:22391cd705e2 | 20 | tx.period_us(26.3); |
21400688 | 0:22391cd705e2 | 21 | |
21400688 | 0:22391cd705e2 | 22 | work.state = Idle; |
21400688 | 0:22391cd705e2 | 23 | work.bitcount = 0; |
21400688 | 0:22391cd705e2 | 24 | work.leader = 0; |
21400688 | 0:22391cd705e2 | 25 | work.data = 0; |
21400688 | 0:22391cd705e2 | 26 | work.trailer = 0; |
21400688 | 0:22391cd705e2 | 27 | |
21400688 | 0:22391cd705e2 | 28 | data.format = RemoteIR::UNKNOWN; |
21400688 | 0:22391cd705e2 | 29 | data.bitlength = 0; |
21400688 | 0:22391cd705e2 | 30 | } |
21400688 | 0:22391cd705e2 | 31 | |
21400688 | 0:22391cd705e2 | 32 | /** |
21400688 | 0:22391cd705e2 | 33 | * Destructor. |
21400688 | 0:22391cd705e2 | 34 | */ |
21400688 | 0:22391cd705e2 | 35 | TransmitterIR::~TransmitterIR() { |
21400688 | 0:22391cd705e2 | 36 | } |
21400688 | 0:22391cd705e2 | 37 | |
21400688 | 0:22391cd705e2 | 38 | /** |
21400688 | 0:22391cd705e2 | 39 | * Get state. |
21400688 | 0:22391cd705e2 | 40 | * |
21400688 | 0:22391cd705e2 | 41 | * @return Current state. |
21400688 | 0:22391cd705e2 | 42 | */ |
21400688 | 0:22391cd705e2 | 43 | TransmitterIR::State TransmitterIR::getState(void) { |
21400688 | 0:22391cd705e2 | 44 | LOCK(); |
21400688 | 0:22391cd705e2 | 45 | State s = work.state; |
21400688 | 0:22391cd705e2 | 46 | UNLOCK(); |
21400688 | 0:22391cd705e2 | 47 | return s; |
21400688 | 0:22391cd705e2 | 48 | } |
21400688 | 0:22391cd705e2 | 49 | |
21400688 | 0:22391cd705e2 | 50 | /** |
21400688 | 0:22391cd705e2 | 51 | * Set data. |
21400688 | 0:22391cd705e2 | 52 | * |
21400688 | 0:22391cd705e2 | 53 | * @param format Format. |
21400688 | 0:22391cd705e2 | 54 | * @param buf Buffer of a data. |
21400688 | 0:22391cd705e2 | 55 | * @param bitlength Bit length of the data. |
21400688 | 0:22391cd705e2 | 56 | * |
21400688 | 0:22391cd705e2 | 57 | * @return Data bit length. |
21400688 | 0:22391cd705e2 | 58 | */ |
21400688 | 0:22391cd705e2 | 59 | int TransmitterIR::setData(RemoteIR::Format format, uint8_t *buf, int bitlength) { |
21400688 | 0:22391cd705e2 | 60 | LOCK(); |
21400688 | 0:22391cd705e2 | 61 | if (work.state != Idle) { |
21400688 | 0:22391cd705e2 | 62 | UNLOCK(); |
21400688 | 0:22391cd705e2 | 63 | return -1; |
21400688 | 0:22391cd705e2 | 64 | } |
21400688 | 0:22391cd705e2 | 65 | |
21400688 | 0:22391cd705e2 | 66 | work.state = Leader; |
21400688 | 0:22391cd705e2 | 67 | work.bitcount = 0; |
21400688 | 0:22391cd705e2 | 68 | work.leader = 0; |
21400688 | 0:22391cd705e2 | 69 | work.data = 0; |
21400688 | 0:22391cd705e2 | 70 | work.trailer = 0; |
21400688 | 0:22391cd705e2 | 71 | |
21400688 | 0:22391cd705e2 | 72 | data.format = format; |
21400688 | 0:22391cd705e2 | 73 | data.bitlength = bitlength; |
21400688 | 0:22391cd705e2 | 74 | const int n = bitlength / 8 + (((bitlength % 8) != 0) ? 1 : 0); |
21400688 | 0:22391cd705e2 | 75 | for (int i = 0; i < n; i++) { |
21400688 | 0:22391cd705e2 | 76 | data.buffer[i] = buf[i]; |
21400688 | 0:22391cd705e2 | 77 | } |
21400688 | 0:22391cd705e2 | 78 | |
21400688 | 0:22391cd705e2 | 79 | switch (format) { |
21400688 | 0:22391cd705e2 | 80 | case RemoteIR::NEC: |
21400688 | 0:22391cd705e2 | 81 | ticker.detach(); |
21400688 | 0:22391cd705e2 | 82 | ticker.attach_us(this, &TransmitterIR::tick, RemoteIR::TUS_NEC); |
21400688 | 0:22391cd705e2 | 83 | break; |
21400688 | 0:22391cd705e2 | 84 | case RemoteIR::AEHA: |
21400688 | 0:22391cd705e2 | 85 | ticker.detach(); |
21400688 | 0:22391cd705e2 | 86 | ticker.attach_us(this, &TransmitterIR::tick, RemoteIR::TUS_AEHA); |
21400688 | 0:22391cd705e2 | 87 | break; |
21400688 | 0:22391cd705e2 | 88 | case RemoteIR::SONY: |
21400688 | 0:22391cd705e2 | 89 | ticker.detach(); |
21400688 | 0:22391cd705e2 | 90 | ticker.attach_us(this, &TransmitterIR::tick, RemoteIR::TUS_SONY); |
21400688 | 0:22391cd705e2 | 91 | break; |
21400688 | 0:22391cd705e2 | 92 | } |
21400688 | 0:22391cd705e2 | 93 | |
21400688 | 0:22391cd705e2 | 94 | UNLOCK(); |
21400688 | 0:22391cd705e2 | 95 | return bitlength; |
21400688 | 0:22391cd705e2 | 96 | } |
21400688 | 0:22391cd705e2 | 97 | |
21400688 | 0:22391cd705e2 | 98 | void TransmitterIR::tick(void) { |
21400688 | 0:22391cd705e2 | 99 | LOCK(); |
21400688 | 0:22391cd705e2 | 100 | switch (work.state) { |
21400688 | 0:22391cd705e2 | 101 | case Idle: |
21400688 | 0:22391cd705e2 | 102 | work.bitcount = 0; |
21400688 | 0:22391cd705e2 | 103 | work.leader = 0; |
21400688 | 0:22391cd705e2 | 104 | work.data = 0; |
21400688 | 0:22391cd705e2 | 105 | work.trailer = 0; |
21400688 | 0:22391cd705e2 | 106 | break; |
21400688 | 0:22391cd705e2 | 107 | case Leader: |
21400688 | 0:22391cd705e2 | 108 | if (data.format == RemoteIR::NEC) { |
21400688 | 0:22391cd705e2 | 109 | /* |
21400688 | 0:22391cd705e2 | 110 | * NEC. |
21400688 | 0:22391cd705e2 | 111 | */ |
21400688 | 0:22391cd705e2 | 112 | static const int LEADER_NEC_HEAD = 16; |
21400688 | 0:22391cd705e2 | 113 | static const int LEADER_NEC_TAIL = 8; |
21400688 | 0:22391cd705e2 | 114 | if (work.leader < LEADER_NEC_HEAD) { |
21400688 | 0:22391cd705e2 | 115 | tx.write(0.5); |
21400688 | 0:22391cd705e2 | 116 | } else { |
21400688 | 0:22391cd705e2 | 117 | tx.write(0.0); |
21400688 | 0:22391cd705e2 | 118 | } |
21400688 | 0:22391cd705e2 | 119 | work.leader++; |
21400688 | 0:22391cd705e2 | 120 | if ((LEADER_NEC_HEAD + LEADER_NEC_TAIL) <= work.leader) { |
21400688 | 0:22391cd705e2 | 121 | work.state = Data; |
21400688 | 0:22391cd705e2 | 122 | } |
21400688 | 0:22391cd705e2 | 123 | } else if (data.format == RemoteIR::AEHA) { |
21400688 | 0:22391cd705e2 | 124 | /* |
21400688 | 0:22391cd705e2 | 125 | * AEHA. |
21400688 | 0:22391cd705e2 | 126 | */ |
21400688 | 0:22391cd705e2 | 127 | static const int LEADER_AEHA_HEAD = 8; |
21400688 | 0:22391cd705e2 | 128 | static const int LEADER_AEHA_TAIL = 4; |
21400688 | 0:22391cd705e2 | 129 | if (work.leader < LEADER_AEHA_HEAD) { |
21400688 | 0:22391cd705e2 | 130 | tx.write(0.5); |
21400688 | 0:22391cd705e2 | 131 | } else { |
21400688 | 0:22391cd705e2 | 132 | tx.write(0.0); |
21400688 | 0:22391cd705e2 | 133 | } |
21400688 | 0:22391cd705e2 | 134 | work.leader++; |
21400688 | 0:22391cd705e2 | 135 | if ((LEADER_AEHA_HEAD + LEADER_AEHA_TAIL) <= work.leader) { |
21400688 | 0:22391cd705e2 | 136 | work.state = Data; |
21400688 | 0:22391cd705e2 | 137 | } |
21400688 | 0:22391cd705e2 | 138 | } else if (data.format == RemoteIR::SONY) { |
21400688 | 0:22391cd705e2 | 139 | /* |
21400688 | 0:22391cd705e2 | 140 | * SONY. |
21400688 | 0:22391cd705e2 | 141 | */ |
21400688 | 0:22391cd705e2 | 142 | static const int LEADER_SONY_HEAD = 4; |
21400688 | 0:22391cd705e2 | 143 | static const int LEADER_SONY_TAIL = 0; |
21400688 | 0:22391cd705e2 | 144 | if (work.leader < LEADER_SONY_HEAD) { |
21400688 | 0:22391cd705e2 | 145 | tx.write(0.5); |
21400688 | 0:22391cd705e2 | 146 | } else { |
21400688 | 0:22391cd705e2 | 147 | tx.write(0.0); |
21400688 | 0:22391cd705e2 | 148 | } |
21400688 | 0:22391cd705e2 | 149 | work.leader++; |
21400688 | 0:22391cd705e2 | 150 | if ((LEADER_SONY_HEAD + LEADER_SONY_TAIL) <= work.leader) { |
21400688 | 0:22391cd705e2 | 151 | work.state = Data; |
21400688 | 0:22391cd705e2 | 152 | } |
21400688 | 0:22391cd705e2 | 153 | } else { |
21400688 | 0:22391cd705e2 | 154 | } |
21400688 | 0:22391cd705e2 | 155 | break; |
21400688 | 0:22391cd705e2 | 156 | case Data: |
21400688 | 0:22391cd705e2 | 157 | if (data.format == RemoteIR::NEC) { |
21400688 | 0:22391cd705e2 | 158 | /* |
21400688 | 0:22391cd705e2 | 159 | * NEC. |
21400688 | 0:22391cd705e2 | 160 | */ |
21400688 | 0:22391cd705e2 | 161 | if (work.data == 0) { |
21400688 | 0:22391cd705e2 | 162 | tx.write(0.5); |
21400688 | 0:22391cd705e2 | 163 | work.data++; |
21400688 | 0:22391cd705e2 | 164 | } else { |
21400688 | 0:22391cd705e2 | 165 | tx.write(0.0); |
21400688 | 0:22391cd705e2 | 166 | if (0 != (data.buffer[work.bitcount / 8] & (1 << work.bitcount % 8))) { |
21400688 | 0:22391cd705e2 | 167 | if (3 <= work.data) { |
21400688 | 0:22391cd705e2 | 168 | work.bitcount++; |
21400688 | 0:22391cd705e2 | 169 | work.data = 0; |
21400688 | 0:22391cd705e2 | 170 | } else { |
21400688 | 0:22391cd705e2 | 171 | work.data++; |
21400688 | 0:22391cd705e2 | 172 | } |
21400688 | 0:22391cd705e2 | 173 | } else { |
21400688 | 0:22391cd705e2 | 174 | if (1 <= work.data) { |
21400688 | 0:22391cd705e2 | 175 | work.bitcount++; |
21400688 | 0:22391cd705e2 | 176 | work.data = 0; |
21400688 | 0:22391cd705e2 | 177 | } else { |
21400688 | 0:22391cd705e2 | 178 | work.data++; |
21400688 | 0:22391cd705e2 | 179 | } |
21400688 | 0:22391cd705e2 | 180 | } |
21400688 | 0:22391cd705e2 | 181 | } |
21400688 | 0:22391cd705e2 | 182 | if (data.bitlength <= work.bitcount) { |
21400688 | 0:22391cd705e2 | 183 | work.state = Trailer; |
21400688 | 0:22391cd705e2 | 184 | } |
21400688 | 0:22391cd705e2 | 185 | } else if (data.format == RemoteIR::AEHA) { |
21400688 | 0:22391cd705e2 | 186 | /* |
21400688 | 0:22391cd705e2 | 187 | * AEHA. |
21400688 | 0:22391cd705e2 | 188 | */ |
21400688 | 0:22391cd705e2 | 189 | if (work.data == 0) { |
21400688 | 0:22391cd705e2 | 190 | tx.write(0.5); |
21400688 | 0:22391cd705e2 | 191 | work.data++; |
21400688 | 0:22391cd705e2 | 192 | } else { |
21400688 | 0:22391cd705e2 | 193 | tx.write(0.0); |
21400688 | 0:22391cd705e2 | 194 | if (0 != (data.buffer[work.bitcount / 8] & (1 << work.bitcount % 8))) { |
21400688 | 0:22391cd705e2 | 195 | if (3 <= work.data) { |
21400688 | 0:22391cd705e2 | 196 | work.bitcount++; |
21400688 | 0:22391cd705e2 | 197 | work.data = 0; |
21400688 | 0:22391cd705e2 | 198 | } else { |
21400688 | 0:22391cd705e2 | 199 | work.data++; |
21400688 | 0:22391cd705e2 | 200 | } |
21400688 | 0:22391cd705e2 | 201 | } else { |
21400688 | 0:22391cd705e2 | 202 | if (1 <= work.data) { |
21400688 | 0:22391cd705e2 | 203 | work.bitcount++; |
21400688 | 0:22391cd705e2 | 204 | work.data = 0; |
21400688 | 0:22391cd705e2 | 205 | } else { |
21400688 | 0:22391cd705e2 | 206 | work.data++; |
21400688 | 0:22391cd705e2 | 207 | } |
21400688 | 0:22391cd705e2 | 208 | } |
21400688 | 0:22391cd705e2 | 209 | } |
21400688 | 0:22391cd705e2 | 210 | if (data.bitlength <= work.bitcount) { |
21400688 | 0:22391cd705e2 | 211 | work.state = Trailer; |
21400688 | 0:22391cd705e2 | 212 | } |
21400688 | 0:22391cd705e2 | 213 | } else if (data.format == RemoteIR::SONY) { |
21400688 | 0:22391cd705e2 | 214 | /* |
21400688 | 0:22391cd705e2 | 215 | * SONY. |
21400688 | 0:22391cd705e2 | 216 | */ |
21400688 | 0:22391cd705e2 | 217 | if (work.data == 0) { |
21400688 | 0:22391cd705e2 | 218 | tx.write(0.0); |
21400688 | 0:22391cd705e2 | 219 | work.data++; |
21400688 | 0:22391cd705e2 | 220 | } else { |
21400688 | 0:22391cd705e2 | 221 | tx.write(0.5); |
21400688 | 0:22391cd705e2 | 222 | if (0 != (data.buffer[work.bitcount / 8] & (1 << work.bitcount % 8))) { |
21400688 | 0:22391cd705e2 | 223 | if (2 <= work.data) { |
21400688 | 0:22391cd705e2 | 224 | work.bitcount++; |
21400688 | 0:22391cd705e2 | 225 | work.data = 0; |
21400688 | 0:22391cd705e2 | 226 | } else { |
21400688 | 0:22391cd705e2 | 227 | work.data++; |
21400688 | 0:22391cd705e2 | 228 | } |
21400688 | 0:22391cd705e2 | 229 | } else { |
21400688 | 0:22391cd705e2 | 230 | if (1 <= work.data) { |
21400688 | 0:22391cd705e2 | 231 | work.bitcount++; |
21400688 | 0:22391cd705e2 | 232 | work.data = 0; |
21400688 | 0:22391cd705e2 | 233 | } else { |
21400688 | 0:22391cd705e2 | 234 | work.data++; |
21400688 | 0:22391cd705e2 | 235 | } |
21400688 | 0:22391cd705e2 | 236 | } |
21400688 | 0:22391cd705e2 | 237 | } |
21400688 | 0:22391cd705e2 | 238 | if (data.bitlength <= work.bitcount) { |
21400688 | 0:22391cd705e2 | 239 | work.state = Trailer; |
21400688 | 0:22391cd705e2 | 240 | } |
21400688 | 0:22391cd705e2 | 241 | } else { |
21400688 | 0:22391cd705e2 | 242 | } |
21400688 | 0:22391cd705e2 | 243 | break; |
21400688 | 0:22391cd705e2 | 244 | case Trailer: |
21400688 | 0:22391cd705e2 | 245 | if (data.format == RemoteIR::NEC) { |
21400688 | 0:22391cd705e2 | 246 | /* |
21400688 | 0:22391cd705e2 | 247 | * NEC. |
21400688 | 0:22391cd705e2 | 248 | */ |
21400688 | 0:22391cd705e2 | 249 | static const int TRAILER_NEC_HEAD = 1; |
21400688 | 0:22391cd705e2 | 250 | static const int TRAILER_NEC_TAIL = 2; |
21400688 | 0:22391cd705e2 | 251 | if (work.trailer < TRAILER_NEC_HEAD) { |
21400688 | 0:22391cd705e2 | 252 | tx.write(0.5); |
21400688 | 0:22391cd705e2 | 253 | } else { |
21400688 | 0:22391cd705e2 | 254 | tx.write(0.0); |
21400688 | 0:22391cd705e2 | 255 | } |
21400688 | 0:22391cd705e2 | 256 | work.trailer++; |
21400688 | 0:22391cd705e2 | 257 | if ((TRAILER_NEC_HEAD + TRAILER_NEC_TAIL) <= work.trailer) { |
21400688 | 0:22391cd705e2 | 258 | work.state = Idle; |
21400688 | 0:22391cd705e2 | 259 | //ticker.detach(); |
21400688 | 0:22391cd705e2 | 260 | } |
21400688 | 0:22391cd705e2 | 261 | } else if (data.format == RemoteIR::AEHA) { |
21400688 | 0:22391cd705e2 | 262 | /* |
21400688 | 0:22391cd705e2 | 263 | * AEHA. |
21400688 | 0:22391cd705e2 | 264 | */ |
21400688 | 0:22391cd705e2 | 265 | static const int TRAILER_AEHA_HEAD = 1; |
21400688 | 0:22391cd705e2 | 266 | static const int TRAILER_AEHA_TAIL = 8000 / RemoteIR::TUS_AEHA; |
21400688 | 0:22391cd705e2 | 267 | if (work.trailer < TRAILER_AEHA_HEAD) { |
21400688 | 0:22391cd705e2 | 268 | tx.write(0.5); |
21400688 | 0:22391cd705e2 | 269 | } else { |
21400688 | 0:22391cd705e2 | 270 | tx.write(0.0); |
21400688 | 0:22391cd705e2 | 271 | } |
21400688 | 0:22391cd705e2 | 272 | work.trailer++; |
21400688 | 0:22391cd705e2 | 273 | if ((TRAILER_AEHA_HEAD + TRAILER_AEHA_TAIL) <= work.trailer) { |
21400688 | 0:22391cd705e2 | 274 | work.state = Idle; |
21400688 | 0:22391cd705e2 | 275 | //ticker.detach(); |
21400688 | 0:22391cd705e2 | 276 | } |
21400688 | 0:22391cd705e2 | 277 | } else if (data.format == RemoteIR::SONY) { |
21400688 | 0:22391cd705e2 | 278 | /* |
21400688 | 0:22391cd705e2 | 279 | * SONY. |
21400688 | 0:22391cd705e2 | 280 | */ |
21400688 | 0:22391cd705e2 | 281 | static const int TRAILER_SONY_HEAD = 0; |
21400688 | 0:22391cd705e2 | 282 | static const int TRAILER_SONY_TAIL = 0; |
21400688 | 0:22391cd705e2 | 283 | if (work.trailer < TRAILER_SONY_HEAD) { |
21400688 | 0:22391cd705e2 | 284 | tx.write(0.5); |
21400688 | 0:22391cd705e2 | 285 | } else { |
21400688 | 0:22391cd705e2 | 286 | tx.write(0.0); |
21400688 | 0:22391cd705e2 | 287 | } |
21400688 | 0:22391cd705e2 | 288 | work.trailer++; |
21400688 | 0:22391cd705e2 | 289 | if ((TRAILER_SONY_HEAD + TRAILER_SONY_TAIL) <= work.trailer) { |
21400688 | 0:22391cd705e2 | 290 | work.state = Idle; |
21400688 | 0:22391cd705e2 | 291 | //ticker.detach(); |
21400688 | 0:22391cd705e2 | 292 | } |
21400688 | 0:22391cd705e2 | 293 | } else { |
21400688 | 0:22391cd705e2 | 294 | } |
21400688 | 0:22391cd705e2 | 295 | break; |
21400688 | 0:22391cd705e2 | 296 | default: |
21400688 | 0:22391cd705e2 | 297 | break; |
21400688 | 0:22391cd705e2 | 298 | } |
21400688 | 0:22391cd705e2 | 299 | UNLOCK(); |
21400688 | 0:22391cd705e2 | 300 | } |