|
| 1 | +/* -*- mode: c; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; -*- |
| 2 | +
|
| 3 | + G35: An Arduino library for GE Color Effects G-35 holiday lights. |
| 4 | + Copyright © 2012 The G35 Authors. Use, modification, and distribution are |
| 5 | + subject to the BSD license as described in the accompanying LICENSE file. |
| 6 | +
|
| 7 | + TiconXmas2012.ino is the code that I used for Christmas 2012 at my home. |
| 8 | + It's yet another demonstration of working code. |
| 9 | +
|
| 10 | + By Mike Tsao <github.com/sowbug>. See README for complete attributions. |
| 11 | +*/ |
| 12 | + |
| 13 | +#include <G35String.h> |
| 14 | +#include <G35StringGroup.h> |
| 15 | +#include <ProgramRunner.h> |
| 16 | +#include <StockPrograms.h> |
| 17 | +#include <PlusPrograms.h> |
| 18 | +#include <MEOPrograms.h> |
| 19 | + |
| 20 | +// Arduino with Like A G35 shield (https://github.com/sowbug/like-a-g35). |
| 21 | +// First string on pin 8. Second on 9. My roofline isn't quite long enough |
| 22 | +// for 100 lights, so I have the final 9 hidden. |
| 23 | +G35String lights_1(8, 50, 50, 0, false); |
| 24 | +G35String lights_2(9, 41); |
| 25 | + |
| 26 | +const int PROGRAM_COUNT = StockProgramGroup::ProgramCount + |
| 27 | + PlusProgramGroup::ProgramCount + MEOProgramGroup::ProgramCount; |
| 28 | + |
| 29 | +StockProgramGroup stock_programs; |
| 30 | +PlusProgramGroup plus_programs; |
| 31 | +MEOProgramGroup meo_programs; |
| 32 | +G35StringGroup string_group; |
| 33 | + |
| 34 | +LightProgram* CreateProgram(uint8_t program_index) { |
| 35 | + program_index = random() % PROGRAM_COUNT; |
| 36 | + |
| 37 | + if (program_index < StockProgramGroup::ProgramCount) { |
| 38 | + return stock_programs.CreateProgram(string_group, program_index); |
| 39 | + } |
| 40 | + program_index -= StockProgramGroup::ProgramCount; |
| 41 | + |
| 42 | + if (program_index < PlusProgramGroup::ProgramCount) { |
| 43 | + return plus_programs.CreateProgram(string_group, program_index); |
| 44 | + } |
| 45 | + program_index -= PlusProgramGroup::ProgramCount; |
| 46 | + |
| 47 | + if (program_index < MEOProgramGroup::ProgramCount) { |
| 48 | + return meo_programs.CreateProgram(string_group, program_index); |
| 49 | + } |
| 50 | + program_index -= MEOProgramGroup::ProgramCount; |
| 51 | + |
| 52 | + return NULL; |
| 53 | +} |
| 54 | + |
| 55 | +// How long each program should run. |
| 56 | +#define PROGRAM_DURATION_SECONDS (90) |
| 57 | + |
| 58 | +ProgramRunner runner(CreateProgram, PROGRAM_COUNT, PROGRAM_DURATION_SECONDS); |
| 59 | + |
| 60 | +// http://www.utopiamechanicus.com/77/better-arduino-random-numbers/ |
| 61 | +// We assume A0 and A1 are disconnected. |
| 62 | +uint32_t seedOut(unsigned int noOfBits) { |
| 63 | + uint32_t seed = 0, limit = 99; |
| 64 | + int bit0 = 0, bit1 = 0; |
| 65 | + while (noOfBits--) { |
| 66 | + for (int i = 0; i < limit; ++i) { |
| 67 | + bit0 = analogRead(0) & 1; |
| 68 | + bit1 = analogRead(1) & 1; |
| 69 | + if (bit1 != bit0) |
| 70 | + break; |
| 71 | + } |
| 72 | + seed = (seed << 1) | bit1; |
| 73 | + } |
| 74 | + return seed; |
| 75 | +} |
| 76 | + |
| 77 | +void setup() { |
| 78 | + uint32_t seed = seedOut(32); |
| 79 | + randomSeed(seed); |
| 80 | + seed &= 0xff; |
| 81 | + // random() isn't very random. But this seed generator works quite well. |
| 82 | + while (seed--) { |
| 83 | + random(); |
| 84 | + } |
| 85 | + |
| 86 | + delay(50); |
| 87 | + lights_1.enumerate(); |
| 88 | + lights_2.enumerate(); |
| 89 | + |
| 90 | + lights_1.do_test_patterns(); |
| 91 | + lights_2.do_test_patterns(); |
| 92 | + |
| 93 | + string_group.AddString(&lights_1); |
| 94 | + string_group.AddString(&lights_2); |
| 95 | +} |
| 96 | + |
| 97 | +void loop() { |
| 98 | + runner.loop(); |
| 99 | +} |
0 commit comments