code.py
 1  # SPDX-FileCopyrightText: 2018 Phillip Burgess for Adafruit Industries
 2  #
 3  # SPDX-License-Identifier: MIT
 4  
 5  """Jack-o'-Lantern flame example Adafruit Circuit Playground Express"""
 6  
 7  import math
 8  import board
 9  import neopixel
10  try:
11      import urandom as random  # for v1.0 API support
12  except ImportError:
13      import random
14  
15  NUMPIX = 10        # Number of NeoPixels
16  PIXPIN = board.D8  # Pin where NeoPixels are connected
17  STRIP = neopixel.NeoPixel(PIXPIN, NUMPIX, brightness=1.0)
18  PREV = 128
19  
20  def split(first, second, offset):
21      """
22      Subdivide a brightness range, introducing a random offset in middle,
23      then call recursively with smaller offsets along the way.
24      @param1 first:  Initial brightness value.
25      @param1 second: Ending brightness value.
26      @param1 offset: Midpoint offset range is +/- this amount max.
27      """
28      if offset != 0:
29          mid = ((first + second + 1) / 2 + random.randint(-offset, offset))
30          offset = int(offset / 2)
31          split(first, mid, offset)
32          split(mid, second, offset)
33      else:
34          level = math.pow(first / 255.0, 2.7) * 255.0 + 0.5
35          STRIP.fill((int(level), int(level / 8), int(level / 48)))
36          STRIP.write()
37  
38  while True:  # Loop forever...
39      LVL = random.randint(64, 191)
40      split(PREV, LVL, 32)
41      PREV = LVL