32 #if defined(ARDUINO) && ARDUINO >= 100 38 #include "pins_arduino.h" 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 47 #ifdef ENCODER_OPTIMIZE_INTERRUPTS 48 #include "interrupt_config.h" 51 #define ENCODER_ARGLIST_SIZE 0 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;
74 pinMode(pin1, INPUT_PULLUP);
75 pinMode(pin2, INPUT_PULLUP);
78 digitalWrite(pin1, HIGH);
80 digitalWrite(pin2, HIGH);
90 delayMicroseconds(2000);
95 #ifdef ENCODER_USE_INTERRUPTS 103 #ifdef ENCODER_USE_INTERRUPTS 133 inline int32_t
read() {
143 inline void write(int32_t p) {
149 #ifdef ENCODER_USE_INTERRUPTS 226 "and r24, r30" "\n\t" 229 "L%=1:" "and r25, r31" "\n\t" 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" 249 "rjmp L%=plus1" "\n\t" 250 "rjmp L%=minus1" "\n\t" 251 "rjmp L%=plus2" "\n\t" 252 "rjmp L%=minus1" "\n\t" 254 "rjmp L%=minus2" "\n\t" 255 "rjmp L%=plus1" "\n\t" 256 "rjmp L%=plus1" "\n\t" 257 "rjmp L%=minus2" "\n\t" 259 "rjmp L%=minus1" "\n\t" 260 "rjmp L%=plus2" "\n\t" 261 "rjmp L%=minus1" "\n\t" 262 "rjmp L%=plus1" "\n\t" 269 "rjmp L%=store" "\n\t" 275 "rjmp L%=store" "\n\t" 277 "subi r22, 254" "\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" 290 : :
"x" (arg) :
"r22",
"r23",
"r24",
"r25",
"r30",
"r31");
294 uint8_t state = arg->
state & 3;
295 if (p1val) state |= 4;
296 if (p2val) state |= 8;
297 arg->
state = (state >> 2);
299 case 1:
case 7:
case 8:
case 14:
302 case 2:
case 4:
case 11:
case 13:
375 #ifdef ENCODER_USE_INTERRUPTS 383 interruptArgs[0] = state;
384 attachInterrupt(0, isr0, CHANGE);
389 interruptArgs[1] = state;
390 attachInterrupt(1, isr1, CHANGE);
395 interruptArgs[2] = state;
396 attachInterrupt(2, isr2, CHANGE);
401 interruptArgs[3] = state;
402 attachInterrupt(3, isr3, CHANGE);
407 interruptArgs[4] = state;
408 attachInterrupt(4, isr4, CHANGE);
413 interruptArgs[5] = state;
414 attachInterrupt(5, isr5, CHANGE);
419 interruptArgs[6] = state;
420 attachInterrupt(6, isr6, CHANGE);
425 interruptArgs[7] = state;
426 attachInterrupt(7, isr7, CHANGE);
431 interruptArgs[8] = state;
432 attachInterrupt(8, isr8, CHANGE);
437 interruptArgs[9] = state;
438 attachInterrupt(9, isr9, CHANGE);
441 #ifdef CORE_INT10_PIN 443 interruptArgs[10] = state;
444 attachInterrupt(10, isr10, CHANGE);
447 #ifdef CORE_INT11_PIN 449 interruptArgs[11] = state;
450 attachInterrupt(11, isr11, CHANGE);
453 #ifdef CORE_INT12_PIN 455 interruptArgs[12] = state;
456 attachInterrupt(12, isr12, CHANGE);
459 #ifdef CORE_INT13_PIN 461 interruptArgs[13] = state;
462 attachInterrupt(13, isr13, CHANGE);
465 #ifdef CORE_INT14_PIN 467 interruptArgs[14] = state;
468 attachInterrupt(14, isr14, CHANGE);
471 #ifdef CORE_INT15_PIN 473 interruptArgs[15] = state;
474 attachInterrupt(15, isr15, CHANGE);
477 #ifdef CORE_INT16_PIN 479 interruptArgs[16] = state;
480 attachInterrupt(16, isr16, CHANGE);
483 #ifdef CORE_INT17_PIN 485 interruptArgs[17] = state;
486 attachInterrupt(17, isr17, CHANGE);
489 #ifdef CORE_INT18_PIN 491 interruptArgs[18] = state;
492 attachInterrupt(18, isr18, CHANGE);
495 #ifdef CORE_INT19_PIN 497 interruptArgs[19] = state;
498 attachInterrupt(19, isr19, CHANGE);
501 #ifdef CORE_INT20_PIN 503 interruptArgs[20] = state;
504 attachInterrupt(20, isr20, CHANGE);
507 #ifdef CORE_INT21_PIN 509 interruptArgs[21] = state;
510 attachInterrupt(21, isr21, CHANGE);
513 #ifdef CORE_INT22_PIN 515 interruptArgs[22] = state;
516 attachInterrupt(22, isr22, CHANGE);
519 #ifdef CORE_INT23_PIN 521 interruptArgs[23] = state;
522 attachInterrupt(23, isr23, CHANGE);
525 #ifdef CORE_INT24_PIN 527 interruptArgs[24] = state;
528 attachInterrupt(24, isr24, CHANGE);
531 #ifdef CORE_INT25_PIN 533 interruptArgs[25] = state;
534 attachInterrupt(25, isr25, CHANGE);
537 #ifdef CORE_INT26_PIN 539 interruptArgs[26] = state;
540 attachInterrupt(26, isr26, CHANGE);
543 #ifdef CORE_INT27_PIN 545 interruptArgs[27] = state;
546 attachInterrupt(27, isr27, CHANGE);
549 #ifdef CORE_INT28_PIN 551 interruptArgs[28] = state;
552 attachInterrupt(28, isr28, CHANGE);
555 #ifdef CORE_INT29_PIN 557 interruptArgs[29] = state;
558 attachInterrupt(29, isr29, CHANGE);
562 #ifdef CORE_INT30_PIN 564 interruptArgs[30] = state;
565 attachInterrupt(30, isr30, CHANGE);
568 #ifdef CORE_INT31_PIN 570 interruptArgs[31] = state;
571 attachInterrupt(31, isr31, CHANGE);
574 #ifdef CORE_INT32_PIN 576 interruptArgs[32] = state;
577 attachInterrupt(32, isr32, CHANGE);
580 #ifdef CORE_INT33_PIN 582 interruptArgs[33] = state;
583 attachInterrupt(33, isr33, CHANGE);
586 #ifdef CORE_INT34_PIN 588 interruptArgs[34] = state;
589 attachInterrupt(34, isr34, CHANGE);
592 #ifdef CORE_INT35_PIN 594 interruptArgs[35] = state;
595 attachInterrupt(35, isr35, CHANGE);
598 #ifdef CORE_INT36_PIN 600 interruptArgs[36] = state;
601 attachInterrupt(36, isr36, CHANGE);
604 #ifdef CORE_INT37_PIN 606 interruptArgs[37] = state;
607 attachInterrupt(37, isr37, CHANGE);
610 #ifdef CORE_INT38_PIN 612 interruptArgs[38] = state;
613 attachInterrupt(38, isr38, CHANGE);
616 #ifdef CORE_INT39_PIN 618 interruptArgs[39] = state;
619 attachInterrupt(39, isr39, CHANGE);
622 #ifdef CORE_INT40_PIN 624 interruptArgs[40] = state;
625 attachInterrupt(40, isr40, CHANGE);
628 #ifdef CORE_INT41_PIN 630 interruptArgs[41] = state;
631 attachInterrupt(41, isr41, CHANGE);
634 #ifdef CORE_INT42_PIN 636 interruptArgs[42] = state;
637 attachInterrupt(42, isr42, CHANGE);
640 #ifdef CORE_INT43_PIN 642 interruptArgs[43] = state;
643 attachInterrupt(43, isr43, CHANGE);
646 #ifdef CORE_INT44_PIN 648 interruptArgs[44] = state;
649 attachInterrupt(44, isr44, CHANGE);
652 #ifdef CORE_INT45_PIN 654 interruptArgs[45] = state;
655 attachInterrupt(45, isr45, CHANGE);
658 #ifdef CORE_INT46_PIN 660 interruptArgs[46] = state;
661 attachInterrupt(46, isr46, CHANGE);
664 #ifdef CORE_INT47_PIN 666 interruptArgs[47] = state;
667 attachInterrupt(47, isr47, CHANGE);
670 #ifdef CORE_INT48_PIN 672 interruptArgs[48] = state;
673 attachInterrupt(48, isr48, CHANGE);
676 #ifdef CORE_INT49_PIN 678 interruptArgs[49] = state;
679 attachInterrupt(49, isr49, CHANGE);
682 #ifdef CORE_INT50_PIN 684 interruptArgs[50] = state;
685 attachInterrupt(50, isr50, CHANGE);
688 #ifdef CORE_INT51_PIN 690 interruptArgs[51] = state;
691 attachInterrupt(51, isr51, CHANGE);
694 #ifdef CORE_INT52_PIN 696 interruptArgs[52] = state;
697 attachInterrupt(52, isr52, CHANGE);
700 #ifdef CORE_INT53_PIN 702 interruptArgs[53] = state;
703 attachInterrupt(53, isr53, CHANGE);
706 #ifdef CORE_INT54_PIN 708 interruptArgs[54] = state;
709 attachInterrupt(54, isr54, CHANGE);
712 #ifdef CORE_INT55_PIN 714 interruptArgs[55] = state;
715 attachInterrupt(55, isr55, CHANGE);
718 #ifdef CORE_INT56_PIN 720 interruptArgs[56] = state;
721 attachInterrupt(56, isr56, CHANGE);
724 #ifdef CORE_INT57_PIN 726 interruptArgs[57] = state;
727 attachInterrupt(57, isr57, CHANGE);
730 #ifdef CORE_INT58_PIN 732 interruptArgs[58] = state;
733 attachInterrupt(58, isr58, CHANGE);
736 #ifdef CORE_INT59_PIN 738 interruptArgs[59] = state;
739 attachInterrupt(59, isr59, CHANGE);
747 #endif // ENCODER_USE_INTERRUPTS 750 #if defined(ENCODER_USE_INTERRUPTS) && !defined(ENCODER_OPTIMIZE_INTERRUPTS) 752 static void isr0(
void) {
update(interruptArgs[0]); }
755 static void isr1(
void) {
update(interruptArgs[1]); }
758 static void isr2(
void) {
update(interruptArgs[2]); }
761 static void isr3(
void) {
update(interruptArgs[3]); }
764 static void isr4(
void) {
update(interruptArgs[4]); }
767 static void isr5(
void) {
update(interruptArgs[5]); }
770 static void isr6(
void) {
update(interruptArgs[6]); }
773 static void isr7(
void) {
update(interruptArgs[7]); }
776 static void isr8(
void) {
update(interruptArgs[8]); }
779 static void isr9(
void) {
update(interruptArgs[9]); }
781 #ifdef CORE_INT10_PIN 782 static void isr10(
void) {
update(interruptArgs[10]); }
784 #ifdef CORE_INT11_PIN 785 static void isr11(
void) {
update(interruptArgs[11]); }
787 #ifdef CORE_INT12_PIN 788 static void isr12(
void) {
update(interruptArgs[12]); }
790 #ifdef CORE_INT13_PIN 791 static void isr13(
void) {
update(interruptArgs[13]); }
793 #ifdef CORE_INT14_PIN 794 static void isr14(
void) {
update(interruptArgs[14]); }
796 #ifdef CORE_INT15_PIN 797 static void isr15(
void) {
update(interruptArgs[15]); }
799 #ifdef CORE_INT16_PIN 800 static void isr16(
void) {
update(interruptArgs[16]); }
802 #ifdef CORE_INT17_PIN 803 static void isr17(
void) {
update(interruptArgs[17]); }
805 #ifdef CORE_INT18_PIN 806 static void isr18(
void) {
update(interruptArgs[18]); }
808 #ifdef CORE_INT19_PIN 809 static void isr19(
void) {
update(interruptArgs[19]); }
811 #ifdef CORE_INT20_PIN 812 static void isr20(
void) {
update(interruptArgs[20]); }
814 #ifdef CORE_INT21_PIN 815 static void isr21(
void) {
update(interruptArgs[21]); }
817 #ifdef CORE_INT22_PIN 818 static void isr22(
void) {
update(interruptArgs[22]); }
820 #ifdef CORE_INT23_PIN 821 static void isr23(
void) {
update(interruptArgs[23]); }
823 #ifdef CORE_INT24_PIN 824 static void isr24(
void) {
update(interruptArgs[24]); }
826 #ifdef CORE_INT25_PIN 827 static void isr25(
void) {
update(interruptArgs[25]); }
829 #ifdef CORE_INT26_PIN 830 static void isr26(
void) {
update(interruptArgs[26]); }
832 #ifdef CORE_INT27_PIN 833 static void isr27(
void) {
update(interruptArgs[27]); }
835 #ifdef CORE_INT28_PIN 836 static void isr28(
void) {
update(interruptArgs[28]); }
838 #ifdef CORE_INT29_PIN 839 static void isr29(
void) {
update(interruptArgs[29]); }
841 #ifdef CORE_INT30_PIN 842 static void isr30(
void) {
update(interruptArgs[30]); }
844 #ifdef CORE_INT31_PIN 845 static void isr31(
void) {
update(interruptArgs[31]); }
847 #ifdef CORE_INT32_PIN 848 static void isr32(
void) {
update(interruptArgs[32]); }
850 #ifdef CORE_INT33_PIN 851 static void isr33(
void) {
update(interruptArgs[33]); }
853 #ifdef CORE_INT34_PIN 854 static void isr34(
void) {
update(interruptArgs[34]); }
856 #ifdef CORE_INT35_PIN 857 static void isr35(
void) {
update(interruptArgs[35]); }
859 #ifdef CORE_INT36_PIN 860 static void isr36(
void) {
update(interruptArgs[36]); }
862 #ifdef CORE_INT37_PIN 863 static void isr37(
void) {
update(interruptArgs[37]); }
865 #ifdef CORE_INT38_PIN 866 static void isr38(
void) {
update(interruptArgs[38]); }
868 #ifdef CORE_INT39_PIN 869 static void isr39(
void) {
update(interruptArgs[39]); }
871 #ifdef CORE_INT40_PIN 872 static void isr40(
void) {
update(interruptArgs[40]); }
874 #ifdef CORE_INT41_PIN 875 static void isr41(
void) {
update(interruptArgs[41]); }
877 #ifdef CORE_INT42_PIN 878 static void isr42(
void) {
update(interruptArgs[42]); }
880 #ifdef CORE_INT43_PIN 881 static void isr43(
void) {
update(interruptArgs[43]); }
883 #ifdef CORE_INT44_PIN 884 static void isr44(
void) {
update(interruptArgs[44]); }
886 #ifdef CORE_INT45_PIN 887 static void isr45(
void) {
update(interruptArgs[45]); }
889 #ifdef CORE_INT46_PIN 890 static void isr46(
void) {
update(interruptArgs[46]); }
892 #ifdef CORE_INT47_PIN 893 static void isr47(
void) {
update(interruptArgs[47]); }
895 #ifdef CORE_INT48_PIN 896 static void isr48(
void) {
update(interruptArgs[48]); }
898 #ifdef CORE_INT49_PIN 899 static void isr49(
void) {
update(interruptArgs[49]); }
901 #ifdef CORE_INT50_PIN 902 static void isr50(
void) {
update(interruptArgs[50]); }
904 #ifdef CORE_INT51_PIN 905 static void isr51(
void) {
update(interruptArgs[51]); }
907 #ifdef CORE_INT52_PIN 908 static void isr52(
void) {
update(interruptArgs[52]); }
910 #ifdef CORE_INT53_PIN 911 static void isr53(
void) {
update(interruptArgs[53]); }
913 #ifdef CORE_INT54_PIN 914 static void isr54(
void) {
update(interruptArgs[54]); }
916 #ifdef CORE_INT55_PIN 917 static void isr55(
void) {
update(interruptArgs[55]); }
919 #ifdef CORE_INT56_PIN 920 static void isr56(
void) {
update(interruptArgs[56]); }
922 #ifdef CORE_INT57_PIN 923 static void isr57(
void) {
update(interruptArgs[57]); }
925 #ifdef CORE_INT58_PIN 926 static void isr58(
void) {
update(interruptArgs[58]); }
928 #ifdef CORE_INT59_PIN 929 static void isr59(
void) {
update(interruptArgs[59]); }
934 #if defined(ENCODER_USE_INTERRUPTS) && defined(ENCODER_OPTIMIZE_INTERRUPTS) 936 #if defined(INT0_vect) && CORE_NUM_INTERRUPT > 0 939 #if defined(INT1_vect) && CORE_NUM_INTERRUPT > 1 942 #if defined(INT2_vect) && CORE_NUM_INTERRUPT > 2 945 #if defined(INT3_vect) && CORE_NUM_INTERRUPT > 3 948 #if defined(INT4_vect) && CORE_NUM_INTERRUPT > 4 951 #if defined(INT5_vect) && CORE_NUM_INTERRUPT > 5 954 #if defined(INT6_vect) && CORE_NUM_INTERRUPT > 6 957 #if defined(INT7_vect) && CORE_NUM_INTERRUPT > 7 961 #if defined(attachInterrupt) 964 #undef attachInterrupt 966 #endif // ENCODER_OPTIMIZE_INTERRUPTS
static uint8_t attach_interrupt(uint8_t pin, Encoder_internal_state_t *state)
#define ENCODER_ARGLIST_SIZE
static void update(Encoder_internal_state_t *arg)
Encoder(uint8_t pin1, uint8_t pin2)
static Encoder_internal_state_t * interruptArgs[ENCODER_ARGLIST_SIZE]
uint8_t interrupts_in_use
Encoder_internal_state_t encoder
volatile IO_REG_TYPE * pin2_register
volatile IO_REG_TYPE * pin1_register