├── Encoder.cpp ├── Encoder.h ├── LICENSE ├── README.md ├── assets ├── fritzing-schematic.png ├── lasercut-case.svg └── portrait.jpg ├── kitchen-timer.ino ├── kitchen_font.h ├── music.cpp ├── music.h ├── music_data.h ├── pitches.h ├── toneAC.cpp ├── toneAC.h └── utility ├── direct_pin_read.h ├── interrupt_config.h └── interrupt_pins.h /Encoder.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include "Encoder.h" 3 | 4 | // Yes, all the code is in the header file, to provide the user 5 | // configure options with #define (before they include it), and 6 | // to facilitate some crafty optimizations! 7 | 8 | Encoder_internal_state_t * Encoder::interruptArgs[]; 9 | 10 | 11 | -------------------------------------------------------------------------------- /Encoder.h: -------------------------------------------------------------------------------- 1 | /* Encoder Library, for measuring quadrature encoded signals 2 | * http://www.pjrc.com/teensy/td_libs_Encoder.html 3 | * Copyright (c) 2011,2013 PJRC.COM, LLC - Paul Stoffregen 4 | * 5 | * Version 1.2 - fix -2 bug in C-only code 6 | * Version 1.1 - expand to support boards with up to 60 interrupts 7 | * Version 1.0 - initial release 8 | * 9 | * Permission is hereby granted, free of charge, to any person obtaining a copy 10 | * of this software and associated documentation files (the "Software"), to deal 11 | * in the Software without restriction, including without limitation the rights 12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 | * copies of the Software, and to permit persons to whom the Software is 14 | * furnished to do so, subject to the following conditions: 15 | * 16 | * The above copyright notice and this permission notice shall be included in 17 | * all copies or substantial portions of the Software. 18 | * 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 25 | * THE SOFTWARE. 26 | */ 27 | 28 | 29 | #ifndef Encoder_h_ 30 | #define Encoder_h_ 31 | 32 | #if defined(ARDUINO) && ARDUINO >= 100 33 | #include "Arduino.h" 34 | #elif defined(WIRING) 35 | #include "Wiring.h" 36 | #else 37 | #include "WProgram.h" 38 | #include "pins_arduino.h" 39 | #endif 40 | 41 | #include "utility/direct_pin_read.h" 42 | 43 | #if defined(ENCODER_USE_INTERRUPTS) || !defined(ENCODER_DO_NOT_USE_INTERRUPTS) 44 | #define ENCODER_USE_INTERRUPTS 45 | #define ENCODER_ARGLIST_SIZE CORE_NUM_INTERRUPT 46 | #include "utility/interrupt_pins.h" 47 | #ifdef ENCODER_OPTIMIZE_INTERRUPTS 48 | #include "utility/interrupt_config.h" 49 | #endif 50 | #else 51 | #define ENCODER_ARGLIST_SIZE 0 52 | #endif 53 | 54 | extern void wakeUpInterrupt(); 55 | 56 | // All the data needed by interrupts is consolidated into this ugly struct 57 | // to facilitate assembly language optimizing of the speed critical update. 58 | // The assembly code uses auto-incrementing addressing modes, so the struct 59 | // must remain in exactly this order. 60 | typedef struct { 61 | volatile IO_REG_TYPE * pin1_register; 62 | volatile IO_REG_TYPE * pin2_register; 63 | IO_REG_TYPE pin1_bitmask; 64 | IO_REG_TYPE pin2_bitmask; 65 | uint8_t state; 66 | int32_t position; 67 | } Encoder_internal_state_t; 68 | 69 | class Encoder 70 | { 71 | public: 72 | Encoder(uint8_t pin1, uint8_t pin2) { 73 | #ifdef INPUT_PULLUP 74 | pinMode(pin1, INPUT_PULLUP); 75 | pinMode(pin2, INPUT_PULLUP); 76 | #else 77 | pinMode(pin1, INPUT); 78 | digitalWrite(pin1, HIGH); 79 | pinMode(pin2, INPUT); 80 | digitalWrite(pin2, HIGH); 81 | #endif 82 | encoder.pin1_register = PIN_TO_BASEREG(pin1); 83 | encoder.pin1_bitmask = PIN_TO_BITMASK(pin1); 84 | encoder.pin2_register = PIN_TO_BASEREG(pin2); 85 | encoder.pin2_bitmask = PIN_TO_BITMASK(pin2); 86 | encoder.position = 0; 87 | // allow time for a passive R-C filter to charge 88 | // through the pullup resistors, before reading 89 | // the initial state 90 | delayMicroseconds(2000); 91 | uint8_t s = 0; 92 | if (DIRECT_PIN_READ(encoder.pin1_register, encoder.pin1_bitmask)) s |= 1; 93 | if (DIRECT_PIN_READ(encoder.pin2_register, encoder.pin2_bitmask)) s |= 2; 94 | encoder.state = s; 95 | #ifdef ENCODER_USE_INTERRUPTS 96 | interrupts_in_use = attach_interrupt(pin1, &encoder); 97 | interrupts_in_use += attach_interrupt(pin2, &encoder); 98 | #endif 99 | //update_finishup(); // to force linker to include the code (does not work) 100 | } 101 | 102 | 103 | #ifdef ENCODER_USE_INTERRUPTS 104 | inline int32_t read() { 105 | if (interrupts_in_use < 2) { 106 | noInterrupts(); 107 | update(&encoder); 108 | } else { 109 | noInterrupts(); 110 | } 111 | int32_t ret = encoder.position; 112 | interrupts(); 113 | return ret; 114 | } 115 | inline int32_t readAndReset() { 116 | if (interrupts_in_use < 2) { 117 | noInterrupts(); 118 | update(&encoder); 119 | } else { 120 | noInterrupts(); 121 | } 122 | int32_t ret = encoder.position; 123 | encoder.position = 0; 124 | interrupts(); 125 | return ret; 126 | } 127 | inline void write(int32_t p) { 128 | noInterrupts(); 129 | encoder.position = p; 130 | interrupts(); 131 | } 132 | #else 133 | inline int32_t read() { 134 | update(&encoder); 135 | return encoder.position; 136 | } 137 | inline int32_t readAndReset() { 138 | update(&encoder); 139 | int32_t ret = encoder.position; 140 | encoder.position = 0; 141 | return ret; 142 | } 143 | inline void write(int32_t p) { 144 | encoder.position = p; 145 | } 146 | #endif 147 | private: 148 | Encoder_internal_state_t encoder; 149 | #ifdef ENCODER_USE_INTERRUPTS 150 | uint8_t interrupts_in_use; 151 | #endif 152 | public: 153 | static Encoder_internal_state_t * interruptArgs[ENCODER_ARGLIST_SIZE]; 154 | 155 | // _______ _______ 156 | // Pin1 ______| |_______| |______ Pin1 157 | // negative <--- _______ _______ __ --> positive 158 | // Pin2 __| |_______| |_______| Pin2 159 | 160 | // new new old old 161 | // pin2 pin1 pin2 pin1 Result 162 | // ---- ---- ---- ---- ------ 163 | // 0 0 0 0 no movement 164 | // 0 0 0 1 +1 165 | // 0 0 1 0 -1 166 | // 0 0 1 1 +2 (assume pin1 edges only) 167 | // 0 1 0 0 -1 168 | // 0 1 0 1 no movement 169 | // 0 1 1 0 -2 (assume pin1 edges only) 170 | // 0 1 1 1 +1 171 | // 1 0 0 0 +1 172 | // 1 0 0 1 -2 (assume pin1 edges only) 173 | // 1 0 1 0 no movement 174 | // 1 0 1 1 -1 175 | // 1 1 0 0 +2 (assume pin1 edges only) 176 | // 1 1 0 1 -1 177 | // 1 1 1 0 +1 178 | // 1 1 1 1 no movement 179 | /* 180 | // Simple, easy-to-read "documentation" version :-) 181 | // 182 | void update(void) { 183 | uint8_t s = state & 3; 184 | if (digitalRead(pin1)) s |= 4; 185 | if (digitalRead(pin2)) s |= 8; 186 | switch (s) { 187 | case 0: case 5: case 10: case 15: 188 | break; 189 | case 1: case 7: case 8: case 14: 190 | position++; break; 191 | case 2: case 4: case 11: case 13: 192 | position--; break; 193 | case 3: case 12: 194 | position += 2; break; 195 | default: 196 | position -= 2; break; 197 | } 198 | state = (s >> 2); 199 | } 200 | */ 201 | 202 | public: 203 | // update() is not meant to be called from outside Encoder, 204 | // but it is public to allow static interrupt routines. 205 | // DO NOT call update() directly from sketches. 206 | static void update(Encoder_internal_state_t *arg) { 207 | #if defined(__AVR__) 208 | // The compiler believes this is just 1 line of code, so 209 | // it will inline this function into each interrupt 210 | // handler. That's a tiny bit faster, but grows the code. 211 | // Especially when used with ENCODER_OPTIMIZE_INTERRUPTS, 212 | // the inline nature allows the ISR prologue and epilogue 213 | // to only save/restore necessary registers, for very nice 214 | // speed increase. 215 | asm volatile ( 216 | "ld r30, X+" "\n\t" 217 | "ld r31, X+" "\n\t" 218 | "ld r24, Z" "\n\t" // r24 = pin1 input 219 | "ld r30, X+" "\n\t" 220 | "ld r31, X+" "\n\t" 221 | "ld r25, Z" "\n\t" // r25 = pin2 input 222 | "ld r30, X+" "\n\t" // r30 = pin1 mask 223 | "ld r31, X+" "\n\t" // r31 = pin2 mask 224 | "ld r22, X" "\n\t" // r22 = state 225 | "andi r22, 3" "\n\t" 226 | "and r24, r30" "\n\t" 227 | "breq L%=1" "\n\t" // if (pin1) 228 | "ori r22, 4" "\n\t" // state |= 4 229 | "L%=1:" "and r25, r31" "\n\t" 230 | "breq L%=2" "\n\t" // if (pin2) 231 | "ori r22, 8" "\n\t" // state |= 8 232 | "L%=2:" "ldi r30, lo8(pm(L%=table))" "\n\t" 233 | "ldi r31, hi8(pm(L%=table))" "\n\t" 234 | "add r30, r22" "\n\t" 235 | "adc r31, __zero_reg__" "\n\t" 236 | "asr r22" "\n\t" 237 | "asr r22" "\n\t" 238 | "st X+, r22" "\n\t" // store new state 239 | "ld r22, X+" "\n\t" 240 | "ld r23, X+" "\n\t" 241 | "ld r24, X+" "\n\t" 242 | "ld r25, X+" "\n\t" 243 | "ijmp" "\n\t" // jumps to update_finishup() 244 | // TODO move this table to another static function, 245 | // so it doesn't get needlessly duplicated. Easier 246 | // said than done, due to linker issues and inlining 247 | "L%=table:" "\n\t" 248 | "rjmp L%=end" "\n\t" // 0 249 | "rjmp L%=plus1" "\n\t" // 1 250 | "rjmp L%=minus1" "\n\t" // 2 251 | "rjmp L%=plus2" "\n\t" // 3 252 | "rjmp L%=minus1" "\n\t" // 4 253 | "rjmp L%=end" "\n\t" // 5 254 | "rjmp L%=minus2" "\n\t" // 6 255 | "rjmp L%=plus1" "\n\t" // 7 256 | "rjmp L%=plus1" "\n\t" // 8 257 | "rjmp L%=minus2" "\n\t" // 9 258 | "rjmp L%=end" "\n\t" // 10 259 | "rjmp L%=minus1" "\n\t" // 11 260 | "rjmp L%=plus2" "\n\t" // 12 261 | "rjmp L%=minus1" "\n\t" // 13 262 | "rjmp L%=plus1" "\n\t" // 14 263 | "rjmp L%=end" "\n\t" // 15 264 | "L%=minus2:" "\n\t" 265 | "subi r22, 2" "\n\t" 266 | "sbci r23, 0" "\n\t" 267 | "sbci r24, 0" "\n\t" 268 | "sbci r25, 0" "\n\t" 269 | "rjmp L%=store" "\n\t" 270 | "L%=minus1:" "\n\t" 271 | "subi r22, 1" "\n\t" 272 | "sbci r23, 0" "\n\t" 273 | "sbci r24, 0" "\n\t" 274 | "sbci r25, 0" "\n\t" 275 | "rjmp L%=store" "\n\t" 276 | "L%=plus2:" "\n\t" 277 | "subi r22, 254" "\n\t" 278 | "rjmp L%=z" "\n\t" 279 | "L%=plus1:" "\n\t" 280 | "subi r22, 255" "\n\t" 281 | "L%=z:" "sbci r23, 255" "\n\t" 282 | "sbci r24, 255" "\n\t" 283 | "sbci r25, 255" "\n\t" 284 | "L%=store:" "\n\t" 285 | "st -X, r25" "\n\t" 286 | "st -X, r24" "\n\t" 287 | "st -X, r23" "\n\t" 288 | "st -X, r22" "\n\t" 289 | "L%=end:" "\n" 290 | : : "x" (arg) : "r22", "r23", "r24", "r25", "r30", "r31"); 291 | #else 292 | uint8_t p1val = DIRECT_PIN_READ(arg->pin1_register, arg->pin1_bitmask); 293 | uint8_t p2val = DIRECT_PIN_READ(arg->pin2_register, arg->pin2_bitmask); 294 | uint8_t state = arg->state & 3; 295 | if (p1val) state |= 4; 296 | if (p2val) state |= 8; 297 | arg->state = (state >> 2); 298 | switch (state) { 299 | case 1: case 7: case 8: case 14: 300 | arg->position++; 301 | return; 302 | case 2: case 4: case 11: case 13: 303 | arg->position--; 304 | return; 305 | case 3: case 12: 306 | arg->position += 2; 307 | return; 308 | case 6: case 9: 309 | arg->position -= 2; 310 | return; 311 | } 312 | #endif 313 | 314 | wakeUpInterrupt(); 315 | } 316 | private: 317 | /* 318 | #if defined(__AVR__) 319 | // TODO: this must be a no inline function 320 | // even noinline does not seem to solve difficult 321 | // problems with this. Oh well, it was only meant 322 | // to shrink code size - there's no performance 323 | // improvement in this, only code size reduction. 324 | __attribute__((noinline)) void update_finishup(void) { 325 | asm volatile ( 326 | "ldi r30, lo8(pm(Ltable))" "\n\t" 327 | "ldi r31, hi8(pm(Ltable))" "\n\t" 328 | "Ltable:" "\n\t" 329 | "rjmp L%=end" "\n\t" // 0 330 | "rjmp L%=plus1" "\n\t" // 1 331 | "rjmp L%=minus1" "\n\t" // 2 332 | "rjmp L%=plus2" "\n\t" // 3 333 | "rjmp L%=minus1" "\n\t" // 4 334 | "rjmp L%=end" "\n\t" // 5 335 | "rjmp L%=minus2" "\n\t" // 6 336 | "rjmp L%=plus1" "\n\t" // 7 337 | "rjmp L%=plus1" "\n\t" // 8 338 | "rjmp L%=minus2" "\n\t" // 9 339 | "rjmp L%=end" "\n\t" // 10 340 | "rjmp L%=minus1" "\n\t" // 11 341 | "rjmp L%=plus2" "\n\t" // 12 342 | "rjmp L%=minus1" "\n\t" // 13 343 | "rjmp L%=plus1" "\n\t" // 14 344 | "rjmp L%=end" "\n\t" // 15 345 | "L%=minus2:" "\n\t" 346 | "subi r22, 2" "\n\t" 347 | "sbci r23, 0" "\n\t" 348 | "sbci r24, 0" "\n\t" 349 | "sbci r25, 0" "\n\t" 350 | "rjmp L%=store" "\n\t" 351 | "L%=minus1:" "\n\t" 352 | "subi r22, 1" "\n\t" 353 | "sbci r23, 0" "\n\t" 354 | "sbci r24, 0" "\n\t" 355 | "sbci r25, 0" "\n\t" 356 | "rjmp L%=store" "\n\t" 357 | "L%=plus2:" "\n\t" 358 | "subi r22, 254" "\n\t" 359 | "rjmp L%=z" "\n\t" 360 | "L%=plus1:" "\n\t" 361 | "subi r22, 255" "\n\t" 362 | "L%=z:" "sbci r23, 255" "\n\t" 363 | "sbci r24, 255" "\n\t" 364 | "sbci r25, 255" "\n\t" 365 | "L%=store:" "\n\t" 366 | "st -X, r25" "\n\t" 367 | "st -X, r24" "\n\t" 368 | "st -X, r23" "\n\t" 369 | "st -X, r22" "\n\t" 370 | "L%=end:" "\n" 371 | : : : "r22", "r23", "r24", "r25", "r30", "r31"); 372 | } 373 | #endif 374 | */ 375 | 376 | 377 | #ifdef ENCODER_USE_INTERRUPTS 378 | // this giant function is an unfortunate consequence of Arduino's 379 | // attachInterrupt function not supporting any way to pass a pointer 380 | // or other context to the attached function. 381 | static uint8_t attach_interrupt(uint8_t pin, Encoder_internal_state_t *state) { 382 | switch (pin) { 383 | #ifdef CORE_INT0_PIN 384 | case CORE_INT0_PIN: 385 | interruptArgs[0] = state; 386 | attachInterrupt(0, isr0, CHANGE); 387 | break; 388 | #endif 389 | #ifdef CORE_INT1_PIN 390 | case CORE_INT1_PIN: 391 | interruptArgs[1] = state; 392 | attachInterrupt(1, isr1, CHANGE); 393 | break; 394 | #endif 395 | #ifdef CORE_INT2_PIN 396 | case CORE_INT2_PIN: 397 | interruptArgs[2] = state; 398 | attachInterrupt(2, isr2, CHANGE); 399 | break; 400 | #endif 401 | #ifdef CORE_INT3_PIN 402 | case CORE_INT3_PIN: 403 | interruptArgs[3] = state; 404 | attachInterrupt(3, isr3, CHANGE); 405 | break; 406 | #endif 407 | #ifdef CORE_INT4_PIN 408 | case CORE_INT4_PIN: 409 | interruptArgs[4] = state; 410 | attachInterrupt(4, isr4, CHANGE); 411 | break; 412 | #endif 413 | #ifdef CORE_INT5_PIN 414 | case CORE_INT5_PIN: 415 | interruptArgs[5] = state; 416 | attachInterrupt(5, isr5, CHANGE); 417 | break; 418 | #endif 419 | #ifdef CORE_INT6_PIN 420 | case CORE_INT6_PIN: 421 | interruptArgs[6] = state; 422 | attachInterrupt(6, isr6, CHANGE); 423 | break; 424 | #endif 425 | #ifdef CORE_INT7_PIN 426 | case CORE_INT7_PIN: 427 | interruptArgs[7] = state; 428 | attachInterrupt(7, isr7, CHANGE); 429 | break; 430 | #endif 431 | #ifdef CORE_INT8_PIN 432 | case CORE_INT8_PIN: 433 | interruptArgs[8] = state; 434 | attachInterrupt(8, isr8, CHANGE); 435 | break; 436 | #endif 437 | #ifdef CORE_INT9_PIN 438 | case CORE_INT9_PIN: 439 | interruptArgs[9] = state; 440 | attachInterrupt(9, isr9, CHANGE); 441 | break; 442 | #endif 443 | #ifdef CORE_INT10_PIN 444 | case CORE_INT10_PIN: 445 | interruptArgs[10] = state; 446 | attachInterrupt(10, isr10, CHANGE); 447 | break; 448 | #endif 449 | #ifdef CORE_INT11_PIN 450 | case CORE_INT11_PIN: 451 | interruptArgs[11] = state; 452 | attachInterrupt(11, isr11, CHANGE); 453 | break; 454 | #endif 455 | #ifdef CORE_INT12_PIN 456 | case CORE_INT12_PIN: 457 | interruptArgs[12] = state; 458 | attachInterrupt(12, isr12, CHANGE); 459 | break; 460 | #endif 461 | #ifdef CORE_INT13_PIN 462 | case CORE_INT13_PIN: 463 | interruptArgs[13] = state; 464 | attachInterrupt(13, isr13, CHANGE); 465 | break; 466 | #endif 467 | #ifdef CORE_INT14_PIN 468 | case CORE_INT14_PIN: 469 | interruptArgs[14] = state; 470 | attachInterrupt(14, isr14, CHANGE); 471 | break; 472 | #endif 473 | #ifdef CORE_INT15_PIN 474 | case CORE_INT15_PIN: 475 | interruptArgs[15] = state; 476 | attachInterrupt(15, isr15, CHANGE); 477 | break; 478 | #endif 479 | #ifdef CORE_INT16_PIN 480 | case CORE_INT16_PIN: 481 | interruptArgs[16] = state; 482 | attachInterrupt(16, isr16, CHANGE); 483 | break; 484 | #endif 485 | #ifdef CORE_INT17_PIN 486 | case CORE_INT17_PIN: 487 | interruptArgs[17] = state; 488 | attachInterrupt(17, isr17, CHANGE); 489 | break; 490 | #endif 491 | #ifdef CORE_INT18_PIN 492 | case CORE_INT18_PIN: 493 | interruptArgs[18] = state; 494 | attachInterrupt(18, isr18, CHANGE); 495 | break; 496 | #endif 497 | #ifdef CORE_INT19_PIN 498 | case CORE_INT19_PIN: 499 | interruptArgs[19] = state; 500 | attachInterrupt(19, isr19, CHANGE); 501 | break; 502 | #endif 503 | #ifdef CORE_INT20_PIN 504 | case CORE_INT20_PIN: 505 | interruptArgs[20] = state; 506 | attachInterrupt(20, isr20, CHANGE); 507 | break; 508 | #endif 509 | #ifdef CORE_INT21_PIN 510 | case CORE_INT21_PIN: 511 | interruptArgs[21] = state; 512 | attachInterrupt(21, isr21, CHANGE); 513 | break; 514 | #endif 515 | #ifdef CORE_INT22_PIN 516 | case CORE_INT22_PIN: 517 | interruptArgs[22] = state; 518 | attachInterrupt(22, isr22, CHANGE); 519 | break; 520 | #endif 521 | #ifdef CORE_INT23_PIN 522 | case CORE_INT23_PIN: 523 | interruptArgs[23] = state; 524 | attachInterrupt(23, isr23, CHANGE); 525 | break; 526 | #endif 527 | #ifdef CORE_INT24_PIN 528 | case CORE_INT24_PIN: 529 | interruptArgs[24] = state; 530 | attachInterrupt(24, isr24, CHANGE); 531 | break; 532 | #endif 533 | #ifdef CORE_INT25_PIN 534 | case CORE_INT25_PIN: 535 | interruptArgs[25] = state; 536 | attachInterrupt(25, isr25, CHANGE); 537 | break; 538 | #endif 539 | #ifdef CORE_INT26_PIN 540 | case CORE_INT26_PIN: 541 | interruptArgs[26] = state; 542 | attachInterrupt(26, isr26, CHANGE); 543 | break; 544 | #endif 545 | #ifdef CORE_INT27_PIN 546 | case CORE_INT27_PIN: 547 | interruptArgs[27] = state; 548 | attachInterrupt(27, isr27, CHANGE); 549 | break; 550 | #endif 551 | #ifdef CORE_INT28_PIN 552 | case CORE_INT28_PIN: 553 | interruptArgs[28] = state; 554 | attachInterrupt(28, isr28, CHANGE); 555 | break; 556 | #endif 557 | #ifdef CORE_INT29_PIN 558 | case CORE_INT29_PIN: 559 | interruptArgs[29] = state; 560 | attachInterrupt(29, isr29, CHANGE); 561 | break; 562 | #endif 563 | 564 | #ifdef CORE_INT30_PIN 565 | case CORE_INT30_PIN: 566 | interruptArgs[30] = state; 567 | attachInterrupt(30, isr30, CHANGE); 568 | break; 569 | #endif 570 | #ifdef CORE_INT31_PIN 571 | case CORE_INT31_PIN: 572 | interruptArgs[31] = state; 573 | attachInterrupt(31, isr31, CHANGE); 574 | break; 575 | #endif 576 | #ifdef CORE_INT32_PIN 577 | case CORE_INT32_PIN: 578 | interruptArgs[32] = state; 579 | attachInterrupt(32, isr32, CHANGE); 580 | break; 581 | #endif 582 | #ifdef CORE_INT33_PIN 583 | case CORE_INT33_PIN: 584 | interruptArgs[33] = state; 585 | attachInterrupt(33, isr33, CHANGE); 586 | break; 587 | #endif 588 | #ifdef CORE_INT34_PIN 589 | case CORE_INT34_PIN: 590 | interruptArgs[34] = state; 591 | attachInterrupt(34, isr34, CHANGE); 592 | break; 593 | #endif 594 | #ifdef CORE_INT35_PIN 595 | case CORE_INT35_PIN: 596 | interruptArgs[35] = state; 597 | attachInterrupt(35, isr35, CHANGE); 598 | break; 599 | #endif 600 | #ifdef CORE_INT36_PIN 601 | case CORE_INT36_PIN: 602 | interruptArgs[36] = state; 603 | attachInterrupt(36, isr36, CHANGE); 604 | break; 605 | #endif 606 | #ifdef CORE_INT37_PIN 607 | case CORE_INT37_PIN: 608 | interruptArgs[37] = state; 609 | attachInterrupt(37, isr37, CHANGE); 610 | break; 611 | #endif 612 | #ifdef CORE_INT38_PIN 613 | case CORE_INT38_PIN: 614 | interruptArgs[38] = state; 615 | attachInterrupt(38, isr38, CHANGE); 616 | break; 617 | #endif 618 | #ifdef CORE_INT39_PIN 619 | case CORE_INT39_PIN: 620 | interruptArgs[39] = state; 621 | attachInterrupt(39, isr39, CHANGE); 622 | break; 623 | #endif 624 | #ifdef CORE_INT40_PIN 625 | case CORE_INT40_PIN: 626 | interruptArgs[40] = state; 627 | attachInterrupt(40, isr40, CHANGE); 628 | break; 629 | #endif 630 | #ifdef CORE_INT41_PIN 631 | case CORE_INT41_PIN: 632 | interruptArgs[41] = state; 633 | attachInterrupt(41, isr41, CHANGE); 634 | break; 635 | #endif 636 | #ifdef CORE_INT42_PIN 637 | case CORE_INT42_PIN: 638 | interruptArgs[42] = state; 639 | attachInterrupt(42, isr42, CHANGE); 640 | break; 641 | #endif 642 | #ifdef CORE_INT43_PIN 643 | case CORE_INT43_PIN: 644 | interruptArgs[43] = state; 645 | attachInterrupt(43, isr43, CHANGE); 646 | break; 647 | #endif 648 | #ifdef CORE_INT44_PIN 649 | case CORE_INT44_PIN: 650 | interruptArgs[44] = state; 651 | attachInterrupt(44, isr44, CHANGE); 652 | break; 653 | #endif 654 | #ifdef CORE_INT45_PIN 655 | case CORE_INT45_PIN: 656 | interruptArgs[45] = state; 657 | attachInterrupt(45, isr45, CHANGE); 658 | break; 659 | #endif 660 | #ifdef CORE_INT46_PIN 661 | case CORE_INT46_PIN: 662 | interruptArgs[46] = state; 663 | attachInterrupt(46, isr46, CHANGE); 664 | break; 665 | #endif 666 | #ifdef CORE_INT47_PIN 667 | case CORE_INT47_PIN: 668 | interruptArgs[47] = state; 669 | attachInterrupt(47, isr47, CHANGE); 670 | break; 671 | #endif 672 | #ifdef CORE_INT48_PIN 673 | case CORE_INT48_PIN: 674 | interruptArgs[48] = state; 675 | attachInterrupt(48, isr48, CHANGE); 676 | break; 677 | #endif 678 | #ifdef CORE_INT49_PIN 679 | case CORE_INT49_PIN: 680 | interruptArgs[49] = state; 681 | attachInterrupt(49, isr49, CHANGE); 682 | break; 683 | #endif 684 | #ifdef CORE_INT50_PIN 685 | case CORE_INT50_PIN: 686 | interruptArgs[50] = state; 687 | attachInterrupt(50, isr50, CHANGE); 688 | break; 689 | #endif 690 | #ifdef CORE_INT51_PIN 691 | case CORE_INT51_PIN: 692 | interruptArgs[51] = state; 693 | attachInterrupt(51, isr51, CHANGE); 694 | break; 695 | #endif 696 | #ifdef CORE_INT52_PIN 697 | case CORE_INT52_PIN: 698 | interruptArgs[52] = state; 699 | attachInterrupt(52, isr52, CHANGE); 700 | break; 701 | #endif 702 | #ifdef CORE_INT53_PIN 703 | case CORE_INT53_PIN: 704 | interruptArgs[53] = state; 705 | attachInterrupt(53, isr53, CHANGE); 706 | break; 707 | #endif 708 | #ifdef CORE_INT54_PIN 709 | case CORE_INT54_PIN: 710 | interruptArgs[54] = state; 711 | attachInterrupt(54, isr54, CHANGE); 712 | break; 713 | #endif 714 | #ifdef CORE_INT55_PIN 715 | case CORE_INT55_PIN: 716 | interruptArgs[55] = state; 717 | attachInterrupt(55, isr55, CHANGE); 718 | break; 719 | #endif 720 | #ifdef CORE_INT56_PIN 721 | case CORE_INT56_PIN: 722 | interruptArgs[56] = state; 723 | attachInterrupt(56, isr56, CHANGE); 724 | break; 725 | #endif 726 | #ifdef CORE_INT57_PIN 727 | case CORE_INT57_PIN: 728 | interruptArgs[57] = state; 729 | attachInterrupt(57, isr57, CHANGE); 730 | break; 731 | #endif 732 | #ifdef CORE_INT58_PIN 733 | case CORE_INT58_PIN: 734 | interruptArgs[58] = state; 735 | attachInterrupt(58, isr58, CHANGE); 736 | break; 737 | #endif 738 | #ifdef CORE_INT59_PIN 739 | case CORE_INT59_PIN: 740 | interruptArgs[59] = state; 741 | attachInterrupt(59, isr59, CHANGE); 742 | break; 743 | #endif 744 | default: 745 | return 0; 746 | } 747 | return 1; 748 | } 749 | #endif // ENCODER_USE_INTERRUPTS 750 | 751 | 752 | #if defined(ENCODER_USE_INTERRUPTS) && !defined(ENCODER_OPTIMIZE_INTERRUPTS) 753 | #ifdef CORE_INT0_PIN 754 | static void isr0(void) { update(interruptArgs[0]); } 755 | #endif 756 | #ifdef CORE_INT1_PIN 757 | static void isr1(void) { update(interruptArgs[1]); } 758 | #endif 759 | #ifdef CORE_INT2_PIN 760 | static void isr2(void) { update(interruptArgs[2]); } 761 | #endif 762 | #ifdef CORE_INT3_PIN 763 | static void isr3(void) { update(interruptArgs[3]); } 764 | #endif 765 | #ifdef CORE_INT4_PIN 766 | static void isr4(void) { update(interruptArgs[4]); } 767 | #endif 768 | #ifdef CORE_INT5_PIN 769 | static void isr5(void) { update(interruptArgs[5]); } 770 | #endif 771 | #ifdef CORE_INT6_PIN 772 | static void isr6(void) { update(interruptArgs[6]); } 773 | #endif 774 | #ifdef CORE_INT7_PIN 775 | static void isr7(void) { update(interruptArgs[7]); } 776 | #endif 777 | #ifdef CORE_INT8_PIN 778 | static void isr8(void) { update(interruptArgs[8]); } 779 | #endif 780 | #ifdef CORE_INT9_PIN 781 | static void isr9(void) { update(interruptArgs[9]); } 782 | #endif 783 | #ifdef CORE_INT10_PIN 784 | static void isr10(void) { update(interruptArgs[10]); } 785 | #endif 786 | #ifdef CORE_INT11_PIN 787 | static void isr11(void) { update(interruptArgs[11]); } 788 | #endif 789 | #ifdef CORE_INT12_PIN 790 | static void isr12(void) { update(interruptArgs[12]); } 791 | #endif 792 | #ifdef CORE_INT13_PIN 793 | static void isr13(void) { update(interruptArgs[13]); } 794 | #endif 795 | #ifdef CORE_INT14_PIN 796 | static void isr14(void) { update(interruptArgs[14]); } 797 | #endif 798 | #ifdef CORE_INT15_PIN 799 | static void isr15(void) { update(interruptArgs[15]); } 800 | #endif 801 | #ifdef CORE_INT16_PIN 802 | static void isr16(void) { update(interruptArgs[16]); } 803 | #endif 804 | #ifdef CORE_INT17_PIN 805 | static void isr17(void) { update(interruptArgs[17]); } 806 | #endif 807 | #ifdef CORE_INT18_PIN 808 | static void isr18(void) { update(interruptArgs[18]); } 809 | #endif 810 | #ifdef CORE_INT19_PIN 811 | static void isr19(void) { update(interruptArgs[19]); } 812 | #endif 813 | #ifdef CORE_INT20_PIN 814 | static void isr20(void) { update(interruptArgs[20]); } 815 | #endif 816 | #ifdef CORE_INT21_PIN 817 | static void isr21(void) { update(interruptArgs[21]); } 818 | #endif 819 | #ifdef CORE_INT22_PIN 820 | static void isr22(void) { update(interruptArgs[22]); } 821 | #endif 822 | #ifdef CORE_INT23_PIN 823 | static void isr23(void) { update(interruptArgs[23]); } 824 | #endif 825 | #ifdef CORE_INT24_PIN 826 | static void isr24(void) { update(interruptArgs[24]); } 827 | #endif 828 | #ifdef CORE_INT25_PIN 829 | static void isr25(void) { update(interruptArgs[25]); } 830 | #endif 831 | #ifdef CORE_INT26_PIN 832 | static void isr26(void) { update(interruptArgs[26]); } 833 | #endif 834 | #ifdef CORE_INT27_PIN 835 | static void isr27(void) { update(interruptArgs[27]); } 836 | #endif 837 | #ifdef CORE_INT28_PIN 838 | static void isr28(void) { update(interruptArgs[28]); } 839 | #endif 840 | #ifdef CORE_INT29_PIN 841 | static void isr29(void) { update(interruptArgs[29]); } 842 | #endif 843 | #ifdef CORE_INT30_PIN 844 | static void isr30(void) { update(interruptArgs[30]); } 845 | #endif 846 | #ifdef CORE_INT31_PIN 847 | static void isr31(void) { update(interruptArgs[31]); } 848 | #endif 849 | #ifdef CORE_INT32_PIN 850 | static void isr32(void) { update(interruptArgs[32]); } 851 | #endif 852 | #ifdef CORE_INT33_PIN 853 | static void isr33(void) { update(interruptArgs[33]); } 854 | #endif 855 | #ifdef CORE_INT34_PIN 856 | static void isr34(void) { update(interruptArgs[34]); } 857 | #endif 858 | #ifdef CORE_INT35_PIN 859 | static void isr35(void) { update(interruptArgs[35]); } 860 | #endif 861 | #ifdef CORE_INT36_PIN 862 | static void isr36(void) { update(interruptArgs[36]); } 863 | #endif 864 | #ifdef CORE_INT37_PIN 865 | static void isr37(void) { update(interruptArgs[37]); } 866 | #endif 867 | #ifdef CORE_INT38_PIN 868 | static void isr38(void) { update(interruptArgs[38]); } 869 | #endif 870 | #ifdef CORE_INT39_PIN 871 | static void isr39(void) { update(interruptArgs[39]); } 872 | #endif 873 | #ifdef CORE_INT40_PIN 874 | static void isr40(void) { update(interruptArgs[40]); } 875 | #endif 876 | #ifdef CORE_INT41_PIN 877 | static void isr41(void) { update(interruptArgs[41]); } 878 | #endif 879 | #ifdef CORE_INT42_PIN 880 | static void isr42(void) { update(interruptArgs[42]); } 881 | #endif 882 | #ifdef CORE_INT43_PIN 883 | static void isr43(void) { update(interruptArgs[43]); } 884 | #endif 885 | #ifdef CORE_INT44_PIN 886 | static void isr44(void) { update(interruptArgs[44]); } 887 | #endif 888 | #ifdef CORE_INT45_PIN 889 | static void isr45(void) { update(interruptArgs[45]); } 890 | #endif 891 | #ifdef CORE_INT46_PIN 892 | static void isr46(void) { update(interruptArgs[46]); } 893 | #endif 894 | #ifdef CORE_INT47_PIN 895 | static void isr47(void) { update(interruptArgs[47]); } 896 | #endif 897 | #ifdef CORE_INT48_PIN 898 | static void isr48(void) { update(interruptArgs[48]); } 899 | #endif 900 | #ifdef CORE_INT49_PIN 901 | static void isr49(void) { update(interruptArgs[49]); } 902 | #endif 903 | #ifdef CORE_INT50_PIN 904 | static void isr50(void) { update(interruptArgs[50]); } 905 | #endif 906 | #ifdef CORE_INT51_PIN 907 | static void isr51(void) { update(interruptArgs[51]); } 908 | #endif 909 | #ifdef CORE_INT52_PIN 910 | static void isr52(void) { update(interruptArgs[52]); } 911 | #endif 912 | #ifdef CORE_INT53_PIN 913 | static void isr53(void) { update(interruptArgs[53]); } 914 | #endif 915 | #ifdef CORE_INT54_PIN 916 | static void isr54(void) { update(interruptArgs[54]); } 917 | #endif 918 | #ifdef CORE_INT55_PIN 919 | static void isr55(void) { update(interruptArgs[55]); } 920 | #endif 921 | #ifdef CORE_INT56_PIN 922 | static void isr56(void) { update(interruptArgs[56]); } 923 | #endif 924 | #ifdef CORE_INT57_PIN 925 | static void isr57(void) { update(interruptArgs[57]); } 926 | #endif 927 | #ifdef CORE_INT58_PIN 928 | static void isr58(void) { update(interruptArgs[58]); } 929 | #endif 930 | #ifdef CORE_INT59_PIN 931 | static void isr59(void) { update(interruptArgs[59]); } 932 | #endif 933 | #endif 934 | }; 935 | 936 | #if defined(ENCODER_USE_INTERRUPTS) && defined(ENCODER_OPTIMIZE_INTERRUPTS) 937 | #if defined(__AVR__) 938 | #if defined(INT0_vect) && CORE_NUM_INTERRUPT > 0 939 | ISR(INT0_vect) { Encoder::update(Encoder::interruptArgs[SCRAMBLE_INT_ORDER(0)]); } 940 | #endif 941 | #if defined(INT1_vect) && CORE_NUM_INTERRUPT > 1 942 | ISR(INT1_vect) { Encoder::update(Encoder::interruptArgs[SCRAMBLE_INT_ORDER(1)]); } 943 | #endif 944 | #if defined(INT2_vect) && CORE_NUM_INTERRUPT > 2 945 | ISR(INT2_vect) { Encoder::update(Encoder::interruptArgs[SCRAMBLE_INT_ORDER(2)]); } 946 | #endif 947 | #if defined(INT3_vect) && CORE_NUM_INTERRUPT > 3 948 | ISR(INT3_vect) { Encoder::update(Encoder::interruptArgs[SCRAMBLE_INT_ORDER(3)]); } 949 | #endif 950 | #if defined(INT4_vect) && CORE_NUM_INTERRUPT > 4 951 | ISR(INT4_vect) { Encoder::update(Encoder::interruptArgs[SCRAMBLE_INT_ORDER(4)]); } 952 | #endif 953 | #if defined(INT5_vect) && CORE_NUM_INTERRUPT > 5 954 | ISR(INT5_vect) { Encoder::update(Encoder::interruptArgs[SCRAMBLE_INT_ORDER(5)]); } 955 | #endif 956 | #if defined(INT6_vect) && CORE_NUM_INTERRUPT > 6 957 | ISR(INT6_vect) { Encoder::update(Encoder::interruptArgs[SCRAMBLE_INT_ORDER(6)]); } 958 | #endif 959 | #if defined(INT7_vect) && CORE_NUM_INTERRUPT > 7 960 | ISR(INT7_vect) { Encoder::update(Encoder::interruptArgs[SCRAMBLE_INT_ORDER(7)]); } 961 | #endif 962 | #endif // AVR 963 | #if defined(attachInterrupt) 964 | // Don't intefere with other libraries or sketch use of attachInterrupt() 965 | // https://github.com/PaulStoffregen/Encoder/issues/8 966 | #undef attachInterrupt 967 | #endif 968 | #endif // ENCODER_OPTIMIZE_INTERRUPTS 969 | 970 | 971 | #endif 972 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # kitchen timer 2 | ![a kitchen timer, sorry about the dust](assets/portrait.jpg?raw=true) 3 | The summer of 2021 I finally got fed up with my crappy ikea kitchen timer and decided to build my own. 4 | 5 | You can find a [build log in this Twitter thread](https://twitter.com/grapefrukt/status/1417568865563852805). 6 | 7 | Here's the bill of materials I ended up with: 8 | [Teensy 2.0](https://www.adafruit.com/product/199) 9 | 10 | [Adafruit 16x9 Charlieplexed PWM LED Matrix Driver](https://www.adafruit.com/product/2946) 11 | [LED Charlieplexed Matrix - 9x16 LEDs - Warm White](https://www.adafruit.com/product/3162) 12 | 13 | [Rotary Encoder](https://www.adafruit.com/product/377) 14 | [Solid Machined Metal Knob](https://www.adafruit.com/product/2056) 15 | 16 | [PowerBoost 500 Charger](https://www.adafruit.com/product/1944) 17 | [Lithium Ion Polymer Battery - 3.7v 2500mAh](https://www.adafruit.com/product/328) 18 | 19 | [Mini Magnet Feet for RGB LED Matrices](https://www.adafruit.com/product/4631) 20 | [Piezo Buzzer](https://www.adafruit.com/product/160) 21 | 22 | I supplemented some additional fasteners to screw into the mini magnet feet from the front clamping the case layers together. 23 | 24 | The [schematic](assets/fritzing-schematic.png) is available in the assets folder, as is the [templates I used to laser cut the enclosure](assets/lasercut-case.svg) from 3mm acrylic. 25 | -------------------------------------------------------------------------------- /assets/fritzing-schematic.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/grapefrukt/kitchen-timer/8cb4c175b95e927e8ebd3dc3c08422d04085e8bb/assets/fritzing-schematic.png -------------------------------------------------------------------------------- /assets/lasercut-case.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | 9 | 17 | 23 | 31 | 39 | 46 | 53 | 61 | 68 | 69 | -------------------------------------------------------------------------------- /assets/portrait.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/grapefrukt/kitchen-timer/8cb4c175b95e927e8ebd3dc3c08422d04085e8bb/assets/portrait.jpg -------------------------------------------------------------------------------- /kitchen-timer.ino: -------------------------------------------------------------------------------- 1 | #include "Encoder.h" 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include "kitchen_font.h" 8 | #include "pitches.h" 9 | #include "toneAC.h" 10 | #include "music.h" 11 | #include "music_data.h" 12 | 13 | #define PIN_ENCODER_A 7 14 | #define PIN_ENCODER_B 8 15 | #define PIN_ENCODER_BUTTON 16 16 | #define PIN_SLEEP_LED 6 17 | #define PIN_SLEEP_SCREEN 9 18 | 19 | // #define USE_SLEEP_LED 20 | 21 | // useful to make the timer run faster 22 | #define MS_IN_A_SECOND 1000 23 | 24 | #define BRIGHTNESS 16 25 | #define BRIGHTNESS_HALF 8 26 | #define BRIGHTNESS_EXTRA 32 27 | 28 | // how long to sound the alarm, after this, we just give up and go back to sleep 29 | #define ALARM_DURATION_SECONDS 60 30 | 31 | // any remaining ticks on the rotary will be cleared after this long (ms) 32 | #define CLEAR_TICKS_AFTER_MS 750 33 | 34 | // how long to wait after user input stops before starting the timer 35 | #define WAIT_AFTER_INPUT_MS 1000 36 | // ignore user inputs for a bit after turning of the alarm as to not set a new time immediately 37 | #define IGNORE_TIMER_SET_AFTER_ALARM_MS 1000 38 | // how long to wait in the view total state 39 | #define WAIT_WHILE_VIEW_TOTAL_MS 2000 40 | // how long to wait without user input before going to sleep 41 | const float SLEEP_AFTER_MS = 5000; 42 | // how long before the display starts to fade, hitting 0 as we go to sleep 43 | const float FADE_AFTER_MS = 3000; 44 | const float FADE_DURATION_MS = SLEEP_AFTER_MS - FADE_AFTER_MS; 45 | 46 | 47 | enum State { 48 | IDLE, 49 | SET_TIMER, 50 | TIMER, 51 | ALARM, 52 | ALARM_OFF_COOLDOWN, 53 | VIEW_TOTAL, 54 | MUSIC, 55 | }; 56 | 57 | State state = IDLE; 58 | elapsedMillis timeInState; 59 | 60 | Adafruit_IS31FL3731 matrix = Adafruit_IS31FL3731(); 61 | Encoder rotaryEncoder = Encoder(PIN_ENCODER_A, PIN_ENCODER_B); 62 | Bounce pushbutton = Bounce(PIN_ENCODER_BUTTON, 10); // 10 ms debounce 63 | 64 | bool buttonDown = false; 65 | 66 | elapsedMillis timerSeconds; 67 | elapsedMillis timeSinceInput; 68 | elapsedMillis timeSinceTick; 69 | elapsedMillis timeButtonHold; 70 | 71 | // the main time keeping variable 72 | int time = 0; 73 | // this stores the total time counted for this "session" 74 | int timeTotal = 0; 75 | 76 | // this is used for the double buffering of the display, swaps between 0/1 each update 77 | bool bufferSwapper = true; 78 | 79 | // keep track of where the encoder was last update 80 | long encoderPosition = 0; 81 | 82 | long lastFrame = 0; 83 | 84 | // this is set by the wake up interrupt, if this is 1 we run the wake up code the next loop() 85 | volatile bool inWakeUp = false; 86 | volatile bool inSleep = false; 87 | 88 | void setup() { 89 | // set all pins as outputs to save power (this is a teensy 2.0 thing) 90 | for (int i=0; i < 46; i++) { 91 | if (i == PIN_ENCODER_A) continue; 92 | if (i == PIN_ENCODER_B) continue; 93 | if (i == PIN_ENCODER_BUTTON) continue; 94 | if (i == PIN_SLEEP_SCREEN) continue; 95 | pinMode(i, OUTPUT); 96 | } 97 | 98 | // set up the pullup for the encoder push-button 99 | pinMode(PIN_ENCODER_BUTTON, INPUT_PULLUP); 100 | 101 | // initialize the screen 102 | if (!matrix.begin()) Serial.println("display not found"); 103 | 104 | wakeUp(); 105 | } 106 | 107 | void wakeUp(){ 108 | inWakeUp = false; 109 | 110 | #ifdef USE_SLEEP_LED 111 | digitalWrite(PIN_SLEEP_LED, LOW); // set the LED off 112 | #endif 113 | 114 | // wake up the screen 115 | digitalWrite(PIN_SLEEP_SCREEN, HIGH); 116 | 117 | matrix.setTextSize(1); 118 | matrix.setTextWrap(false); // we dont want text to wrap so it scrolls nicely 119 | matrix.setTextColor(BRIGHTNESS); 120 | matrix.setFont(&kitchen_font); 121 | swapBuffers(); 122 | 123 | timeSinceInput = 0; 124 | setState(IDLE); 125 | refreshScreen(); 126 | } 127 | 128 | void swapBuffers(){ 129 | // this needs to be double buffered because the matrix refreshes so quickly it will flicker 130 | 131 | // we display what was the back buffer 132 | matrix.displayFrame(bufferSwapper ? 0 : 1); 133 | 134 | // swap them around 135 | bufferSwapper = !bufferSwapper; 136 | 137 | // and now we set what was the front buffer to be the target for any updates 138 | matrix.setFrame(bufferSwapper ? 0 : 1); 139 | matrix.clear(); 140 | matrix.setCursor(0, 7); 141 | matrix.setTextColor(BRIGHTNESS); 142 | } 143 | 144 | int ticks = 0; 145 | void readRotaryEncoder(){ 146 | int delta = -rotaryEncoder.readAndReset(); 147 | if (delta == 0) return; 148 | 149 | // because one "step" on the encoder is four ticks, it will sometimes become offset by a tick or two 150 | // this will manifest as the timer jumping in a strange fashion. this has gotten worse as the encoder has 151 | // been getting worn out, the way i fix this is to reset the tick counter if there's been no input for a short while 152 | // this assumes that if the encoder is still, it's in one of the detents 153 | if (timeSinceTick > CLEAR_TICKS_AFTER_MS && ticks != 0) ticks = 0; 154 | 155 | 156 | timeSinceTick = 0; 157 | ticks += delta; 158 | 159 | // one "step" is four ticks, so we only move if we're four+ ticks past our last position 160 | if (ticks < 4 && ticks > -4) return; 161 | 162 | int direction = delta > 0 ? 1 : -1; 163 | encoderPosition += direction; 164 | 165 | // finally, call the onRotary function to tell it we've moved 166 | onRotary(direction, encoderPosition >> 2); 167 | ticks = 0; 168 | } 169 | 170 | void onRotary(int delta, int position){ 171 | timeSinceInput = 0; 172 | 173 | // if the alarm is going off, the knob won't change anything 174 | if (state == ALARM){ 175 | dismissAlarm(); 176 | return; 177 | } 178 | 179 | // if we recently turned off the alarm, ignore inputs for a bit 180 | if (state == ALARM_OFF_COOLDOWN) return; 181 | 182 | setState(SET_TIMER); 183 | 184 | const int volume = 5; 185 | const int duration = 5; 186 | const bool background = true; 187 | if (delta > 0) toneAC(position % 2 == 0 ? NOTE_C7 : NOTE_D7, volume, duration, background); 188 | else if (time > 0) toneAC(position % 2 == 0 ? NOTE_C7 : NOTE_B6, volume, duration, background); 189 | 190 | // if the counter is at zero, we also reset the timeTotal counter to make this a new "session" 191 | if (time == 0) timeTotal = 0; 192 | 193 | // 30 second steps up to 5 minutes, then 1 minute steps 194 | if (time < 5 * 60) { 195 | time += delta * 30; 196 | timeTotal += delta * 30; 197 | } else { 198 | time += delta * 60; 199 | timeTotal += delta * 60; 200 | } 201 | 202 | // don't go below zero 203 | if (time < 0) time = 0; 204 | if (timeTotal < 0) timeTotal = 0; 205 | 206 | refreshScreen(true); 207 | } 208 | 209 | void onButtonDown(){ 210 | timeSinceInput = 0; 211 | 212 | if (state == ALARM){ 213 | dismissAlarm(); 214 | return; 215 | } 216 | 217 | if (state == IDLE || state == TIMER){ 218 | setState(VIEW_TOTAL); 219 | return; 220 | } 221 | 222 | buttonDown = true; 223 | } 224 | 225 | void onButtonUp(){ 226 | buttonDown = false; 227 | timeSinceInput = 0; 228 | } 229 | 230 | void dismissAlarm(){ 231 | setState(ALARM_OFF_COOLDOWN); 232 | time = 0; 233 | playMelody(melody_timer_dismiss); 234 | refreshScreen(); 235 | } 236 | 237 | void onSecond(){ 238 | if (state == ALARM) onAlarm(false); 239 | if (time == 0) { 240 | refreshScreen(); 241 | return; 242 | } 243 | 244 | if (state != TIMER) return; 245 | 246 | time -= 1; 247 | if (time < 0) time = 0; 248 | 249 | refreshScreen(true); 250 | 251 | if (time == 0) onAlarm(true); 252 | } 253 | 254 | void onAlarm(bool setActive){ 255 | if (setActive) setState(ALARM); 256 | if ((timeInState / 1000) % 2 == 1) return; 257 | playMelody(melody_alarm); 258 | } 259 | 260 | void refreshScreen(){ 261 | refreshScreen(false); 262 | } 263 | 264 | void refreshScreen(bool force){ 265 | long currentFrame = frame(); 266 | if (currentFrame == lastFrame && !force) return; 267 | lastFrame = currentFrame; 268 | 269 | // play alarm animation 270 | if (state == ALARM){ 271 | int frameHalf = currentFrame / 2; 272 | int frameHalfOffset = currentFrame / 2 + 50; 273 | 274 | matrix.setCursor(3 - frameHalf % 4, 8); 275 | matrix.print(";"); 276 | 277 | matrix.setCursor(11 + frameHalfOffset % 4, 8); 278 | matrix.print("<"); 279 | 280 | matrix.setCursor(5 + (currentFrame % 2 == 0 ? 0 : -1), 8); 281 | matrix.print("@"); 282 | swapBuffers(); 283 | return; 284 | } 285 | 286 | // alarm's just been turned off, show a little confirm message 287 | if (state == ALARM_OFF_COOLDOWN) { 288 | matrix.setCursor(2, 7); 289 | matrix.print("off"); 290 | swapBuffers(); 291 | return; 292 | } 293 | 294 | // we're idle, show a cute face 295 | if (state == IDLE) { 296 | // do a nice fade out before going to sleep 297 | int time = timeSinceInput; 298 | float fade = (float) (time - FADE_AFTER_MS) / FADE_DURATION_MS; 299 | if (fade < 0) fade = 0; 300 | matrix.setTextColor(BRIGHTNESS * (1.0 - fade)); 301 | 302 | long frame = currentFrame % 40; 303 | matrix.setCursor(4, 7); 304 | if (frame > 1) matrix.print("="); // eyes 305 | matrix.setCursor(7, 7); 306 | matrix.print("?"); // nose 307 | matrix.setCursor(5, 13); 308 | matrix.print(">"); // mouth 309 | swapBuffers(); 310 | return; 311 | } 312 | 313 | int seconds = -1; 314 | int minutes = -1; 315 | 316 | // timer is active, show the timer digits 317 | if (state == TIMER || state == SET_TIMER) { 318 | seconds = time % 60; 319 | minutes = time / 60; 320 | 321 | if (seconds > 0) { 322 | const int barY = 8; 323 | int lastBarPixelX = seconds / 4; 324 | matrix.drawLine(0, barY, lastBarPixelX, barY, BRIGHTNESS); 325 | matrix.drawLine(lastBarPixelX + 1, barY, lastBarPixelX + 1, barY, seconds % 2 == 0 ? BRIGHTNESS_EXTRA : BRIGHTNESS_HALF); 326 | } 327 | } 328 | 329 | if (state == VIEW_TOTAL){ 330 | seconds = timeTotal % 60; 331 | minutes = timeTotal / 60; 332 | } 333 | 334 | if (state == TIMER || state == SET_TIMER || state == VIEW_TOTAL) { 335 | if (minutes > 0) { 336 | matrix.print(minutes); 337 | 338 | if (minutes > 9){ 339 | matrix.print("m"); 340 | } else { 341 | matrix.print(":"); 342 | } 343 | } 344 | 345 | if (seconds < 10) matrix.print(0); 346 | matrix.print(seconds); 347 | if (minutes == 0) matrix.print("s"); 348 | } 349 | 350 | swapBuffers(); 351 | } 352 | 353 | void loop() { 354 | if (inWakeUp) wakeUp(); 355 | 356 | readRotaryEncoder(); 357 | if (pushbutton.update()){ 358 | if (pushbutton.fallingEdge()) onButtonDown(); 359 | if (pushbutton.risingEdge()) onButtonUp(); 360 | } 361 | if (!buttonDown) timeButtonHold = 0; 362 | 363 | if (timerSeconds >= MS_IN_A_SECOND) { 364 | onSecond(); 365 | timerSeconds -= MS_IN_A_SECOND; 366 | } 367 | 368 | switch(state) { 369 | case IDLE: loopIdle(); 370 | break; 371 | case SET_TIMER: loopSetTimer(); 372 | break; 373 | case TIMER: loopTimer(); 374 | break; 375 | case ALARM: loopAlarm(); 376 | break; 377 | case ALARM_OFF_COOLDOWN: loopAlarmOffCooldown(); 378 | break; 379 | case VIEW_TOTAL: loopViewTotal(); 380 | break; 381 | case MUSIC: loopMusic(); 382 | } 383 | 384 | updateMelody(); 385 | idle(); 386 | } 387 | 388 | void loopIdle(){ 389 | refreshScreen(); 390 | if (timeSinceInput > SLEEP_AFTER_MS) sleep(); 391 | } 392 | 393 | void loopSetTimer(){ 394 | if (timeSinceInput < WAIT_AFTER_INPUT_MS) return; 395 | if (time > 0){ 396 | playMelody(melody_timer_start); 397 | setState(TIMER); 398 | } else { 399 | playMelody(melody_timer_dismiss); 400 | setState(IDLE); 401 | } 402 | // we reset this here to make the delay until the timer ticks consistent 403 | timerSeconds = 700; 404 | } 405 | 406 | void loopTimer(){ 407 | 408 | } 409 | 410 | void loopAlarm(){ 411 | // if the button is down, we instantly dismiss any alarm, if it's just the timer, we wait for one second first 412 | if (buttonDown) { 413 | dismissAlarm(); 414 | onButtonUp(); 415 | } 416 | refreshScreen(); 417 | } 418 | 419 | void loopAlarmOffCooldown(){ 420 | refreshScreen(); 421 | if (timeInState < IGNORE_TIMER_SET_AFTER_ALARM_MS) return; 422 | setState(IDLE); 423 | } 424 | 425 | void loopViewTotal(){ 426 | refreshScreen(); 427 | if (timeButtonHold > 3000){ 428 | playMelody(melody_nevergonnagive); 429 | setState(MUSIC); 430 | } 431 | if (buttonDown || timeInState < WAIT_WHILE_VIEW_TOTAL_MS) return; 432 | if (time > 0){ 433 | setState(TIMER); 434 | playMelody(melody_timer_start); 435 | } else { 436 | playMelody(melody_timer_dismiss); 437 | setState(IDLE); 438 | } 439 | } 440 | 441 | void loopMusic(){ 442 | if (!isPlayingMelody()) setState(IDLE); 443 | } 444 | 445 | void idle() { 446 | set_sleep_mode(SLEEP_MODE_IDLE); 447 | noInterrupts(); 448 | sleep_enable(); 449 | interrupts(); 450 | sleep_cpu(); 451 | sleep_disable(); 452 | } 453 | 454 | void sleep() { 455 | #ifdef USE_SLEEP_LED 456 | digitalWrite(PIN_SLEEP_LED, HIGH); // set the LED on 457 | #endif 458 | 459 | // make sure melodies are stopped, we don't want to start playing anything when we wake up 460 | stopMelody(); 461 | 462 | // reset the button down flag too, just to avoid any funny business 463 | buttonDown = false; 464 | 465 | // set the sleep flag, this makes the wakeUpInterrupt function actually trigger the wakeup once called 466 | inSleep = true; 467 | 468 | // setting the pin mode triggers some kind of sleep thing on the screen, so i only do it here 469 | // not in setup. this seems to work well. 470 | pinMode(PIN_SLEEP_SCREEN, OUTPUT); 471 | digitalWrite(PIN_SLEEP_SCREEN, LOW); 472 | 473 | Serial.end(); // shut off USB 474 | ADCSRA = 0; // shut off ADC 475 | set_sleep_mode(SLEEP_MODE_PWR_DOWN); 476 | noInterrupts(); 477 | sleep_enable(); 478 | interrupts(); 479 | sleep_cpu(); // cpu goes to sleep here 480 | sleep_disable(); // this is where we come back in again after sleeping 481 | } 482 | 483 | // this function gets called by the encoder class, i couln't make the interrupts play nice together 484 | // so i hacked it in there, this sets a flag that is then read by the loop() which brings everything back 485 | void wakeUpInterrupt(){ 486 | if (!inSleep) return; 487 | inSleep = false; 488 | inWakeUp = true; 489 | } 490 | 491 | long frame() { 492 | return millis() / (long) 100; 493 | } 494 | 495 | void setState(State newState){ 496 | if (state == newState) return; 497 | state = newState; 498 | timeInState = 0; 499 | switch(state) { 500 | case IDLE: Serial.println("IDLE"); 501 | break; 502 | case SET_TIMER: Serial.println("SET_TIMER"); 503 | break; 504 | case TIMER: Serial.println("TIMER"); 505 | break; 506 | case ALARM: Serial.println("ALARM"); 507 | break; 508 | case ALARM_OFF_COOLDOWN: Serial.println("ALARM_OFF_COOLDOWN"); 509 | break; 510 | case VIEW_TOTAL: Serial.println("VIEW_TOTAL"); 511 | break; 512 | case MUSIC: Serial.println("MUSIC"); 513 | } 514 | } 515 | -------------------------------------------------------------------------------- /kitchen_font.h: -------------------------------------------------------------------------------- 1 | const uint8_t kitchen_font_Bitmaps[] PROGMEM = { 2 | 0x69, 0x9B, 0xD9, 0x60, 0x59, 0x24, 0xB8, 0x69, 0x12, 0x48, 0xF0, 0xE1, 3 | 0x16, 0x11, 0xE0, 0x99, 0x9F, 0x11, 0x10, 0xF8, 0x8E, 0x19, 0x60, 0x68, 4 | 0x8E, 0x99, 0x60, 0xF1, 0x12, 0x44, 0x40, 0x69, 0x96, 0x99, 0x60, 0x69, 5 | 0x97, 0x11, 0x10, 0x90, 0x5A, 0x94, 0x00, 0xA5, 0x68, 0x00, 0x81, 0x81, 6 | 0x85, 0xE0, 0x55, 0xC0, 0x38, 0x89, 0x12, 0x24, 0x50, 0x7F, 0x80, 0xB5, 7 | 0xC6, 0x00, 0x73, 0xC9, 0x00, 0x93, 0x5B, 0x40, 0x96, 0xEB, 0x40, 0xFC, 8 | 0x00, 0x55, 0x6B, 0x50, 0x56, 0xD4, 0x00, 0x68, 0x61, 0xE0 9 | }; 10 | 11 | const GFXglyph kitchen_font_Glyphs[] PROGMEM = { 12 | { 0, 4, 7, 5, 0, -7 }, // 0x30 '0' 13 | { 4, 3, 8, 5, 1, -7 }, // 0x31 '1' 14 | { 7, 4, 7, 5, 0, -7 }, // 0x32 '2' 15 | { 11, 4, 7, 5, 0, -7 }, // 0x33 '3' 16 | { 15, 4, 7, 5, 0, -7 }, // 0x34 '4' 17 | { 19, 4, 7, 5, 0, -7 }, // 0x35 '5' 18 | { 23, 4, 7, 5, 0, -7 }, // 0x36 '6' 19 | { 27, 4, 7, 5, 0, -7 }, // 0x37 '7' 20 | { 31, 4, 7, 5, 0, -7 }, // 0x38 '8' 21 | { 35, 4, 7, 5, 0, -7 }, // 0x39 '9' 22 | { 39, 1, 4, 2, 0, -5 }, // 0x3A ':' 23 | { 40, 2, 7, 2, 0, -7 }, // 0x3B ';' 24 | { 43, 2, 7, 2, 0, -7 }, // 0x3C '<' 25 | { 46, 8, 2, 8, 0, -7 }, // 0x3D '=' 26 | { 48, 6, 2, 6, 0, -7 }, // 0x3E '>' 27 | { 50, 2, 5, 2, 0, -7 }, // 0x3F '?' 28 | { 52, 7, 7, 7, 0, -7 }, // 0x40 '@' 29 | { 0, 0, 0, 0, 0, 0 }, // 0x41 'A' 30 | { 0, 0, 0, 0, 0, 0 }, // 0x42 'B' 31 | { 0, 0, 0, 0, 0, 0 }, // 0x43 'C' 32 | { 0, 0, 0, 0, 0, 0 }, // 0x44 'D' 33 | { 0, 0, 0, 0, 0, 0 }, // 0x45 'E' 34 | { 0, 0, 0, 0, 0, 0 }, // 0x46 'F' 35 | { 0, 0, 0, 0, 0, 0 }, // 0x47 'G' 36 | { 0, 0, 0, 0, 0, 0 }, // 0x48 'H' 37 | { 0, 0, 0, 0, 0, 0 }, // 0x49 'I' 38 | { 0, 0, 0, 0, 0, 0 }, // 0x4A 'J' 39 | { 0, 0, 0, 0, 0, 0 }, // 0x4B 'K' 40 | { 0, 0, 0, 0, 0, 0 }, // 0x4C 'L' 41 | { 0, 0, 0, 0, 0, 0 }, // 0x4D 'M' 42 | { 0, 0, 0, 0, 0, 0 }, // 0x4E 'N' 43 | { 0, 0, 0, 0, 0, 0 }, // 0x4F 'O' 44 | { 0, 0, 0, 0, 0, 0 }, // 0x50 'P' 45 | { 0, 0, 0, 0, 0, 0 }, // 0x51 'Q' 46 | { 0, 0, 0, 0, 0, 0 }, // 0x52 'R' 47 | { 0, 0, 0, 0, 0, 0 }, // 0x53 'S' 48 | { 0, 0, 0, 0, 0, 0 }, // 0x54 'T' 49 | { 0, 0, 0, 0, 0, 0 }, // 0x55 'U' 50 | { 0, 0, 0, 0, 0, 0 }, // 0x56 'V' 51 | { 0, 0, 0, 0, 0, 0 }, // 0x57 'W' 52 | { 0, 0, 0, 0, 0, 0 }, // 0x58 'X' 53 | { 0, 0, 0, 0, 0, 0 }, // 0x59 'Y' 54 | { 0, 0, 0, 0, 0, 0 }, // 0x5A 'Z' 55 | { 0, 0, 0, 0, 0, 0 }, // 0x5B '[' 56 | { 0, 0, 0, 0, 0, 0 }, // 0x5C '\' 57 | { 0, 0, 0, 0, 0, 0 }, // 0x5D ']' 58 | { 0, 0, 0, 0, 0, 0 }, // 0x5E '^' 59 | { 0, 0, 0, 0, 0, 0 }, // 0x5F '_' 60 | { 0, 0, 0, 0, 0, 0 }, // 0x60 '`' 61 | { 0, 0, 0, 0, 0, 0 }, // 0x61 'a' 62 | { 0, 0, 0, 0, 0, 0 }, // 0x62 'b' 63 | { 0, 0, 0, 0, 0, 0 }, // 0x63 'c' 64 | { 0, 0, 0, 0, 0, 0 }, // 0x64 'd' 65 | { 59, 3, 5, 4, 0, -5 }, // 0x65 'e' 66 | { 62, 3, 6, 4, 0, -6 }, // 0x66 'f' 67 | { 0, 0, 0, 0, 0, 0 }, // 0x67 'g' 68 | { 65, 3, 6, 4, 0, -6 }, // 0x68 'h' 69 | { 0, 0, 0, 0, 0, 0 }, // 0x69 'i' 70 | { 0, 0, 0, 0, 0, 0 }, // 0x6A 'j' 71 | { 68, 3, 6, 4, 0, -6 }, // 0x6B 'k' 72 | { 71, 1, 7, 2, 0, -6 }, // 0x6C 'l' 73 | { 73, 5, 4, 6, 0, -4 }, // 0x6D 'm' 74 | { 0, 0, 0, 0, 0, 0 }, // 0x6E 'n' 75 | { 76, 3, 5, 4, 0, -5 }, // 0x6F 'o' 76 | { 0, 0, 0, 0, 0, 0 }, // 0x70 'p' 77 | { 0, 0, 0, 0, 0, 0 }, // 0x71 'q' 78 | { 0, 0, 0, 0, 0, 0 }, // 0x72 'r' 79 | { 79, 4, 5, 5, 0, -5 } // 0x73 's' 80 | }; 81 | 82 | const GFXfont kitchen_font PROGMEM = {(uint8_t *) kitchen_font_Bitmaps, (GFXglyph *)kitchen_font_Glyphs, 0x30, 0x73, 7}; 83 | -------------------------------------------------------------------------------- /music.cpp: -------------------------------------------------------------------------------- 1 | #include "music.h" 2 | #include "toneAC.h" 3 | 4 | Melody melody; 5 | int index = -1; 6 | 7 | // this calculates the duration of a whole note in ms 8 | int wholenote = 0; 9 | int noteDuration = 0; 10 | 11 | elapsedMillis musicTimer; 12 | 13 | bool isPlayingMelody(){ 14 | return index >= 0; 15 | } 16 | 17 | void updateMelody(){ 18 | if ((int) musicTimer < noteDuration) return; 19 | if (index < 0) return; 20 | 21 | musicTimer -= noteDuration; 22 | 23 | // calculates the duration of each note 24 | int divider = melody.data[index + 1]; 25 | 26 | if (divider > 0) { 27 | // regular note, just proceed 28 | noteDuration = (wholenote) / divider; 29 | } else if (divider < 0) { 30 | // dotted notes are represented with negative durations!! 31 | noteDuration = (wholenote) / abs(divider); 32 | noteDuration *= 1.5; // increases the duration in half for dotted notes 33 | } 34 | 35 | // we only play the note for 90% of the duration, leaving 10% as a pause 36 | if (melody.data[index] == REST) noToneAC(); 37 | else toneAC(melody.data[index], melody.volume, noteDuration * .9, true); 38 | 39 | // if we're at the end of the song, stop 40 | if (index + 2 >= melody.size * 2) stopMelody(); 41 | // move the playhead to the next note 42 | else index += 2; 43 | } 44 | 45 | void playMelody(Melody newMelody) { 46 | melody = newMelody; 47 | wholenote = (60000 * 4) / melody.tempo; 48 | index = 0; 49 | musicTimer = 0; 50 | noteDuration = 0; 51 | } 52 | 53 | void stopMelody() { 54 | index = -1; 55 | } 56 | -------------------------------------------------------------------------------- /music.h: -------------------------------------------------------------------------------- 1 | #ifndef music_h 2 | #define music_h 3 | 4 | #include "pitches.h" 5 | 6 | typedef struct { 7 | const int* data; 8 | int size; 9 | int tempo; 10 | int volume; 11 | } Melody; 12 | 13 | 14 | void playMelody(Melody melody); 15 | void updateMelody(); 16 | void stopMelody(); 17 | bool isPlayingMelody(); 18 | 19 | #endif 20 | -------------------------------------------------------------------------------- /music_data.h: -------------------------------------------------------------------------------- 1 | #include "music.h" 2 | 3 | #define MELODY(name,tempo,volume) Melody melody_##name = { name, sizeof(name) / sizeof(name[0]) / 2, tempo, volume}; 4 | 5 | const int alarm[] = { 6 | NOTE_C8, 4, NOTE_B7, 4, NOTE_D8, 4, REST, 4, 7 | NOTE_C8, 4, NOTE_B7, 4, NOTE_D8, 4, REST, 4, 8 | NOTE_C8, 4, NOTE_B7, 4, NOTE_D8, 4, REST, 4, 9 | }; 10 | 11 | MELODY(alarm, 900, 10) 12 | 13 | const int timer_start[] = { 14 | NOTE_C6, 4, NOTE_D6, 4, REST, 4, 15 | NOTE_C6, 4, NOTE_D6, 4, REST, 4, 16 | }; 17 | 18 | MELODY(timer_start, 2000, 4) 19 | 20 | const int timer_dismiss[] = { 21 | NOTE_D5, 4, NOTE_C5, 4, NOTE_D5, 4, REST, 4, 22 | }; 23 | 24 | MELODY(timer_dismiss, 900, 3) 25 | 26 | const int cantina[] = { 27 | // Cantina Band - Star wars 28 | // Score available at https://musescore.com/user/6795541/scores/1606876 29 | NOTE_B4,-4, NOTE_E5,-4, NOTE_B4,-4, NOTE_E5,-4, 30 | NOTE_B4,8, NOTE_E5,-4, NOTE_B4,8, REST,8, NOTE_AS4,8, NOTE_B4,8, 31 | NOTE_B4,8, NOTE_AS4,8, NOTE_B4,8, NOTE_A4,8, REST,8, NOTE_GS4,8, NOTE_A4,8, NOTE_G4,8, 32 | NOTE_G4,4, NOTE_E4,-2, 33 | NOTE_B4,-4, NOTE_E5,-4, NOTE_B4,-4, NOTE_E5,-4, 34 | NOTE_B4,8, NOTE_E5,-4, NOTE_B4,8, REST,8, NOTE_AS4,8, NOTE_B4,8, 35 | 36 | NOTE_A4,-4, NOTE_A4,-4, NOTE_GS4,8, NOTE_A4,-4, 37 | NOTE_D5,8, NOTE_C5,-4, NOTE_B4,-4, NOTE_A4,-4, 38 | NOTE_B4,-4, NOTE_E5,-4, NOTE_B4,-4, NOTE_E5,-4, 39 | NOTE_B4,8, NOTE_E5,-4, NOTE_B4,8, REST,8, NOTE_AS4,8, NOTE_B4,8, 40 | NOTE_D5,4, NOTE_D5,-4, NOTE_B4,8, NOTE_A4,-4, 41 | NOTE_G4,-4, NOTE_E4,-2, 42 | NOTE_E4, 2, NOTE_G4,2, 43 | NOTE_B4, 2, NOTE_D5,2, 44 | 45 | NOTE_F5, -4, NOTE_E5,-4, NOTE_AS4,8, NOTE_AS4,8, NOTE_B4,4, NOTE_G4,4, 46 | }; 47 | 48 | // Melody melody_cantina = { cantina, sizeof(cantina) / sizeof(cantina[0]) / 2, 144}; 49 | 50 | MELODY(cantina, 144, 8) 51 | 52 | const int tetris[] = { 53 | 54 | //Based on the arrangement at https://www.flutetunes.com/tunes.php?id=192 55 | 56 | NOTE_E5, 4, NOTE_B4,8, NOTE_C5,8, NOTE_D5,4, NOTE_C5,8, NOTE_B4,8, 57 | NOTE_A4, 4, NOTE_A4,8, NOTE_C5,8, NOTE_E5,4, NOTE_D5,8, NOTE_C5,8, 58 | NOTE_B4, -4, NOTE_C5,8, NOTE_D5,4, NOTE_E5,4, 59 | NOTE_C5, 4, NOTE_A4,4, NOTE_A4,8, NOTE_A4,4, NOTE_B4,8, NOTE_C5,8, 60 | 61 | NOTE_D5, -4, NOTE_F5,8, NOTE_A5,4, NOTE_G5,8, NOTE_F5,8, 62 | NOTE_E5, -4, NOTE_C5,8, NOTE_E5,4, NOTE_D5,8, NOTE_C5,8, 63 | NOTE_B4, 4, NOTE_B4,8, NOTE_C5,8, NOTE_D5,4, NOTE_E5,4, 64 | NOTE_C5, 4, NOTE_A4,4, NOTE_A4,4, REST, 4, 65 | }; 66 | 67 | MELODY(tetris, 144, 8) 68 | 69 | const int mii_channel[] = { 70 | 71 | // Mii Channel theme 72 | // Score available at https://musescore.com/user/16403456/scores/4984153 73 | // Uploaded by Catalina Andrade 74 | 75 | NOTE_FS4,8, REST,8, NOTE_A4,8, NOTE_CS5,8, REST,8,NOTE_A4,8, REST,8, NOTE_FS4,8, //1 76 | NOTE_D4,8, NOTE_D4,8, NOTE_D4,8, REST,8, REST,4, REST,8, NOTE_CS4,8, 77 | NOTE_D4,8, NOTE_FS4,8, NOTE_A4,8, NOTE_CS5,8, REST,8, NOTE_A4,8, REST,8, NOTE_F4,8, 78 | NOTE_E5,-4, NOTE_DS5,8, NOTE_D5,8, REST,8, REST,4, 79 | 80 | NOTE_GS4,8, REST,8, NOTE_CS5,8, NOTE_FS4,8, REST,8,NOTE_CS5,8, REST,8, NOTE_GS4,8, //5 81 | REST,8, NOTE_CS5,8, NOTE_G4,8, NOTE_FS4,8, REST,8, NOTE_E4,8, REST,8, 82 | NOTE_E4,8, NOTE_E4,8, NOTE_E4,8, REST,8, REST,4, NOTE_E4,8, NOTE_E4,8, 83 | NOTE_E4,8, REST,8, REST,4, NOTE_DS4,8, NOTE_D4,8, 84 | 85 | NOTE_CS4,8, REST,8, NOTE_A4,8, NOTE_CS5,8, REST,8,NOTE_A4,8, REST,8, NOTE_FS4,8, //9 86 | NOTE_D4,8, NOTE_D4,8, NOTE_D4,8, REST,8, NOTE_E5,8, NOTE_E5,8, NOTE_E5,8, REST,8, 87 | REST,8, NOTE_FS4,8, NOTE_A4,8, NOTE_CS5,8, REST,8, NOTE_A4,8, REST,8, NOTE_F4,8, 88 | NOTE_E5,2, NOTE_D5,8, REST,8, REST,4, 89 | 90 | NOTE_B4,8, NOTE_G4,8, NOTE_D4,8, NOTE_CS4,4, NOTE_B4,8, NOTE_G4,8, NOTE_CS4,8, //13 91 | NOTE_A4,8, NOTE_FS4,8, NOTE_C4,8, NOTE_B3,4, NOTE_F4,8, NOTE_D4,8, NOTE_B3,8, 92 | NOTE_E4,8, NOTE_E4,8, NOTE_E4,8, REST,4, REST,4, NOTE_AS4,4, 93 | NOTE_CS5,8, NOTE_D5,8, NOTE_FS5,8, NOTE_A5,8, REST,8, REST,4, 94 | 95 | REST,2, NOTE_A3,4, NOTE_AS3,4, //17 96 | NOTE_A3,-4, NOTE_A3,8, NOTE_A3,2, 97 | REST,4, NOTE_A3,8, NOTE_AS3,8, NOTE_A3,8, NOTE_F4,4, NOTE_C4,8, 98 | NOTE_A3,-4, NOTE_A3,8, NOTE_A3,2, 99 | 100 | REST,2, NOTE_B3,4, NOTE_C4,4, //21 101 | NOTE_CS4,-4, NOTE_C4,8, NOTE_CS4,2, 102 | REST,4, NOTE_CS4,8, NOTE_C4,8, NOTE_CS4,8, NOTE_GS4,4, NOTE_DS4,8, 103 | NOTE_CS4,-4, NOTE_DS4,8, NOTE_B3,1, 104 | 105 | NOTE_E4,4, NOTE_E4,4, NOTE_E4,4, REST,8,//25 106 | 107 | //repeats 1-25 108 | 109 | NOTE_FS4,8, REST,8, NOTE_A4,8, NOTE_CS5,8, REST,8,NOTE_A4,8, REST,8, NOTE_FS4,8, //1 110 | NOTE_D4,8, NOTE_D4,8, NOTE_D4,8, REST,8, REST,4, REST,8, NOTE_CS4,8, 111 | NOTE_D4,8, NOTE_FS4,8, NOTE_A4,8, NOTE_CS5,8, REST,8, NOTE_A4,8, REST,8, NOTE_F4,8, 112 | NOTE_E5,-4, NOTE_DS5,8, NOTE_D5,8, REST,8, REST,4, 113 | 114 | NOTE_GS4,8, REST,8, NOTE_CS5,8, NOTE_FS4,8, REST,8,NOTE_CS5,8, REST,8, NOTE_GS4,8, //5 115 | REST,8, NOTE_CS5,8, NOTE_G4,8, NOTE_FS4,8, REST,8, NOTE_E4,8, REST,8, 116 | NOTE_E4,8, NOTE_E4,8, NOTE_E4,8, REST,8, REST,4, NOTE_E4,8, NOTE_E4,8, 117 | NOTE_E4,8, REST,8, REST,4, NOTE_DS4,8, NOTE_D4,8, 118 | 119 | NOTE_CS4,8, REST,8, NOTE_A4,8, NOTE_CS5,8, REST,8,NOTE_A4,8, REST,8, NOTE_FS4,8, //9 120 | NOTE_D4,8, NOTE_D4,8, NOTE_D4,8, REST,8, NOTE_E5,8, NOTE_E5,8, NOTE_E5,8, REST,8, 121 | REST,8, NOTE_FS4,8, NOTE_A4,8, NOTE_CS5,8, REST,8, NOTE_A4,8, REST,8, NOTE_F4,8, 122 | NOTE_E5,2, NOTE_D5,8, REST,8, REST,4, 123 | 124 | NOTE_B4,8, NOTE_G4,8, NOTE_D4,8, NOTE_CS4,4, NOTE_B4,8, NOTE_G4,8, NOTE_CS4,8, //13 125 | NOTE_A4,8, NOTE_FS4,8, NOTE_C4,8, NOTE_B3,4, NOTE_F4,8, NOTE_D4,8, NOTE_B3,8, 126 | NOTE_E4,8, NOTE_E4,8, NOTE_E4,8, REST,4, REST,4, NOTE_AS4,4, 127 | NOTE_CS5,8, NOTE_D5,8, NOTE_FS5,8, NOTE_A5,8, REST,8, REST,4, 128 | 129 | REST,2, NOTE_A3,4, NOTE_AS3,4, //17 130 | NOTE_A3,-4, NOTE_A3,8, NOTE_A3,2, 131 | REST,4, NOTE_A3,8, NOTE_AS3,8, NOTE_A3,8, NOTE_F4,4, NOTE_C4,8, 132 | NOTE_A3,-4, NOTE_A3,8, NOTE_A3,2, 133 | 134 | REST,2, NOTE_B3,4, NOTE_C4,4, //21 135 | NOTE_CS4,-4, NOTE_C4,8, NOTE_CS4,2, 136 | REST,4, NOTE_CS4,8, NOTE_C4,8, NOTE_CS4,8, NOTE_GS4,4, NOTE_DS4,8, 137 | NOTE_CS4,-4, NOTE_DS4,8, NOTE_B3,1, 138 | 139 | NOTE_E4,4, NOTE_E4,4, NOTE_E4,4, REST,8,//25 140 | 141 | //finishes with 26 142 | //NOTE_FS4,8, REST,8, NOTE_A4,8, NOTE_CS5,8, REST,8, NOTE_A4,8, REST,8, NOTE_FS4,8 143 | 144 | }; 145 | 146 | MELODY(mii_channel, 114, 8) 147 | 148 | const int nevergonnagive[] = { 149 | // Never Gonna Give You Up - Rick Astley 150 | // Score available at https://musescore.com/chlorondria_5/never-gonna-give-you-up_alto-sax 151 | // Arranged by Chlorondria 152 | 153 | /*NOTE_D5,-4, NOTE_E5,-4, NOTE_A4,4, //1 154 | NOTE_E5,-4, NOTE_FS5,-4, NOTE_A5,16, NOTE_G5,16, NOTE_FS5,8, 155 | NOTE_D5,-4, NOTE_E5,-4, NOTE_A4,2, 156 | NOTE_A4,16, NOTE_A4,16, NOTE_B4,16, NOTE_D5,8, NOTE_D5,16, 157 | NOTE_D5,-4, NOTE_E5,-4, NOTE_A4,4, //repeat from 1 158 | NOTE_E5,-4, NOTE_FS5,-4, NOTE_A5,16, NOTE_G5,16, NOTE_FS5,8, 159 | NOTE_D5,-4, NOTE_E5,-4, NOTE_A4,2, 160 | NOTE_A4,16, NOTE_A4,16, NOTE_B4,16, NOTE_D5,8, NOTE_D5,16, 161 | REST,4, NOTE_B4,8, NOTE_CS5,8, NOTE_D5,8, NOTE_D5,8, NOTE_E5,8, NOTE_CS5,-8, 162 | NOTE_B4,16, NOTE_A4,2, REST,4, 163 | 164 | REST,8, NOTE_B4,8, NOTE_B4,8, NOTE_CS5,8, NOTE_D5,8, NOTE_B4,4, NOTE_A4,8, //7 165 | NOTE_A5,8, REST,8, NOTE_A5,8, NOTE_E5,-4, REST,4, 166 | NOTE_B4,8, NOTE_B4,8, NOTE_CS5,8, NOTE_D5,8, NOTE_B4,8, NOTE_D5,8, NOTE_E5,8, REST,8, 167 | REST,8, NOTE_CS5,8, NOTE_B4,8, NOTE_A4,-4, REST,4, 168 | REST,8, NOTE_B4,8, NOTE_B4,8, NOTE_CS5,8, NOTE_D5,8, NOTE_B4,8, NOTE_A4,4, 169 | NOTE_E5,8, NOTE_E5,8, NOTE_E5,8, NOTE_FS5,8, NOTE_E5,4, REST,4, 170 | 171 | NOTE_D5,2, NOTE_E5,8, NOTE_FS5,8, NOTE_D5,8, //13 172 | NOTE_E5,8, NOTE_E5,8, NOTE_E5,8, NOTE_FS5,8, NOTE_E5,4, NOTE_A4,4, 173 | REST,2, NOTE_B4,8, NOTE_CS5,8, NOTE_D5,8, NOTE_B4,8, 174 | REST,8, NOTE_E5,8, NOTE_FS5,8, NOTE_E5,-4, NOTE_A4,16, NOTE_B4,16, NOTE_D5,16, NOTE_B4,16, 175 | NOTE_FS5,-8, NOTE_FS5,-8, NOTE_E5,-4, NOTE_A4,16, NOTE_B4,16, NOTE_D5,16, NOTE_B4,16, 176 | 177 | NOTE_E5,-8, NOTE_E5,-8, NOTE_D5,-8, NOTE_CS5,16, NOTE_B4,-8, NOTE_A4,16, NOTE_B4,16, NOTE_D5,16, NOTE_B4,16, //18 178 | NOTE_D5,4, NOTE_E5,8, NOTE_CS5,-8, NOTE_B4,16, NOTE_A4,8, NOTE_A4,8, NOTE_A4,8, 179 | NOTE_E5,4, NOTE_D5,2, NOTE_A4,16, NOTE_B4,16, NOTE_D5,16, NOTE_B4,16, 180 | NOTE_FS5,-8, NOTE_FS5,-8, NOTE_E5,-4, NOTE_A4,16, NOTE_B4,16, NOTE_D5,16, NOTE_B4,16, 181 | NOTE_A5,4, NOTE_CS5,8, NOTE_D5,-8, NOTE_CS5,16, NOTE_B4,8, NOTE_A4,16, NOTE_B4,16, NOTE_D5,16, NOTE_B4,16, 182 | 183 | NOTE_D5,4, NOTE_E5,8, NOTE_CS5,-8, NOTE_B4,16, NOTE_A4,4, NOTE_A4,8, //23 184 | NOTE_E5,4, NOTE_D5,2, REST,4, 185 | REST,8, NOTE_B4,8, NOTE_D5,8, NOTE_B4,8, NOTE_D5,8, NOTE_E5,4, REST,8, 186 | REST,8, NOTE_CS5,8, NOTE_B4,8, NOTE_A4,-4, REST,4, 187 | REST,8, NOTE_B4,8, NOTE_B4,8, NOTE_CS5,8, NOTE_D5,8, NOTE_B4,8, NOTE_A4,4, 188 | REST,8, NOTE_A5,8, NOTE_A5,8, NOTE_E5,8, NOTE_FS5,8, NOTE_E5,8, NOTE_D5,8, 189 | 190 | REST,8, NOTE_A4,8, NOTE_B4,8, NOTE_CS5,8, NOTE_D5,8, NOTE_B4,8, //29 191 | REST,8, NOTE_CS5,8, NOTE_B4,8, NOTE_A4,-4, REST,4, 192 | NOTE_B4,8, NOTE_B4,8, NOTE_CS5,8, NOTE_D5,8, NOTE_B4,8, NOTE_A4,4, REST,8, 193 | REST,8, NOTE_E5,8, NOTE_E5,8, NOTE_FS5,4, NOTE_E5,-4, 194 | NOTE_D5,2, NOTE_D5,8, NOTE_E5,8, NOTE_FS5,8, NOTE_E5,4, 195 | NOTE_E5,8, NOTE_E5,8, NOTE_FS5,8, NOTE_E5,8, NOTE_A4,8, NOTE_A4,4, 196 | 197 | REST,-4, NOTE_A4,8, NOTE_B4,8, NOTE_CS5,8, NOTE_D5,8, NOTE_B4,8, //35 198 | REST,8, NOTE_E5,8, NOTE_FS5,8, NOTE_E5,-4, NOTE_A4,16, NOTE_B4,16, NOTE_D5,16, NOTE_B4,16, 199 | NOTE_FS5,-8, NOTE_FS5,-8, NOTE_E5,-4, NOTE_A4,16, NOTE_B4,16, NOTE_D5,16, NOTE_B4,16, 200 | NOTE_E5,-8, NOTE_E5,-8, NOTE_D5,-8, NOTE_CS5,16, NOTE_B4,8, NOTE_A4,16, NOTE_B4,16, NOTE_D5,16, NOTE_B4,16, 201 | NOTE_D5,4, NOTE_E5,8, NOTE_CS5,-8, NOTE_B4,16, NOTE_A4,4, NOTE_A4,8, 202 | 203 | NOTE_E5,4, NOTE_D5,2, NOTE_A4,16, NOTE_B4,16, NOTE_D5,16, NOTE_B4,16, //40 204 | NOTE_FS5,-8, NOTE_FS5,-8, NOTE_E5,-4, NOTE_A4,16, NOTE_B4,16, NOTE_D5,16, NOTE_B4,16, 205 | NOTE_A5,4, NOTE_CS5,8, NOTE_D5,-8, NOTE_CS5,16, NOTE_B4,8, NOTE_A4,16, NOTE_B4,16, NOTE_D5,16, NOTE_B4,16, 206 | NOTE_D5,4, NOTE_E5,8, NOTE_CS5,-8, NOTE_B4,16, NOTE_A4,4, NOTE_A4,8, 207 | NOTE_E5,4, NOTE_D5,2, NOTE_A4,16, NOTE_B4,16, NOTE_D5,16, NOTE_B4,16, 208 | 209 | NOTE_FS5,-8, NOTE_FS5,-8, NOTE_E5,-4, NOTE_A4,16, NOTE_B4,16, NOTE_D5,16, NOTE_B4,16, //45 210 | NOTE_A5,4, NOTE_CS5,8, NOTE_D5,-8, NOTE_CS5,16, NOTE_B4,8, NOTE_A4,16, NOTE_B4,16, NOTE_D5,16, NOTE_B4,16, 211 | NOTE_D5,4, NOTE_E5,8, NOTE_CS5,-8, NOTE_B4,16, NOTE_A4,4, NOTE_A4,8, 212 | NOTE_E5,4, NOTE_D5,2, */NOTE_A4,16, NOTE_B4,16, NOTE_D5,16, NOTE_B4,16, 213 | NOTE_FS5,-8, NOTE_FS5,-8, NOTE_E5,-4, NOTE_A4,16, NOTE_B4,16, NOTE_D5,16, NOTE_B4,16, //45 214 | 215 | NOTE_A5,4, NOTE_CS5,8, NOTE_D5,-8, NOTE_CS5,16, NOTE_B4,8, NOTE_A4,16, NOTE_B4,16, NOTE_D5,16, NOTE_B4,16, 216 | NOTE_D5,4, NOTE_E5,8, NOTE_CS5,-8, NOTE_B4,16, NOTE_A4,4, NOTE_A4,8, 217 | 218 | NOTE_E5,4, NOTE_D5,2, REST,4 219 | }; 220 | 221 | MELODY(nevergonnagive, 114, 8) 222 | 223 | 224 | int nokia[] = { 225 | 226 | // Nokia Ringtone 227 | // Score available at https://musescore.com/user/29944637/scores/5266155 228 | 229 | NOTE_E5, 8, NOTE_D5, 8, NOTE_FS4, 4, NOTE_GS4, 4, 230 | NOTE_CS5, 8, NOTE_B4, 8, NOTE_D4, 4, NOTE_E4, 4, 231 | NOTE_B4, 8, NOTE_A4, 8, NOTE_CS4, 4, NOTE_E4, 4, 232 | NOTE_A4, 2, 233 | }; 234 | 235 | MELODY(nokia, 180, 8) 236 | -------------------------------------------------------------------------------- /pitches.h: -------------------------------------------------------------------------------- 1 | /************************************************* 2 | Public Constants 3 | *************************************************/ 4 | 5 | #ifndef pitches_h 6 | #define pitches_h 7 | 8 | #define NOTE_B0 31 9 | #define NOTE_C1 33 10 | #define NOTE_CS1 35 11 | #define NOTE_D1 37 12 | #define NOTE_DS1 39 13 | #define NOTE_E1 41 14 | #define NOTE_F1 44 15 | #define NOTE_FS1 46 16 | #define NOTE_G1 49 17 | #define NOTE_GS1 52 18 | #define NOTE_A1 55 19 | #define NOTE_AS1 58 20 | #define NOTE_B1 62 21 | #define NOTE_C2 65 22 | #define NOTE_CS2 69 23 | #define NOTE_D2 73 24 | #define NOTE_DS2 78 25 | #define NOTE_E2 82 26 | #define NOTE_F2 87 27 | #define NOTE_FS2 93 28 | #define NOTE_G2 98 29 | #define NOTE_GS2 104 30 | #define NOTE_A2 110 31 | #define NOTE_AS2 117 32 | #define NOTE_B2 123 33 | #define NOTE_C3 131 34 | #define NOTE_CS3 139 35 | #define NOTE_D3 147 36 | #define NOTE_DS3 156 37 | #define NOTE_E3 165 38 | #define NOTE_F3 175 39 | #define NOTE_FS3 185 40 | #define NOTE_G3 196 41 | #define NOTE_GS3 208 42 | #define NOTE_A3 220 43 | #define NOTE_AS3 233 44 | #define NOTE_B3 247 45 | #define NOTE_C4 262 46 | #define NOTE_CS4 277 47 | #define NOTE_D4 294 48 | #define NOTE_DS4 311 49 | #define NOTE_E4 330 50 | #define NOTE_F4 349 51 | #define NOTE_FS4 370 52 | #define NOTE_G4 392 53 | #define NOTE_GS4 415 54 | #define NOTE_A4 440 55 | #define NOTE_AS4 466 56 | #define NOTE_B4 494 57 | #define NOTE_C5 523 58 | #define NOTE_CS5 554 59 | #define NOTE_D5 587 60 | #define NOTE_DS5 622 61 | #define NOTE_E5 659 62 | #define NOTE_F5 698 63 | #define NOTE_FS5 740 64 | #define NOTE_G5 784 65 | #define NOTE_GS5 831 66 | #define NOTE_A5 880 67 | #define NOTE_AS5 932 68 | #define NOTE_B5 988 69 | #define NOTE_C6 1047 70 | #define NOTE_CS6 1109 71 | #define NOTE_D6 1175 72 | #define NOTE_DS6 1245 73 | #define NOTE_E6 1319 74 | #define NOTE_F6 1397 75 | #define NOTE_FS6 1480 76 | #define NOTE_G6 1568 77 | #define NOTE_GS6 1661 78 | #define NOTE_A6 1760 79 | #define NOTE_AS6 1865 80 | #define NOTE_B6 1976 81 | #define NOTE_C7 2093 82 | #define NOTE_CS7 2217 83 | #define NOTE_D7 2349 84 | #define NOTE_DS7 2489 85 | #define NOTE_E7 2637 86 | #define NOTE_F7 2794 87 | #define NOTE_FS7 2960 88 | #define NOTE_G7 3136 89 | #define NOTE_GS7 3322 90 | #define NOTE_A7 3520 91 | #define NOTE_AS7 3729 92 | #define NOTE_B7 3951 93 | #define NOTE_C8 4186 94 | #define NOTE_CS8 4435 95 | #define NOTE_D8 4699 96 | #define NOTE_DS8 4978 97 | #define REST 0 98 | 99 | #endif -------------------------------------------------------------------------------- /toneAC.cpp: -------------------------------------------------------------------------------- 1 | /* --------------------------------------------------------------------------- 2 | Created by Tim Eckel - teckel@leethost.com 3 | Copyright 2019 License: GNU GPL v3 http://www.gnu.org/licenses/gpl-3.0.html 4 | 5 | See "toneAC.h" for purpose, syntax, version history, links, and more. 6 | --------------------------------------------------------------------------- */ 7 | 8 | #include "toneAC.h" 9 | 10 | unsigned long _tAC_time; // Used to track end note with timer when playing note in the background. 11 | uint8_t _tAC_volume[] = { 200, 100, 67, 50, 40, 33, 29, 22, 11, 2 }; // Duty for linear volume control. 12 | 13 | void toneAC(unsigned long frequency, uint8_t volume, unsigned long length, uint8_t background) { 14 | if (frequency == NOTONEAC || volume == 0) { noToneAC(); return; } // If frequency or volume are 0, turn off sound and return. 15 | if (volume > 10) volume = 10; // Make sure volume is in range (1 to 10). 16 | 17 | toneAC_playNote(frequency, volume); // Routine that plays the note using timers. 18 | 19 | if (length == PLAY_FOREVER) return; // If length is zero, play note forever. 20 | 21 | if (background) { // Background tone playing, returns control to your sketch. 22 | _tAC_time = millis() + length; // Set when the note should end. 23 | TIMSK1 |= _BV(OCIE1A); // Activate the timer interrupt. 24 | } else { 25 | delay(length); // Just a simple delay, doesn't return control till finished. 26 | noToneAC(); 27 | } 28 | } 29 | 30 | void toneAC_playNote(unsigned long frequency, uint8_t volume) { 31 | PWMT1DREG |= _BV(PWMT1AMASK) | _BV(PWMT1BMASK); // Set timer 1 PWM pins to OUTPUT (because analogWrite does it too). 32 | 33 | uint8_t prescaler = _BV(CS10); // Try using prescaler 1 first. 34 | unsigned long top = F_CPU / frequency / 2 - 1; // Calculate the top. 35 | if (top > 65535) { // If not in the range for prescaler 1, use prescaler 256 (122 Hz and lower @ 16 MHz). 36 | prescaler = _BV(CS12); // Set the 256 prescaler bit. 37 | top = top / 256 - 1; // Calculate the top using prescaler 256. 38 | } 39 | 40 | ICR1 = top; // Set the top. 41 | if (TCNT1 > top) TCNT1 = top; // Counter over the top, put within range. 42 | TCCR1B = _BV(WGM13) | prescaler; // Set PWM, phase and frequency corrected (top=ICR1) and prescaler. 43 | OCR1A = OCR1B = top / _tAC_volume[volume - 1]; // Calculate & set the duty cycle (volume). 44 | TCCR1A = _BV(COM1A1) | _BV(COM1B1) | _BV(COM1B0); // Inverted/non-inverted mode (AC). 45 | } 46 | 47 | void noToneAC() { 48 | TIMSK1 &= ~_BV(OCIE1A); // Remove the timer interrupt. 49 | TCCR1B = _BV(CS11); // Default clock prescaler of 8. 50 | TCCR1A = _BV(WGM10); // Set to defaults so PWM can work like normal (PWM, phase corrected, 8bit). 51 | PWMT1PORT &= ~_BV(PWMT1AMASK); // Set timer 1 PWM pins to LOW. 52 | PWMT1PORT &= ~_BV(PWMT1BMASK); // Other timer 1 PWM pin also to LOW. 53 | } 54 | 55 | ISR(TIMER1_COMPA_vect) { // Timer interrupt vector. 56 | if (millis() >= _tAC_time) noToneAC(); // Check to see if it's time for the note to end. 57 | } -------------------------------------------------------------------------------- /toneAC.h: -------------------------------------------------------------------------------- 1 | /* --------------------------------------------------------------------------- 2 | toneAC Library - v1.5.0 - 04/27/2019 3 | 4 | AUTHOR/LICENSE: 5 | Created by Tim Eckel - tim@leethost.com 6 | Copyright 2019 License: GNU GPL v3 http://www.gnu.org/licenses/gpl-3.0.html 7 | 8 | LINKS: 9 | Project home: https://github.com/teckel12/arduino-toneac 10 | Blog: http://forum.arduino.cc/index.php?topic=142097.0 11 | 12 | DISCLAIMER: 13 | This software is furnished "as is", without technical support, and with no 14 | warranty, express or implied, as to its usefulness for any purpose. 15 | 16 | PURPOSE: 17 | Replacement to the standard tone library with the advantage of nearly twice 18 | the volume, higher frequencies (even if running at a lower clock speed), 19 | higher quality (less clicking), nearly 1.5k smaller compiled code and less 20 | stress on the speaker. Disadvantages are that it must use certain pins and 21 | it uses two pins instead of one. But, if you're flexible with your pin 22 | choices, this is a great upgrade. It also uses timer 1 instead of timer 2, 23 | which may free up a conflict you have with the tone library. It exclusively 24 | uses port registers for the fastest and smallest code possible. 25 | 26 | USAGE: 27 | Connection is very similar to a piezo or standard speaker. Except, instead 28 | of connecting one speaker wire to ground you connect both speaker wires to 29 | Arduino pins. The pins you connect to are specific, as toneAC lets the 30 | ATmega microcontroller do all the pin timing and switching. This is 31 | important due to the high switching speed possible with toneAC and to make 32 | sure the pins are alyways perfectly out of phase with each other 33 | (push/pull). See the below CONNECTION section for which pins to use for 34 | different Arduinos. Just as usual when connecting a speaker, make sure you 35 | add an inline 100 ohm resistor between one of the pins and the speaker wire. 36 | 37 | CONNECTION: 38 | Pins 9 & 10 - ATmega328, ATmega128, ATmega640, ATmega8, Uno, Leonardo, etc. 39 | Pins 11 & 12 - ATmega2560/2561, ATmega1280/1281, Mega 40 | Pins 12 & 13 - ATmega1284P, ATmega644 41 | Pins 14 & 15 - Teensy 2.0 42 | Pins 25 & 26 - Teensy++ 2.0 43 | 44 | SYNTAX: 45 | toneAC( frequency [, volume [, length [, background ]]] ) - Play a note. 46 | Parameters: 47 | * frequency - Play the specified frequency indefinitely, turn off with noToneAC(). 48 | * volume - [optional] Set a volume level. (default: 10, range: 0 to 10 [0 = off]) 49 | * length - [optional] Set the length to play in milliseconds. (default: 0 [forever], range: 0 to 4294967295 [49.7 days]) 50 | * background - [optional] Play note in background or pause till finished? (default: false, values: true/false) 51 | noToneAC() - Stop playing. 52 | 53 | HISTORY: 54 | 55 | 04/27/2019 v1.5.0 - Moved repo to Github and made toneAC compatible with 56 | Arduino library manager. 57 | 58 | 10/02/2017 v1.4 - Added NOTONEAC and PLAY_FOREVER macros for code clarity 59 | and improved readability. ~Gabriel Staples, www.ElectricRCAircraftGuy.com 60 | 61 | 08/19/2016 v1.3 - Fixed to work with Teensy++ 2.0 and probably the 1.0 62 | model as well. Cleaned up and organized code which resulted in smaller 63 | compiled code size and no longer the need for the TONEAC_TINY switch and 64 | alternate method. 65 | 66 | 01/27/2013 v1.2 - Fixed a counter error which went "over the top" and caused 67 | periods of silence (thanks Krodal). For advanced users needing tight code, 68 | the TONEAC_TINY switch in toneAC.h activates a version of toneAC() that 69 | saves 30-130 bytes depending on controller. With TONEAC_TINY, the syntax is 70 | toneAC_tiny(frequency, length) which plays the note at full volume in the 71 | background. Added support for the ATmega 640, 644, 1281, 1284P and 2561 72 | microcontrollers. 73 | 74 | 01/16/2013 v1.1 - Option to play notes in background, returning control back 75 | to your sketch for processing while note plays (similar to the way the tone 76 | library works). Volume is now linear and in the range from 0-10. Now uses 77 | prescaler 256 instead of 64 for frequencies below 122 Hz so it can go down 78 | to 1 Hz no matter what speed the CPU is clocked at (helpful if using toneAC 79 | to control a two-pin dual LED). 80 | 81 | 01/11/2013 v1.0 - Initial release. 82 | --------------------------------------------------------------------------- */ 83 | 84 | #ifndef toneAC_h 85 | #define toneAC_h 86 | 87 | #if defined(ARDUINO) && ARDUINO >= 100 88 | #include 89 | #else 90 | #include 91 | #endif 92 | 93 | #if defined (__AVR_ATmega32U4__) || defined(__AVR_ATmega640__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega1281__) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega2561__) || defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB1286__) 94 | #define PWMT1AMASK DDB5 95 | #define PWMT1BMASK DDB6 96 | #define PWMT1DREG DDRB 97 | #define PWMT1PORT PORTB 98 | #elif defined(__AVR_ATmega1284P__) || defined(__AVR_ATmega644__) || defined(__AVR_ATmega644P__) 99 | #define PWMT1AMASK DDD4 100 | #define PWMT1BMASK DDD5 101 | #define PWMT1DREG DDRD 102 | #define PWMT1PORT PORTD 103 | #else 104 | #define PWMT1AMASK DDB1 105 | #define PWMT1BMASK DDB2 106 | #define PWMT1DREG DDRB 107 | #define PWMT1PORT PORTB 108 | #endif 109 | 110 | #if defined(__AVR_ATmega8__) || defined(__AVR_ATmega128__) 111 | #define TIMSK1 TIMSK 112 | #endif 113 | 114 | #define NOTONEAC 0 115 | #define PLAY_FOREVER 0 116 | 117 | void toneAC(unsigned long frequency = NOTONEAC, uint8_t volume = 10, unsigned long length = PLAY_FOREVER, uint8_t background = false); 118 | void toneAC_playNote(unsigned long frequency, uint8_t volume); 119 | void noToneAC(); 120 | #endif -------------------------------------------------------------------------------- /utility/direct_pin_read.h: -------------------------------------------------------------------------------- 1 | #ifndef direct_pin_read_h_ 2 | #define direct_pin_read_h_ 3 | 4 | #if defined(__AVR__) 5 | 6 | #define IO_REG_TYPE uint8_t 7 | #define PIN_TO_BASEREG(pin) (portInputRegister(digitalPinToPort(pin))) 8 | #define PIN_TO_BITMASK(pin) (digitalPinToBitMask(pin)) 9 | #define DIRECT_PIN_READ(base, mask) (((*(base)) & (mask)) ? 1 : 0) 10 | 11 | #elif defined(TEENSYDUINO) && (defined(KINETISK) || defined(KINETISL)) 12 | 13 | #define IO_REG_TYPE uint8_t 14 | #define PIN_TO_BASEREG(pin) (portInputRegister(digitalPinToPort(pin))) 15 | #define PIN_TO_BITMASK(pin) (digitalPinToBitMask(pin)) 16 | #define DIRECT_PIN_READ(base, mask) (((*(base)) & (mask)) ? 1 : 0) 17 | 18 | #elif defined(__IMXRT1052__) || defined(__IMXRT1062__) 19 | 20 | #define IO_REG_TYPE uint32_t 21 | #define PIN_TO_BASEREG(pin) (portOutputRegister(pin)) 22 | #define PIN_TO_BITMASK(pin) (digitalPinToBitMask(pin)) 23 | #define DIRECT_PIN_READ(base, mask) (((*(base)) & (mask)) ? 1 : 0) 24 | 25 | #elif defined(__SAM3X8E__) // || defined(ESP8266) 26 | 27 | #define IO_REG_TYPE uint32_t 28 | #define PIN_TO_BASEREG(pin) (portInputRegister(digitalPinToPort(pin))) 29 | #define PIN_TO_BITMASK(pin) (digitalPinToBitMask(pin)) 30 | #define DIRECT_PIN_READ(base, mask) (((*(base)) & (mask)) ? 1 : 0) 31 | 32 | #elif defined(__PIC32MX__) 33 | 34 | #define IO_REG_TYPE uint32_t 35 | #define PIN_TO_BASEREG(pin) (portModeRegister(digitalPinToPort(pin))) 36 | #define PIN_TO_BITMASK(pin) (digitalPinToBitMask(pin)) 37 | #define DIRECT_PIN_READ(base, mask) (((*(base+4)) & (mask)) ? 1 : 0) 38 | 39 | /* ESP8266 v2.0.0 Arduino workaround for bug https://github.com/esp8266/Arduino/issues/1110 */ 40 | #elif defined(ESP8266) 41 | 42 | #define IO_REG_TYPE uint32_t 43 | #define PIN_TO_BASEREG(pin) ((volatile uint32_t *)(0x60000000+(0x318))) 44 | #define PIN_TO_BITMASK(pin) (digitalPinToBitMask(pin)) 45 | #define DIRECT_PIN_READ(base, mask) (((*(base)) & (mask)) ? 1 : 0) 46 | 47 | /* ESP32 Arduino (https://github.com/espressif/arduino-esp32) */ 48 | #elif defined(ESP32) 49 | 50 | #define IO_REG_TYPE uint32_t 51 | #define PIN_TO_BASEREG(pin) (portInputRegister(digitalPinToPort(pin))) 52 | #define PIN_TO_BITMASK(pin) (digitalPinToBitMask(pin)) 53 | #define DIRECT_PIN_READ(base, mask) (((*(base)) & (mask)) ? 1 : 0) 54 | 55 | #elif defined(__SAMD21G18A__) 56 | 57 | #define IO_REG_TYPE uint32_t 58 | #define PIN_TO_BASEREG(pin) portModeRegister(digitalPinToPort(pin)) 59 | #define PIN_TO_BITMASK(pin) (digitalPinToBitMask(pin)) 60 | #define DIRECT_PIN_READ(base, mask) (((*((base)+8)) & (mask)) ? 1 : 0) 61 | 62 | #elif defined(__SAMD51__) 63 | 64 | #define IO_REG_TYPE uint32_t 65 | #define PIN_TO_BASEREG(pin) portInputRegister(digitalPinToPort(pin)) 66 | #define PIN_TO_BITMASK(pin) (digitalPinToBitMask(pin)) 67 | #define DIRECT_PIN_READ(base, mask) (((*(base)) & (mask)) ? 1 : 0) 68 | 69 | #elif defined(RBL_NRF51822) 70 | 71 | #define IO_REG_TYPE uint32_t 72 | #define PIN_TO_BASEREG(pin) (0) 73 | #define PIN_TO_BITMASK(pin) (pin) 74 | #define DIRECT_PIN_READ(base, pin) nrf_gpio_pin_read(pin) 75 | 76 | #elif defined(__arc__) /* Arduino101/Genuino101 specifics */ 77 | 78 | #include "scss_registers.h" 79 | #include "portable.h" 80 | #include "avr/pgmspace.h" 81 | #define GPIO_ID(pin) (g_APinDescription[pin].ulGPIOId) 82 | #define GPIO_TYPE(pin) (g_APinDescription[pin].ulGPIOType) 83 | #define GPIO_BASE(pin) (g_APinDescription[pin].ulGPIOBase) 84 | #define EXT_PORT_OFFSET_SS 0x0A 85 | #define EXT_PORT_OFFSET_SOC 0x50 86 | #define PIN_TO_BASEREG(pin) ((volatile uint32_t *)g_APinDescription[pin].ulGPIOBase) 87 | #define PIN_TO_BITMASK(pin) pin 88 | #define IO_REG_TYPE uint32_t 89 | static inline __attribute__((always_inline)) 90 | IO_REG_TYPE directRead(volatile IO_REG_TYPE *base, IO_REG_TYPE pin) 91 | { 92 | IO_REG_TYPE ret; 93 | if (SS_GPIO == GPIO_TYPE(pin)) { 94 | ret = READ_ARC_REG(((IO_REG_TYPE)base + EXT_PORT_OFFSET_SS)); 95 | } else { 96 | ret = MMIO_REG_VAL_FROM_BASE((IO_REG_TYPE)base, EXT_PORT_OFFSET_SOC); 97 | } 98 | return ((ret >> GPIO_ID(pin)) & 0x01); 99 | } 100 | #define DIRECT_PIN_READ(base, pin) directRead(base, pin) 101 | 102 | #endif 103 | 104 | #endif 105 | -------------------------------------------------------------------------------- /utility/interrupt_config.h: -------------------------------------------------------------------------------- 1 | #if defined(__AVR__) 2 | 3 | #include 4 | #include 5 | 6 | #define attachInterrupt(num, func, mode) enableInterrupt(num) 7 | #if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) 8 | #define SCRAMBLE_INT_ORDER(num) ((num < 4) ? num + 2 : ((num < 6) ? num - 4 : num)) 9 | #define DESCRAMBLE_INT_ORDER(num) ((num < 2) ? num + 4 : ((num < 6) ? num - 2 : num)) 10 | #else 11 | #define SCRAMBLE_INT_ORDER(num) (num) 12 | #define DESCRAMBLE_INT_ORDER(num) (num) 13 | #endif 14 | 15 | static void enableInterrupt(uint8_t num) 16 | { 17 | switch (DESCRAMBLE_INT_ORDER(num)) { 18 | #if defined(EICRA) && defined(EIMSK) 19 | case 0: 20 | EICRA = (EICRA & 0xFC) | 0x01; 21 | EIMSK |= 0x01; 22 | return; 23 | case 1: 24 | EICRA = (EICRA & 0xF3) | 0x04; 25 | EIMSK |= 0x02; 26 | return; 27 | case 2: 28 | EICRA = (EICRA & 0xCF) | 0x10; 29 | EIMSK |= 0x04; 30 | return; 31 | case 3: 32 | EICRA = (EICRA & 0x3F) | 0x40; 33 | EIMSK |= 0x08; 34 | return; 35 | #elif defined(MCUCR) && defined(GICR) 36 | case 0: 37 | MCUCR = (MCUCR & ~((1 << ISC00) | (1 << ISC01))) | (mode << ISC00); 38 | GICR |= (1 << INT0); 39 | return; 40 | case 1: 41 | MCUCR = (MCUCR & ~((1 << ISC10) | (1 << ISC11))) | (mode << ISC10); 42 | GICR |= (1 << INT1); 43 | return; 44 | #elif defined(MCUCR) && defined(GIMSK) 45 | case 0: 46 | MCUCR = (MCUCR & ~((1 << ISC00) | (1 << ISC01))) | (mode << ISC00); 47 | GIMSK |= (1 << INT0); 48 | return; 49 | case 1: 50 | MCUCR = (MCUCR & ~((1 << ISC10) | (1 << ISC11))) | (mode << ISC10); 51 | GIMSK |= (1 << INT1); 52 | return; 53 | #endif 54 | #if defined(EICRB) && defined(EIMSK) 55 | case 4: 56 | EICRB = (EICRB & 0xFC) | 0x01; 57 | EIMSK |= 0x10; 58 | return; 59 | case 5: 60 | EICRB = (EICRB & 0xF3) | 0x04; 61 | EIMSK |= 0x20; 62 | return; 63 | case 6: 64 | EICRB = (EICRB & 0xCF) | 0x10; 65 | EIMSK |= 0x40; 66 | return; 67 | case 7: 68 | EICRB = (EICRB & 0x3F) | 0x40; 69 | EIMSK |= 0x80; 70 | return; 71 | #endif 72 | } 73 | } 74 | 75 | #elif defined(__PIC32MX__) 76 | 77 | #ifdef ENCODER_OPTIMIZE_INTERRUPTS 78 | #undef ENCODER_OPTIMIZE_INTERRUPTS 79 | #endif 80 | 81 | #else 82 | 83 | #ifdef ENCODER_OPTIMIZE_INTERRUPTS 84 | #undef ENCODER_OPTIMIZE_INTERRUPTS 85 | #endif 86 | 87 | #endif 88 | -------------------------------------------------------------------------------- /utility/interrupt_pins.h: -------------------------------------------------------------------------------- 1 | // interrupt pins for known boards 2 | 3 | // Teensy (and maybe others) define these automatically 4 | #if !defined(CORE_NUM_INTERRUPT) 5 | 6 | // Wiring boards 7 | #if defined(WIRING) 8 | #define CORE_NUM_INTERRUPT NUM_EXTERNAL_INTERRUPTS 9 | #if NUM_EXTERNAL_INTERRUPTS > 0 10 | #define CORE_INT0_PIN EI0 11 | #endif 12 | #if NUM_EXTERNAL_INTERRUPTS > 1 13 | #define CORE_INT1_PIN EI1 14 | #endif 15 | #if NUM_EXTERNAL_INTERRUPTS > 2 16 | #define CORE_INT2_PIN EI2 17 | #endif 18 | #if NUM_EXTERNAL_INTERRUPTS > 3 19 | #define CORE_INT3_PIN EI3 20 | #endif 21 | #if NUM_EXTERNAL_INTERRUPTS > 4 22 | #define CORE_INT4_PIN EI4 23 | #endif 24 | #if NUM_EXTERNAL_INTERRUPTS > 5 25 | #define CORE_INT5_PIN EI5 26 | #endif 27 | #if NUM_EXTERNAL_INTERRUPTS > 6 28 | #define CORE_INT6_PIN EI6 29 | #endif 30 | #if NUM_EXTERNAL_INTERRUPTS > 7 31 | #define CORE_INT7_PIN EI7 32 | #endif 33 | 34 | // Arduino Uno, Duemilanove, Diecimila, LilyPad, Mini, Fio, etc... 35 | #elif defined(__AVR_ATmega328P__) || defined(__AVR_ATmega328PB__) ||defined(__AVR_ATmega168__) || defined(__AVR_ATmega8__) 36 | #define CORE_NUM_INTERRUPT 2 37 | #define CORE_INT0_PIN 2 38 | #define CORE_INT1_PIN 3 39 | 40 | // Arduino Mega 41 | #elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) 42 | #define CORE_NUM_INTERRUPT 6 43 | #define CORE_INT0_PIN 2 44 | #define CORE_INT1_PIN 3 45 | #define CORE_INT2_PIN 21 46 | #define CORE_INT3_PIN 20 47 | #define CORE_INT4_PIN 19 48 | #define CORE_INT5_PIN 18 49 | 50 | // Arduino Nano Every, Uno R2 Wifi 51 | #elif defined(__AVR_ATmega4809__) 52 | #define CORE_NUM_INTERRUPT 22 53 | #define CORE_INT0_PIN 0 54 | #define CORE_INT1_PIN 1 55 | #define CORE_INT2_PIN 2 56 | #define CORE_INT3_PIN 3 57 | #define CORE_INT4_PIN 4 58 | #define CORE_INT5_PIN 5 59 | #define CORE_INT6_PIN 6 60 | #define CORE_INT7_PIN 7 61 | #define CORE_INT8_PIN 8 62 | #define CORE_INT9_PIN 9 63 | #define CORE_INT10_PIN 10 64 | #define CORE_INT11_PIN 11 65 | #define CORE_INT12_PIN 12 66 | #define CORE_INT13_PIN 13 67 | #define CORE_INT14_PIN 14 68 | #define CORE_INT15_PIN 15 69 | #define CORE_INT16_PIN 16 70 | #define CORE_INT17_PIN 17 71 | #define CORE_INT18_PIN 18 72 | #define CORE_INT19_PIN 19 73 | #define CORE_INT20_PIN 20 74 | #define CORE_INT21_PIN 21 75 | 76 | // Arduino Leonardo (untested) 77 | #elif defined(__AVR_ATmega32U4__) && !defined(CORE_TEENSY) 78 | #define CORE_NUM_INTERRUPT 5 79 | #define CORE_INT0_PIN 3 80 | #define CORE_INT1_PIN 2 81 | #define CORE_INT2_PIN 0 82 | #define CORE_INT3_PIN 1 83 | #define CORE_INT4_PIN 7 84 | 85 | // Sanguino (untested) and ATmega1284P 86 | #elif defined(__AVR_ATmega644P__) || defined(__AVR_ATmega644__) || defined(__AVR_ATmega1284P__) 87 | #define CORE_NUM_INTERRUPT 3 88 | #define CORE_INT0_PIN 10 89 | #define CORE_INT1_PIN 11 90 | #define CORE_INT2_PIN 2 91 | 92 | // ATmega32u2 and ATmega32u16 based boards with HoodLoader2 93 | #elif defined(__AVR_ATmega32U2__) || defined(__AVR_ATmega16U2__) 94 | #define CORE_NUM_INTERRUPT 8 95 | #define CORE_INT0_PIN 8 96 | #define CORE_INT1_PIN 17 97 | #define CORE_INT2_PIN 13 98 | #define CORE_INT3_PIN 14 99 | #define CORE_INT4_PIN 15 100 | #define CORE_INT5_PIN 16 101 | #define CORE_INT6_PIN 19 102 | #define CORE_INT7_PIN 20 103 | 104 | // Chipkit Uno32 - attachInterrupt may not support CHANGE option 105 | #elif defined(__PIC32MX__) && defined(_BOARD_UNO_) 106 | #define CORE_NUM_INTERRUPT 5 107 | #define CORE_INT0_PIN 38 108 | #define CORE_INT1_PIN 2 109 | #define CORE_INT2_PIN 7 110 | #define CORE_INT3_PIN 8 111 | #define CORE_INT4_PIN 35 112 | 113 | // Chipkit Uno32 - attachInterrupt may not support CHANGE option 114 | #elif defined(__PIC32MX__) && defined(_BOARD_MEGA_) 115 | #define CORE_NUM_INTERRUPT 5 116 | #define CORE_INT0_PIN 3 117 | #define CORE_INT1_PIN 2 118 | #define CORE_INT2_PIN 7 119 | #define CORE_INT3_PIN 21 120 | #define CORE_INT4_PIN 20 121 | 122 | // http://hlt.media.mit.edu/?p=1229 123 | #elif defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__) 124 | #define CORE_NUM_INTERRUPT 1 125 | #define CORE_INT0_PIN 2 126 | 127 | // ATtiny441 ATtiny841 128 | #elif defined(__AVR_ATtiny441__) || defined(__AVR_ATtiny841__) 129 | #define CORE_NUM_INTERRUPT 1 130 | #define CORE_INT0_PIN 9 131 | 132 | //https://github.com/SpenceKonde/ATTinyCore/blob/master/avr/extras/ATtiny_x313.md 133 | #elif defined(__AVR_ATtinyX313__) 134 | #define CORE_NUM_INTERRUPT 2 135 | #define CORE_INT0_PIN 4 136 | #define CORE_INT1_PIN 5 137 | 138 | // Attiny167 same core as abobe 139 | #elif defined(__AVR_ATtiny167__) 140 | #define CORE_NUM_INTERRUPT 2 141 | #define CORE_INT0_PIN 14 142 | #define CORE_INT1_PIN 3 143 | 144 | 145 | // Arduino Due 146 | #elif defined(__SAM3X8E__) 147 | #define CORE_NUM_INTERRUPT 54 148 | #define CORE_INT0_PIN 0 149 | #define CORE_INT1_PIN 1 150 | #define CORE_INT2_PIN 2 151 | #define CORE_INT3_PIN 3 152 | #define CORE_INT4_PIN 4 153 | #define CORE_INT5_PIN 5 154 | #define CORE_INT6_PIN 6 155 | #define CORE_INT7_PIN 7 156 | #define CORE_INT8_PIN 8 157 | #define CORE_INT9_PIN 9 158 | #define CORE_INT10_PIN 10 159 | #define CORE_INT11_PIN 11 160 | #define CORE_INT12_PIN 12 161 | #define CORE_INT13_PIN 13 162 | #define CORE_INT14_PIN 14 163 | #define CORE_INT15_PIN 15 164 | #define CORE_INT16_PIN 16 165 | #define CORE_INT17_PIN 17 166 | #define CORE_INT18_PIN 18 167 | #define CORE_INT19_PIN 19 168 | #define CORE_INT20_PIN 20 169 | #define CORE_INT21_PIN 21 170 | #define CORE_INT22_PIN 22 171 | #define CORE_INT23_PIN 23 172 | #define CORE_INT24_PIN 24 173 | #define CORE_INT25_PIN 25 174 | #define CORE_INT26_PIN 26 175 | #define CORE_INT27_PIN 27 176 | #define CORE_INT28_PIN 28 177 | #define CORE_INT29_PIN 29 178 | #define CORE_INT30_PIN 30 179 | #define CORE_INT31_PIN 31 180 | #define CORE_INT32_PIN 32 181 | #define CORE_INT33_PIN 33 182 | #define CORE_INT34_PIN 34 183 | #define CORE_INT35_PIN 35 184 | #define CORE_INT36_PIN 36 185 | #define CORE_INT37_PIN 37 186 | #define CORE_INT38_PIN 38 187 | #define CORE_INT39_PIN 39 188 | #define CORE_INT40_PIN 40 189 | #define CORE_INT41_PIN 41 190 | #define CORE_INT42_PIN 42 191 | #define CORE_INT43_PIN 43 192 | #define CORE_INT44_PIN 44 193 | #define CORE_INT45_PIN 45 194 | #define CORE_INT46_PIN 46 195 | #define CORE_INT47_PIN 47 196 | #define CORE_INT48_PIN 48 197 | #define CORE_INT49_PIN 49 198 | #define CORE_INT50_PIN 50 199 | #define CORE_INT51_PIN 51 200 | #define CORE_INT52_PIN 52 201 | #define CORE_INT53_PIN 53 202 | 203 | // ESP8266 (https://github.com/esp8266/Arduino/) 204 | #elif defined(ESP8266) 205 | #define CORE_NUM_INTERRUPT EXTERNAL_NUM_INTERRUPTS 206 | #define CORE_INT0_PIN 0 207 | #define CORE_INT1_PIN 1 208 | #define CORE_INT2_PIN 2 209 | #define CORE_INT3_PIN 3 210 | #define CORE_INT4_PIN 4 211 | #define CORE_INT5_PIN 5 212 | // GPIO6-GPIO11 are typically used to interface with the flash memory IC on 213 | // most esp8266 modules, so we should avoid adding interrupts to these pins. 214 | #define CORE_INT12_PIN 12 215 | #define CORE_INT13_PIN 13 216 | #define CORE_INT14_PIN 14 217 | #define CORE_INT15_PIN 15 218 | 219 | // ESP32 (https://github.com/espressif/arduino-esp32) 220 | #elif defined(ESP32) 221 | 222 | #define CORE_NUM_INTERRUPT 40 223 | #define CORE_INT0_PIN 0 224 | #define CORE_INT1_PIN 1 225 | #define CORE_INT2_PIN 2 226 | #define CORE_INT3_PIN 3 227 | #define CORE_INT4_PIN 4 228 | #define CORE_INT5_PIN 5 229 | // GPIO6-GPIO11 are typically used to interface with the flash memory IC on 230 | // esp32, so we should avoid adding interrupts to these pins. 231 | #define CORE_INT12_PIN 12 232 | #define CORE_INT13_PIN 13 233 | #define CORE_INT14_PIN 14 234 | #define CORE_INT15_PIN 15 235 | #define CORE_INT16_PIN 16 236 | #define CORE_INT17_PIN 17 237 | #define CORE_INT18_PIN 18 238 | #define CORE_INT19_PIN 19 239 | #define CORE_INT21_PIN 21 240 | #define CORE_INT22_PIN 22 241 | #define CORE_INT23_PIN 23 242 | #define CORE_INT25_PIN 25 243 | #define CORE_INT26_PIN 26 244 | #define CORE_INT27_PIN 27 245 | #define CORE_INT32_PIN 32 246 | #define CORE_INT33_PIN 33 247 | #define CORE_INT34_PIN 34 248 | #define CORE_INT35_PIN 35 249 | #define CORE_INT36_PIN 36 250 | #define CORE_INT39_PIN 39 251 | 252 | 253 | // Arduino Zero - TODO: interrupts do not seem to work 254 | // please help, contribute a fix! 255 | #elif defined(__SAMD21G18A__) 256 | #define CORE_NUM_INTERRUPT 31 257 | #define CORE_INT0_PIN 0 258 | #define CORE_INT1_PIN 1 259 | #define CORE_INT2_PIN 2 260 | #define CORE_INT3_PIN 3 261 | #define CORE_INT4_PIN 4 262 | #define CORE_INT5_PIN 5 263 | #define CORE_INT6_PIN 6 264 | #define CORE_INT7_PIN 7 265 | #define CORE_INT8_PIN 8 266 | #define CORE_INT9_PIN 9 267 | #define CORE_INT10_PIN 10 268 | #define CORE_INT11_PIN 11 269 | #define CORE_INT12_PIN 12 270 | #define CORE_INT13_PIN 13 271 | #define CORE_INT14_PIN 14 272 | #define CORE_INT15_PIN 15 273 | #define CORE_INT16_PIN 16 274 | #define CORE_INT17_PIN 17 275 | #define CORE_INT18_PIN 18 276 | #define CORE_INT19_PIN 19 277 | #define CORE_INT20_PIN 20 278 | #define CORE_INT21_PIN 21 279 | #define CORE_INT22_PIN 22 280 | #define CORE_INT23_PIN 23 281 | #define CORE_INT24_PIN 24 282 | #define CORE_INT25_PIN 25 283 | #define CORE_INT26_PIN 26 284 | #define CORE_INT27_PIN 27 285 | #define CORE_INT28_PIN 28 286 | #define CORE_INT29_PIN 29 287 | #define CORE_INT30_PIN 30 288 | 289 | #elif defined(__SAMD51__) 290 | #define CORE_NUM_INTERRUPT 26 291 | #define CORE_INT0_PIN 0 292 | #define CORE_INT1_PIN 1 293 | #define CORE_INT2_PIN 2 294 | #define CORE_INT3_PIN 3 295 | #define CORE_INT4_PIN 4 296 | #define CORE_INT5_PIN 5 297 | #define CORE_INT6_PIN 6 298 | #define CORE_INT7_PIN 7 299 | #define CORE_INT8_PIN 8 300 | #define CORE_INT9_PIN 9 301 | #define CORE_INT10_PIN 10 302 | #define CORE_INT11_PIN 11 303 | #define CORE_INT12_PIN 12 304 | #define CORE_INT13_PIN 13 305 | #define CORE_INT14_PIN 14 306 | #define CORE_INT15_PIN 15 307 | #define CORE_INT16_PIN 16 308 | #define CORE_INT17_PIN 17 309 | #define CORE_INT18_PIN 18 310 | #define CORE_INT19_PIN 19 311 | #define CORE_INT20_PIN 20 312 | #define CORE_INT21_PIN 21 313 | #define CORE_INT22_PIN 22 314 | #define CORE_INT23_PIN 23 315 | #define CORE_INT24_PIN 24 316 | #define CORE_INT25_PIN 25 317 | 318 | // Arduino 101 319 | #elif defined(__arc__) 320 | #define CORE_NUM_INTERRUPT 14 321 | #define CORE_INT2_PIN 2 322 | #define CORE_INT5_PIN 5 323 | #define CORE_INT7_PIN 7 324 | #define CORE_INT8_PIN 8 325 | #define CORE_INT10_PIN 10 326 | #define CORE_INT11_PIN 11 327 | #define CORE_INT12_PIN 12 328 | #define CORE_INT13_PIN 13 329 | 330 | #endif 331 | #endif 332 | 333 | #if !defined(CORE_NUM_INTERRUPT) 334 | #error "Interrupts are unknown for this board, please add to this code" 335 | #endif 336 | #if CORE_NUM_INTERRUPT <= 0 337 | #error "Encoder requires interrupt pins, but this board does not have any :(" 338 | #error "You could try defining ENCODER_DO_NOT_USE_INTERRUPTS as a kludge." 339 | #endif 340 | 341 | --------------------------------------------------------------------------------