top of page

Gemma Pendant Code

Animation Code

  1. // Animation data for Trinket/Gemma + LED matrix backpack jewelry.

  2. // Edit this file to change the animation; it's unlikely you'll need

  3. // to edit the source code.

  4.  

  5. #define REPS 3 // Number of times to repeat the animation loop (1-255)

  6.  

  7. const uint8_t PROGMEM anim[] = {

  8.  

  9. // Animation bitmaps. Each frame of animation MUST contain

  10. // 8 lines of graphics data (there is no error checking for

  11. // length). Each line should be prefixed with the letter 'B',

  12. // followed by exactly 8 binary digits (0 or 1), no more,

  13. // no less (again, no error checking). '0' represents an

  14. // 'off' pixel, '1' an 'on' pixel. End line with a comma.

  15. B00011000, // This is the first frame for alien #1

  16. B00111100, // If you squint you can kind of see the

  17. B01111110, // image in the 0's and 1's.

  18. B11011011,

  19. B11111111,

  20. B00100100,

  21. B01011010,

  22. B10100101,

  23. // The 9th line (required) is the time to display this frame,

  24. // in 1/100ths of a second (e.g. 100 = 1 sec, 25 = 1/4 sec,

  25. // etc.). Range is 0 (no delay) to 255 (2.55 seconds). If

  26. // longer delays are needed, make duplicate frames.

  27. 25, // 0.25 seconds

  28.  

  29. B00011000, // This is the second frame for alien #1

  30. B00111100,

  31. B01111110,

  32. B11011011,

  33. B11111111,

  34. B00100100,

  35. B01011010,

  36. B01000010,

  37. 25, // 0.25 second delay

  38.  

  39. // Frames 3 & 4 for alien #1 are duplicates of frames 1 & 2.

  40. // Rather than list them 'the tall way' again, the lines are merged here...

  41. B00011000, B00111100, B01111110, B11011011, B11111111, B00100100, B01011010, B10100101, 25,

  42. B00011000, B00111100, B01111110, B11011011, B11111111, B00100100, B01011010, B01000010, 25,

  43.  

  44. B00000000, // First frame for alien #2

  45. B00111100,

  46. B01111110,

  47. B11011011,

  48. B11011011,

  49. B01111110,

  50. B00100100,

  51. B11000011,

  52. 25, // 0.25 second delay

  53.  

  54. B00111100, // Second frame for alien #2

  55. B01111110,

  56. B11011011,

  57. B11011011,

  58. B01111110,

  59. B00100100,

  60. B00100100,

  61. B00100100,

  62. 25,

  63.  

  64. // Frames 3 & 4 for alien #2 are duplicates of frames 1 & 2

  65. B00000000, B00111100, B01111110, B11011011, B11011011, B01111110, B00100100, B11000011, 25,

  66. B00111100, B01111110, B11011011, B11011011, B01111110, B00100100, B00100100, B00100100, 25,

  67.  

  68. B00100100, // First frame for alien #3

  69. B00100100,

  70. B01111110,

  71. B11011011,

  72. B11111111,

  73. B11111111,

  74. B10100101,

  75. B00100100,

  76. 25,

  77.  

  78. B00100100, // Second frame for alien #3

  79. B10100101,

  80. B11111111,

  81. B11011011,

  82. B11111111,

  83. B01111110,

  84. B00100100,

  85. B01000010,

  86. 25,

  87.  

  88. // Frames are duplicated as with prior aliens

  89. B00100100, B00100100, B01111110, B11011011, B11111111, B11111111, B10100101, B00100100, 25,

  90. B00100100, B10100101, B11111111, B11011011, B11111111, B01111110, B00100100, B01000010, 25,

  91.  

  92. B00111100, // First frame for alien #4

  93. B01111110,

  94. B00110011,

  95. B01111110,

  96. B00111100,

  97. B00000000,

  98. B00001000,

  99. B00000000,

  100. 12, // ~1/8 second delay

  101.  

  102. B00111100, // Second frame for alien #4

  103. B01111110,

  104. B10011001,

  105. B01111110,

  106. B00111100,

  107. B00000000,

  108. B00001000,

  109. B00001000,

  110. 12,

  111.  

  112. B00111100, // Third frame for alien #4 (NOT a repeat of frame 1)

  113. B01111110,

  114. B11001100,

  115. B01111110,

  116. B00111100,

  117. B00000000,

  118. B00000000,

  119. B00001000,

  120. 12,

  121.  

  122. B00111100, // Fourth frame for alien #4 (NOT a repeat of frame 2)

  123. B01111110,

  124. B01100110,

  125. B01111110,

  126. B00111100,

  127. B00000000,

  128. B00000000,

  129. B00000000,

  130. 12,

  131.  

  132. // Frames 5-8 are duplicates of 1-4, lines merged for brevity

  133. B00111100, B01111110, B00110011, B01111110, B00111100, B00000000, B00001000, B00000000, 12,

  134. B00111100, B01111110, B10011001, B01111110, B00111100, B00000000, B00001000, B00001000, 12,

  135. B00111100, B01111110, B11001100, B01111110, B00111100, B00000000, B00000000, B00001000, 12,

  136. B00111100, B01111110, B01100110, B01111110, B00111100, B00000000, B00000000, B00000000, 12,

  137. };

     

     

     

     

     

     

     

     

     

Source code

  1. // Trinket/Gemma + LED matrix backpack jewelry. Plays animated

  2. // sequence on LED matrix. Press reset button to display again,

  3. // or add optional momentary button between pin #1 and +V.

  4. // THERE IS NO ANIMATION DATA IN THIS SOURCE FILE, you should

  5. // rarely need to change anything here. EDIT anim.h INSTEAD.

  6.  

  7. #define BRIGHTNESS 12 // 0=min, 15=max

  8. #define I2C_ADDR 0x70 // Edit if backpack A0/A1 jumpers set

  9.  

  10. #include <Wire.h>

  11. #include <avr/power.h>

  12. #include <avr/sleep.h>

  13. #include "anim.h" // Animation data is located here

  14.  

  15. static const uint8_t PROGMEM reorder[] = { // Column-reordering table

  16. 0x00,0x40,0x20,0x60,0x10,0x50,0x30,0x70,0x08,0x48,0x28,0x68,0x18,0x58,0x38,0x78,

  17. 0x04,0x44,0x24,0x64,0x14,0x54,0x34,0x74,0x0c,0x4c,0x2c,0x6c,0x1c,0x5c,0x3c,0x7c,

  18. 0x02,0x42,0x22,0x62,0x12,0x52,0x32,0x72,0x0a,0x4a,0x2a,0x6a,0x1a,0x5a,0x3a,0x7a,

  19. 0x06,0x46,0x26,0x66,0x16,0x56,0x36,0x76,0x0e,0x4e,0x2e,0x6e,0x1e,0x5e,0x3e,0x7e,

  20. 0x01,0x41,0x21,0x61,0x11,0x51,0x31,0x71,0x09,0x49,0x29,0x69,0x19,0x59,0x39,0x79,

  21. 0x05,0x45,0x25,0x65,0x15,0x55,0x35,0x75,0x0d,0x4d,0x2d,0x6d,0x1d,0x5d,0x3d,0x7d,

  22. 0x03,0x43,0x23,0x63,0x13,0x53,0x33,0x73,0x0b,0x4b,0x2b,0x6b,0x1b,0x5b,0x3b,0x7b,

  23. 0x07,0x47,0x27,0x67,0x17,0x57,0x37,0x77,0x0f,0x4f,0x2f,0x6f,0x1f,0x5f,0x3f,0x7f,

  24. 0x80,0xc0,0xa0,0xe0,0x90,0xd0,0xb0,0xf0,0x88,0xc8,0xa8,0xe8,0x98,0xd8,0xb8,0xf8,

  25. 0x84,0xc4,0xa4,0xe4,0x94,0xd4,0xb4,0xf4,0x8c,0xcc,0xac,0xec,0x9c,0xdc,0xbc,0xfc,

  26. 0x82,0xc2,0xa2,0xe2,0x92,0xd2,0xb2,0xf2,0x8a,0xca,0xaa,0xea,0x9a,0xda,0xba,0xfa,

  27. 0x86,0xc6,0xa6,0xe6,0x96,0xd6,0xb6,0xf6,0x8e,0xce,0xae,0xee,0x9e,0xde,0xbe,0xfe,

  28. 0x81,0xc1,0xa1,0xe1,0x91,0xd1,0xb1,0xf1,0x89,0xc9,0xa9,0xe9,0x99,0xd9,0xb9,0xf9,

  29. 0x85,0xc5,0xa5,0xe5,0x95,0xd5,0xb5,0xf5,0x8d,0xcd,0xad,0xed,0x9d,0xdd,0xbd,0xfd,

  30. 0x83,0xc3,0xa3,0xe3,0x93,0xd3,0xb3,0xf3,0x8b,0xcb,0xab,0xeb,0x9b,0xdb,0xbb,0xfb,

  31. 0x87,0xc7,0xa7,0xe7,0x97,0xd7,0xb7,0xf7,0x8f,0xcf,0xaf,0xef,0x9f,0xdf,0xbf,0xff };

  32.  

  33. void ledCmd(uint8_t x) { // Issue command to LED backback driver

  34. Wire.beginTransmission(I2C_ADDR);

  35. Wire.write(x);

  36. Wire.endTransmission();

  37. }

  38.  

  39. void clear(void) { // Clear display buffer

  40. Wire.beginTransmission(I2C_ADDR);

  41. for(uint8_t i=0; i<17; i++) Wire.write(0);

  42. Wire.endTransmission();

  43. }

  44.  

  45. void setup() {

  46. power_timer1_disable(); // Disable unused peripherals

  47. power_adc_disable(); // to save power

  48. PCMSK |= _BV(PCINT1); // Set change mask for pin 1

  49. Wire.begin(); // I2C init

  50. clear(); // Blank display

  51. ledCmd(0x21); // Turn on oscillator

  52. ledCmd(0xE0 | BRIGHTNESS); // Set brightness

  53. ledCmd(0x81); // Display on, no blink

  54. }

  55.  

  56. uint8_t rep = REPS;

  57.  

  58. void loop() {

  59.  

  60. for(int i=0; i<sizeof(anim); i) { // For each frame...

  61. Wire.beginTransmission(I2C_ADDR);

  62. Wire.write(0); // Start address

  63. for(uint8_t j=0; j<8; j++) { // 8 rows...

  64. Wire.write(pgm_read_byte(&reorder[pgm_read_byte(&anim[i++])]));

  65. Wire.write(0);

  66. }

  67. Wire.endTransmission();

  68. delay(pgm_read_byte(&anim[i++]) * 10);

  69. }

  70.  

  71. if(!--rep) { // If last cycle...

  72. ledCmd(0x20); // LED matrix in standby mode

  73. GIMSK = _BV(PCIE); // Enable pin change interrupt

  74. power_all_disable(); // All peripherals off

  75. set_sleep_mode(SLEEP_MODE_PWR_DOWN);

  76. sleep_enable();

  77. sei(); // Keep interrupts disabled

  78. sleep_mode(); // Power down CPU (pin 1 will wake)

  79. // Execution resumes here on wake.

  80. GIMSK = 0; // Disable pin change interrupt

  81. rep = REPS; // Reset animation counter

  82. power_timer0_enable(); // Re-enable timer

  83. power_usi_enable(); // Re-enable USI

  84. Wire.begin(); // Re-init I2C

  85. clear(); // Blank display

  86. ledCmd(0x21); // Re-enable matrix

  87. }

  88. }

  89.  

  90. ISR(PCINT0_vect) {} // Button tap

     

     

     

     

     

     

     

     

     

bottom of page