<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Cradicle Explorer</title>
    <link href="/css/bootstrap/bootstrap.min.css" rel="stylesheet">
    <style>
      .form-control-dark::placeholder {
          color: #aaa;
          opacity: 1;
      }
    </style>
    <link rel="stylesheet" href="/assets/fontawesome/css/all.min.css">
    <link rel="icon" type="image/png" href="/favicon.png">


                <link href="/css/dashboard.css" rel="stylesheet">
                </head>
                <body>
                <header class="navbar navbar-dark sticky-top bg-dark flex-md-nowrap p-0 shadow">
                  <a class="navbar-brand col-md-3 col-lg-2 me-0 px-3 fs-6" href="/">Cradicle Explorer</a>
                  <button class="navbar-toggler position-absolute d-md-none collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#sidebarMenu" aria-controls="sidebarMenu" aria-expanded="false" aria-label="Toggle navigation">
                    <span class="navbar-toggler-icon"></span>
                  </button>
                  <form method="get" action="/cgi-bin/main" style="width:100%;"><input class="form-control form-control-dark w-100 rounded-0 border-0" type="text" name="q" placeholder="Search repos" aria-label="Search"></form>
                  <div class="navbar-nav flex-row">
                    <div class="nav-item text-nowrap">
                      <a class="nav-link px-3 active" href="/cgi-bin/repo?id=z2fceU8dk9iFCyyT9YMdGnEyxgxw5">Adafruit_CircuitPython_CLUE.git</a>
                    </div>
                  </div>
                </header>
                <div class="container-fluid">
                  <div class="row">
                    <nav id="sidebarMenu" class="col-md-3 col-lg-2 d-md-block bg-dark sidebar collapse">
                      <div class="position-sticky pt-3 sidebar-sticky">
                        <ul class="nav flex-column">
                          <li class="nav-item">
                            <a class="nav-link" href="/cgi-bin/repo?id=z2fceU8dk9iFCyyT9YMdGnEyxgxw5">
                              <i class="align-text-bottom fa-solid fa-info"></i>
                              Info
                            </a>
                          </li>
                          <li class="nav-item">
                            <a class="nav-link" href="/cgi-bin/repo?id=z2fceU8dk9iFCyyT9YMdGnEyxgxw5&issue=list">
                              <i class="align-text-bottom fa-solid fa-layer-group"></i>
                              Issues
                            </a>
                          </li>
                          <li class="nav-item">
                            <a class="nav-link" href="/cgi-bin/repo?id=z2fceU8dk9iFCyyT9YMdGnEyxgxw5&patch=list">
                              <i class="align-text-bottom fa-solid fa-vest-patches"></i>
                              Patches
                            </a>
                          </li>
                          <li class="nav-item">
                            <a class="nav-link" href="/cgi-bin/repo?id=z2fceU8dk9iFCyyT9YMdGnEyxgxw5&wallet=list">
                              <i class="align-text-bottom fa-solid fa-wallet"></i>
                              Wallets
                            </a>
                          </li>
                          <li class="nav-item">
                            <a class="nav-link active" href="/cgi-bin/repo?id=z2fceU8dk9iFCyyT9YMdGnEyxgxw5&source=.">
                              <i class="align-text-bottom fa-solid fa-code"></i>
                              Source
                            </a>
                          </li>
                        <h6 class="sidebar-heading d-flex justify-content-between align-items-center px-3 mt-4 mb-1 text-muted text-uppercase">
                          <span></span>
                        </h6>
                        <ul class="nav flex-column mb-2">
                        
    <h6 class="sidebar-heading d-flex justify-content-between align-items-center px-3 mt-1 mb-1 text-muted text-uppercase">
      <span>Source</span>
    </h6>
    <li><a class="nav-link py-0" style="padding-left:16px;" href="/cgi-bin/repo?id=z2fceU8dk9iFCyyT9YMdGnEyxgxw5&source=.github"><i class="fa-solid fa-folder" style="color:#f0c040;"></i> .github</a></li><li><a class="nav-link py-0" style="padding-left:16px;" href="/cgi-bin/repo?id=z2fceU8dk9iFCyyT9YMdGnEyxgxw5&source=docs"><i class="fa-solid fa-folder" style="color:#f0c040;"></i> docs</a></li><li><a class="nav-link py-0" style="padding-left:16px;" href="/cgi-bin/repo?id=z2fceU8dk9iFCyyT9YMdGnEyxgxw5&source=examples"><i class="fa-solid fa-folder-open" style="color:#f0c040;"></i> examples</a></li><li><a class="nav-link py-0" style="padding-left:32px;" href="/cgi-bin/repo?id=z2fceU8dk9iFCyyT9YMdGnEyxgxw5&source=examples%2Fadvanced_examples"><i class="fa-solid fa-folder" style="color:#f0c040;"></i> advanced_examples</a></li><li><a class="nav-link py-0" style="padding-left:32px;" href="/cgi-bin/repo?id=z2fceU8dk9iFCyyT9YMdGnEyxgxw5&file=examples%2Fclue_ams_remote.py"><i class="fa-solid fa-file" style="color:#888;"></i> clue_ams_remote.py</a></li><li><a class="nav-link py-0 active" style="padding-left:32px;" href="/cgi-bin/repo?id=z2fceU8dk9iFCyyT9YMdGnEyxgxw5&file=examples%2Fclue_ble_color_patchwork.py"><i class="fa-solid fa-file" style="color:#888;"></i> clue_ble_color_patchwork.py</a></li><li><a class="nav-link py-0" style="padding-left:32px;" href="/cgi-bin/repo?id=z2fceU8dk9iFCyyT9YMdGnEyxgxw5&file=examples%2Fclue_display_sensor_data.py"><i class="fa-solid fa-file" style="color:#888;"></i> clue_display_sensor_data.py</a></li><li><a class="nav-link py-0" style="padding-left:32px;" href="/cgi-bin/repo?id=z2fceU8dk9iFCyyT9YMdGnEyxgxw5&file=examples%2Fclue_height_calculator.py"><i class="fa-solid fa-file" style="color:#888;"></i> clue_height_calculator.py</a></li><li><a class="nav-link py-0" style="padding-left:32px;" href="/cgi-bin/repo?id=z2fceU8dk9iFCyyT9YMdGnEyxgxw5&file=examples%2Fclue_simpletest.py"><i class="fa-solid fa-file" style="color:#888;"></i> clue_simpletest.py</a></li><li><a class="nav-link py-0" style="padding-left:32px;" href="/cgi-bin/repo?id=z2fceU8dk9iFCyyT9YMdGnEyxgxw5&file=examples%2Fclue_slideshow.py"><i class="fa-solid fa-file" style="color:#888;"></i> clue_slideshow.py</a></li><li><a class="nav-link py-0" style="padding-left:32px;" href="/cgi-bin/repo?id=z2fceU8dk9iFCyyT9YMdGnEyxgxw5&file=examples%2Fclue_spirit_level.py"><i class="fa-solid fa-file" style="color:#888;"></i> clue_spirit_level.py</a></li><li><a class="nav-link py-0" style="padding-left:32px;" href="/cgi-bin/repo?id=z2fceU8dk9iFCyyT9YMdGnEyxgxw5&file=examples%2Fclue_temperature_humidity_monitor.py"><i class="fa-solid fa-file" style="color:#888;"></i> clue_temperature_humidity_monitor.py</a></li><li><a class="nav-link py-0" style="padding-left:16px;" href="/cgi-bin/repo?id=z2fceU8dk9iFCyyT9YMdGnEyxgxw5&file=.gitignore"><i class="fa-solid fa-file" style="color:#888;"></i> .gitignore</a></li><li><a class="nav-link py-0" style="padding-left:16px;" href="/cgi-bin/repo?id=z2fceU8dk9iFCyyT9YMdGnEyxgxw5&file=.pylintrc"><i class="fa-solid fa-file" style="color:#888;"></i> .pylintrc</a></li><li><a class="nav-link py-0" style="padding-left:16px;" href="/cgi-bin/repo?id=z2fceU8dk9iFCyyT9YMdGnEyxgxw5&file=.readthedocs.yml"><i class="fa-solid fa-file" style="color:#888;"></i> .readthedocs.yml</a></li><li><a class="nav-link py-0" style="padding-left:16px;" href="/cgi-bin/repo?id=z2fceU8dk9iFCyyT9YMdGnEyxgxw5&file=CODE_OF_CONDUCT.md"><i class="fa-solid fa-file" style="color:#888;"></i> CODE_OF_CONDUCT.md</a></li><li><a class="nav-link py-0" style="padding-left:16px;" href="/cgi-bin/repo?id=z2fceU8dk9iFCyyT9YMdGnEyxgxw5&file=LICENSE"><i class="fa-solid fa-file" style="color:#888;"></i> LICENSE</a></li><li><a class="nav-link py-0" style="padding-left:16px;" href="/cgi-bin/repo?id=z2fceU8dk9iFCyyT9YMdGnEyxgxw5&file=README.rst"><i class="fa-solid fa-file" style="color:#888;"></i> README.rst</a></li><li><a class="nav-link py-0" style="padding-left:16px;" href="/cgi-bin/repo?id=z2fceU8dk9iFCyyT9YMdGnEyxgxw5&file=README.rst.license"><i class="fa-solid fa-file" style="color:#888;"></i> README.rst.license</a></li><li><a class="nav-link py-0" style="padding-left:16px;" href="/cgi-bin/repo?id=z2fceU8dk9iFCyyT9YMdGnEyxgxw5&file=adafruit_clue.py"><i class="fa-solid fa-file" style="color:#888;"></i> adafruit_clue.py</a></li><li><a class="nav-link py-0" style="padding-left:16px;" href="/cgi-bin/repo?id=z2fceU8dk9iFCyyT9YMdGnEyxgxw5&file=disabled.pre-commit-config.yaml"><i class="fa-solid fa-file" style="color:#888;"></i> disabled.pre-commit-config.yaml</a></li><li><a class="nav-link py-0" style="padding-left:16px;" href="/cgi-bin/repo?id=z2fceU8dk9iFCyyT9YMdGnEyxgxw5&file=pyproject.toml"><i class="fa-solid fa-file" style="color:#888;"></i> pyproject.toml</a></li><li><a class="nav-link py-0" style="padding-left:16px;" href="/cgi-bin/repo?id=z2fceU8dk9iFCyyT9YMdGnEyxgxw5&file=requirements.txt"><i class="fa-solid fa-file" style="color:#888;"></i> requirements.txt</a></li><li><a class="nav-link py-0" style="padding-left:16px;" href="/cgi-bin/repo?id=z2fceU8dk9iFCyyT9YMdGnEyxgxw5&file=setup.py.disabled"><i class="fa-solid fa-file" style="color:#888;"></i> setup.py.disabled</a></li>
    
                        </ul>
                      </div>
                    </nav>
                <main class="col-md-9 ms-sm-auto col-lg-10">
                  <div class="container px-1 py-3">
        
<div class="mb-2" style="font-size:1.1rem;"><a href="/cgi-bin/repo?id=z2fceU8dk9iFCyyT9YMdGnEyxgxw5&source=.">/</a> <a href="/cgi-bin/repo?id=z2fceU8dk9iFCyyT9YMdGnEyxgxw5&source=examples">examples</a> / clue_ble_color_patchwork.py</div>
        <div class="list-group">
        <div class="list-group-item">
        <div class="mb-2" style="font-weight:bold;"><i class="fa-solid fa-file"></i> clue_ble_color_patchwork.py</div>
        <pre style="margin:0; font-size:0.85rem; overflow-x:auto; color:#fafafa;"><span style="color:#666; user-select:none;">  1</span>  # SPDX-FileCopyrightText: 2019 Kattni Rembor, written for Adafruit Industries
<span style="color:#666; user-select:none;">  2</span>  #
<span style="color:#666; user-select:none;">  3</span>  # SPDX-License-Identifier: Unlicense
<span style="color:#666; user-select:none;">  4</span>  &quot;&quot;&quot;
<span style="color:#666; user-select:none;">  5</span>  Circuit Python BLE Color Patchwork
<span style="color:#666; user-select:none;">  6</span>  This demo uses advertising to broadcast a color of the users choice.
<span style="color:#666; user-select:none;">  7</span>  We will show a color &quot;patch&quot; on the screen for every unique device
<span style="color:#666; user-select:none;">  8</span>  advertisement that we find.
<span style="color:#666; user-select:none;">  9</span>  &quot;&quot;&quot;
<span style="color:#666; user-select:none;"> 10</span>  
<span style="color:#666; user-select:none;"> 11</span>  import time
<span style="color:#666; user-select:none;"> 12</span>  import random
<span style="color:#666; user-select:none;"> 13</span>  import board
<span style="color:#666; user-select:none;"> 14</span>  import displayio
<span style="color:#666; user-select:none;"> 15</span>  from adafruit_display_text import label
<span style="color:#666; user-select:none;"> 16</span>  import terminalio
<span style="color:#666; user-select:none;"> 17</span>  from adafruit_ble import BLERadio
<span style="color:#666; user-select:none;"> 18</span>  from adafruit_ble.advertising.adafruit import AdafruitColor
<span style="color:#666; user-select:none;"> 19</span>  from adafruit_display_shapes.rect import Rect
<span style="color:#666; user-select:none;"> 20</span>  from adafruit_clue import clue
<span style="color:#666; user-select:none;"> 21</span>  
<span style="color:#666; user-select:none;"> 22</span>  MODE_COLOR_SELECT = 0
<span style="color:#666; user-select:none;"> 23</span>  MODE_SHOW_PATCHWORK = 1
<span style="color:#666; user-select:none;"> 24</span>  
<span style="color:#666; user-select:none;"> 25</span>  COLOR_TRANSPARENT_INDEX = 0
<span style="color:#666; user-select:none;"> 26</span>  COLOR_OFFWHITE_INDEX = 1
<span style="color:#666; user-select:none;"> 27</span>  
<span style="color:#666; user-select:none;"> 28</span>  PROXIMITY_LIMIT = 100
<span style="color:#666; user-select:none;"> 29</span>  
<span style="color:#666; user-select:none;"> 30</span>  current_mode = MODE_SHOW_PATCHWORK
<span style="color:#666; user-select:none;"> 31</span>  
<span style="color:#666; user-select:none;"> 32</span>  # The color pickers will cycle through this list with buttons A and B.
<span style="color:#666; user-select:none;"> 33</span>  color_options = [
<span style="color:#666; user-select:none;"> 34</span>      0xEE0000,
<span style="color:#666; user-select:none;"> 35</span>      0xEEEE00,
<span style="color:#666; user-select:none;"> 36</span>      0x00EE00,
<span style="color:#666; user-select:none;"> 37</span>      0x00EEEE,
<span style="color:#666; user-select:none;"> 38</span>      0x0000EE,
<span style="color:#666; user-select:none;"> 39</span>      0xEE00EE,
<span style="color:#666; user-select:none;"> 40</span>      0xCCCCCC,
<span style="color:#666; user-select:none;"> 41</span>      0xFF9999,
<span style="color:#666; user-select:none;"> 42</span>      0x99FF99,
<span style="color:#666; user-select:none;"> 43</span>      0x9999FF,
<span style="color:#666; user-select:none;"> 44</span>  ]
<span style="color:#666; user-select:none;"> 45</span>  
<span style="color:#666; user-select:none;"> 46</span>  
<span style="color:#666; user-select:none;"> 47</span>  # make all pixels in the patchwork bitmap transparent
<span style="color:#666; user-select:none;"> 48</span>  def make_transparent():
<span style="color:#666; user-select:none;"> 49</span>      palette_mapping.make_transparent(0)
<span style="color:#666; user-select:none;"> 50</span>      for i in range(0, 8):
<span style="color:#666; user-select:none;"> 51</span>          for j in range(0, 8):
<span style="color:#666; user-select:none;"> 52</span>              bitmap[i, j] = 0
<span style="color:#666; user-select:none;"> 53</span>  
<span style="color:#666; user-select:none;"> 54</span>  
<span style="color:#666; user-select:none;"> 55</span>  # make the pixels in the patchwork white by setting them in the palette
<span style="color:#666; user-select:none;"> 56</span>  def make_white():
<span style="color:#666; user-select:none;"> 57</span>      for i in range(2, 66):
<span style="color:#666; user-select:none;"> 58</span>          palette_mapping[i] = 0xFFFFFF
<span style="color:#666; user-select:none;"> 59</span>  
<span style="color:#666; user-select:none;"> 60</span>  
<span style="color:#666; user-select:none;"> 61</span>  # draw the patchwork grid based on nearby_colors
<span style="color:#666; user-select:none;"> 62</span>  def draw_grid():
<span style="color:#666; user-select:none;"> 63</span>      for i, color in enumerate(nearby_colors):
<span style="color:#666; user-select:none;"> 64</span>          if i &lt; 64:
<span style="color:#666; user-select:none;"> 65</span>              palette_mapping[i + 2] = (
<span style="color:#666; user-select:none;"> 66</span>                  color &amp; 0xFFFFFF
<span style="color:#666; user-select:none;"> 67</span>              )  # Mask 0xFFFFFF to avoid invalid color.
<span style="color:#666; user-select:none;"> 68</span>              print(i)
<span style="color:#666; user-select:none;"> 69</span>              print(color)
<span style="color:#666; user-select:none;"> 70</span>  
<span style="color:#666; user-select:none;"> 71</span>  
<span style="color:#666; user-select:none;"> 72</span>  # create a fake mac address and color for testing
<span style="color:#666; user-select:none;"> 73</span>  def add_fake():
<span style="color:#666; user-select:none;"> 74</span>      fake_mac = &quot;&quot;.join([random.choice(&quot;0123456789abcdef&quot;) for _ in range(10)])
<span style="color:#666; user-select:none;"> 75</span>      fake_color = random.choice(color_options)
<span style="color:#666; user-select:none;"> 76</span>      nearby_addresses.append(fake_mac)
<span style="color:#666; user-select:none;"> 77</span>      nearby_colors.append(fake_color)
<span style="color:#666; user-select:none;"> 78</span>  
<span style="color:#666; user-select:none;"> 79</span>  
<span style="color:#666; user-select:none;"> 80</span>  # find nearby devices advertising colors
<span style="color:#666; user-select:none;"> 81</span>  def ble_scan():
<span style="color:#666; user-select:none;"> 82</span>      print(&quot;scanning&quot;)
<span style="color:#666; user-select:none;"> 83</span>      # loop over all found devices
<span style="color:#666; user-select:none;"> 84</span>      for entry in ble.start_scan(AdafruitColor, minimum_rssi=-100, timeout=1):
<span style="color:#666; user-select:none;"> 85</span>          # if this device is not in the list already
<span style="color:#666; user-select:none;"> 86</span>          if entry.color in color_options:
<span style="color:#666; user-select:none;"> 87</span>              print(&quot;new color&quot;)
<span style="color:#666; user-select:none;"> 88</span>          if entry.address.address_bytes not in nearby_addresses:
<span style="color:#666; user-select:none;"> 89</span>              # print(entry.color)
<span style="color:#666; user-select:none;"> 90</span>              # add the address and color to respective lists
<span style="color:#666; user-select:none;"> 91</span>              nearby_addresses.append(entry.address.address_bytes)
<span style="color:#666; user-select:none;"> 92</span>              nearby_colors.append(entry.color)
<span style="color:#666; user-select:none;"> 93</span>          else:  # address was already in the list
<span style="color:#666; user-select:none;"> 94</span>              # update the color to currently advertised value
<span style="color:#666; user-select:none;"> 95</span>              _index = nearby_addresses.index(entry.address.address_bytes)
<span style="color:#666; user-select:none;"> 96</span>              nearby_colors[_index] = entry.color
<span style="color:#666; user-select:none;"> 97</span>  
<span style="color:#666; user-select:none;"> 98</span>  
<span style="color:#666; user-select:none;"> 99</span>  # set a new color to be advertised
<span style="color:#666; user-select:none;">100</span>  def change_advertisement(color):
<span style="color:#666; user-select:none;">101</span>      ble.stop_advertising()
<span style="color:#666; user-select:none;">102</span>      advertisement.color = color
<span style="color:#666; user-select:none;">103</span>      ble.start_advertising(advertisement)
<span style="color:#666; user-select:none;">104</span>      # set NeoPixel to selected advertised
<span style="color:#666; user-select:none;">105</span>      clue.pixel.fill(color)
<span style="color:#666; user-select:none;">106</span>      # update top left self patch
<span style="color:#666; user-select:none;">107</span>      nearby_colors[0] = color
<span style="color:#666; user-select:none;">108</span>  
<span style="color:#666; user-select:none;">109</span>  
<span style="color:#666; user-select:none;">110</span>  # BLE Setup
<span style="color:#666; user-select:none;">111</span>  ble = BLERadio()
<span style="color:#666; user-select:none;">112</span>  advertisement = AdafruitColor()
<span style="color:#666; user-select:none;">113</span>  advertisement.color = color_options[0]
<span style="color:#666; user-select:none;">114</span>  
<span style="color:#666; user-select:none;">115</span>  # init neopixel
<span style="color:#666; user-select:none;">116</span>  clue.pixel.fill(color_options[0])
<span style="color:#666; user-select:none;">117</span>  clue.pixel.brightness = 0.05
<span style="color:#666; user-select:none;">118</span>  
<span style="color:#666; user-select:none;">119</span>  display = board.DISPLAY
<span style="color:#666; user-select:none;">120</span>  
<span style="color:#666; user-select:none;">121</span>  # Create a bitmap with two colors + 64 colors for the map
<span style="color:#666; user-select:none;">122</span>  bitmap = displayio.Bitmap(8, 8, 64 + 2)
<span style="color:#666; user-select:none;">123</span>  
<span style="color:#666; user-select:none;">124</span>  # Create a 8*8 bitmap pre-filled with 64 colors (color 0 and 1 are reserved)
<span style="color:#666; user-select:none;">125</span>  for _i in range(0, 8):
<span style="color:#666; user-select:none;">126</span>      for _j in range(0, 8):
<span style="color:#666; user-select:none;">127</span>          bitmap[_i, _j] = 2 + _i + _j * 8
<span style="color:#666; user-select:none;">128</span>  
<span style="color:#666; user-select:none;">129</span>  # Create an empty palette that will be used in one to one mapping
<span style="color:#666; user-select:none;">130</span>  palette_mapping = displayio.Palette(64 + 2)
<span style="color:#666; user-select:none;">131</span>  
<span style="color:#666; user-select:none;">132</span>  palette_mapping[0] = 0x000000
<span style="color:#666; user-select:none;">133</span>  palette_mapping[1] = 0xFFFFFF
<span style="color:#666; user-select:none;">134</span>  
<span style="color:#666; user-select:none;">135</span>  color_select_palette = displayio.Palette(len(color_options))
<span style="color:#666; user-select:none;">136</span>  for _i, _color in enumerate(color_options):
<span style="color:#666; user-select:none;">137</span>      color_select_palette[_i] = _color
<span style="color:#666; user-select:none;">138</span>  
<span style="color:#666; user-select:none;">139</span>  # Color Select Layout
<span style="color:#666; user-select:none;">140</span>  color_select_group = displayio.Group(max_size=10)
<span style="color:#666; user-select:none;">141</span>  color_select_text_group = displayio.Group(max_size=4, scale=3)
<span style="color:#666; user-select:none;">142</span>  
<span style="color:#666; user-select:none;">143</span>  # white backrground
<span style="color:#666; user-select:none;">144</span>  background = Rect(0, 0, 240, 240, fill=0xFFFFFF)
<span style="color:#666; user-select:none;">145</span>  center_line = Rect(119, 0, 2, 180, fill=0x000000)
<span style="color:#666; user-select:none;">146</span>  
<span style="color:#666; user-select:none;">147</span>  # box around the color preview
<span style="color:#666; user-select:none;">148</span>  bottom_box = Rect(79, 174, 80, 80, fill=0xFFFFFF, outline=0x000000, stroke=2)
<span style="color:#666; user-select:none;">149</span>  
<span style="color:#666; user-select:none;">150</span>  left_btn_lbl = label.Label(terminalio.FONT, text=&quot;A&quot;, color=0x000000)
<span style="color:#666; user-select:none;">151</span>  right_btn_lbl = label.Label(terminalio.FONT, text=&quot;B&quot;, color=0x000000)
<span style="color:#666; user-select:none;">152</span>  
<span style="color:#666; user-select:none;">153</span>  left_btn_action = label.Label(
<span style="color:#666; user-select:none;">154</span>      terminalio.FONT, text=&quot;Next\nColor&quot;, color=0x000000, line_spacing=1
<span style="color:#666; user-select:none;">155</span>  )
<span style="color:#666; user-select:none;">156</span>  right_btn_action = label.Label(terminalio.FONT, text=&quot;Save&quot;, color=0x000000)
<span style="color:#666; user-select:none;">157</span>  
<span style="color:#666; user-select:none;">158</span>  color_select_text_group.append(left_btn_lbl)
<span style="color:#666; user-select:none;">159</span>  color_select_text_group.append(right_btn_lbl)
<span style="color:#666; user-select:none;">160</span>  
<span style="color:#666; user-select:none;">161</span>  color_select_text_group.append(left_btn_action)
<span style="color:#666; user-select:none;">162</span>  color_select_text_group.append(right_btn_action)
<span style="color:#666; user-select:none;">163</span>  
<span style="color:#666; user-select:none;">164</span>  # x position centered on 25% of screen
<span style="color:#666; user-select:none;">165</span>  left_btn_lbl.anchor_point = (0.5, 0)
<span style="color:#666; user-select:none;">166</span>  left_btn_lbl.anchored_position = ((240 / 4) // 3, 21 // 3)
<span style="color:#666; user-select:none;">167</span>  
<span style="color:#666; user-select:none;">168</span>  # x position centered on 75% of screen
<span style="color:#666; user-select:none;">169</span>  right_btn_lbl.anchor_point = (0.5, 0)
<span style="color:#666; user-select:none;">170</span>  right_btn_lbl.anchored_position = ((240 / 4), 21 // 3)
<span style="color:#666; user-select:none;">171</span>  
<span style="color:#666; user-select:none;">172</span>  # x position centered on 25% of screen
<span style="color:#666; user-select:none;">173</span>  left_btn_action.anchor_point = (0.5, 0)
<span style="color:#666; user-select:none;">174</span>  left_btn_action.anchored_position = ((240 / 4) // 3, 96 // 3)
<span style="color:#666; user-select:none;">175</span>  
<span style="color:#666; user-select:none;">176</span>  # x position centered on 75% of screen
<span style="color:#666; user-select:none;">177</span>  right_btn_action.anchor_point = (0.5, 0)
<span style="color:#666; user-select:none;">178</span>  right_btn_action.anchored_position = ((240 / 4), 96 // 3)
<span style="color:#666; user-select:none;">179</span>  
<span style="color:#666; user-select:none;">180</span>  color_select_group.append(background)
<span style="color:#666; user-select:none;">181</span>  color_select_group.append(center_line)
<span style="color:#666; user-select:none;">182</span>  color_select_group.append(bottom_box)
<span style="color:#666; user-select:none;">183</span>  color_select_group.append(color_select_text_group)
<span style="color:#666; user-select:none;">184</span>  
<span style="color:#666; user-select:none;">185</span>  # color preview bmp
<span style="color:#666; user-select:none;">186</span>  color_select_preview_bmp = displayio.Bitmap(1, 1, len(color_options))
<span style="color:#666; user-select:none;">187</span>  color_preview_group = displayio.Group(scale=30 * 2)
<span style="color:#666; user-select:none;">188</span>  
<span style="color:#666; user-select:none;">189</span>  # centered horizontally near bottom on screen
<span style="color:#666; user-select:none;">190</span>  color_preview_group.x = 240 // 2 - 60 // 2
<span style="color:#666; user-select:none;">191</span>  color_preview_group.y = 240 - (60 + 2)
<span style="color:#666; user-select:none;">192</span>  
<span style="color:#666; user-select:none;">193</span>  color_preview_tilegrid = displayio.TileGrid(
<span style="color:#666; user-select:none;">194</span>      color_select_preview_bmp, pixel_shader=color_select_palette
<span style="color:#666; user-select:none;">195</span>  )
<span style="color:#666; user-select:none;">196</span>  color_preview_group.append(color_preview_tilegrid)
<span style="color:#666; user-select:none;">197</span>  
<span style="color:#666; user-select:none;">198</span>  # Create a TileGrid using the Bitmap and Palette
<span style="color:#666; user-select:none;">199</span>  tile_grid = displayio.TileGrid(bitmap, pixel_shader=palette_mapping)
<span style="color:#666; user-select:none;">200</span>  patchwork_group = displayio.Group(scale=30)
<span style="color:#666; user-select:none;">201</span>  patchwork_group.append(tile_grid)
<span style="color:#666; user-select:none;">202</span>  
<span style="color:#666; user-select:none;">203</span>  # Create main Group
<span style="color:#666; user-select:none;">204</span>  group = displayio.Group()
<span style="color:#666; user-select:none;">205</span>  
<span style="color:#666; user-select:none;">206</span>  # Add the patchwork to the main
<span style="color:#666; user-select:none;">207</span>  group.append(patchwork_group)
<span style="color:#666; user-select:none;">208</span>  
<span style="color:#666; user-select:none;">209</span>  # Add the Group to the Display
<span style="color:#666; user-select:none;">210</span>  display.show(group)
<span style="color:#666; user-select:none;">211</span>  
<span style="color:#666; user-select:none;">212</span>  cur_color = 0
<span style="color:#666; user-select:none;">213</span>  
<span style="color:#666; user-select:none;">214</span>  prev_b = clue.button_b
<span style="color:#666; user-select:none;">215</span>  prev_a = clue.button_a
<span style="color:#666; user-select:none;">216</span>  
<span style="color:#666; user-select:none;">217</span>  nearby_addresses = [&quot;myself&quot;]
<span style="color:#666; user-select:none;">218</span>  nearby_colors = [color_options[cur_color]]
<span style="color:#666; user-select:none;">219</span>  
<span style="color:#666; user-select:none;">220</span>  make_white()
<span style="color:#666; user-select:none;">221</span>  
<span style="color:#666; user-select:none;">222</span>  last_scan_time = -30
<span style="color:#666; user-select:none;">223</span>  SCAN_INTERVAL = 30  # seconds
<span style="color:#666; user-select:none;">224</span>  
<span style="color:#666; user-select:none;">225</span>  while True:
<span style="color:#666; user-select:none;">226</span>      now = time.monotonic()
<span style="color:#666; user-select:none;">227</span>      cur_a = clue.button_a
<span style="color:#666; user-select:none;">228</span>      cur_b = clue.button_b
<span style="color:#666; user-select:none;">229</span>      if current_mode == MODE_SHOW_PATCHWORK:
<span style="color:#666; user-select:none;">230</span>          # a button was pressed
<span style="color:#666; user-select:none;">231</span>          if cur_a and not prev_a:
<span style="color:#666; user-select:none;">232</span>              current_mode = MODE_COLOR_SELECT
<span style="color:#666; user-select:none;">233</span>  
<span style="color:#666; user-select:none;">234</span>              # insert color select layout
<span style="color:#666; user-select:none;">235</span>              group.append(color_select_group)
<span style="color:#666; user-select:none;">236</span>              group.append(color_preview_group)
<span style="color:#666; user-select:none;">237</span>  
<span style="color:#666; user-select:none;">238</span>          # is it time to scan?
<span style="color:#666; user-select:none;">239</span>          if last_scan_time + SCAN_INTERVAL &lt; now:
<span style="color:#666; user-select:none;">240</span>              ble_scan()
<span style="color:#666; user-select:none;">241</span>              last_scan_time = now
<span style="color:#666; user-select:none;">242</span>              print(&quot;after scan found {} results&quot;.format(len(nearby_colors)))
<span style="color:#666; user-select:none;">243</span>              # print(nearby_addresses)
<span style="color:#666; user-select:none;">244</span>              draw_grid()
<span style="color:#666; user-select:none;">245</span>  
<span style="color:#666; user-select:none;">246</span>          if clue.proximity &gt;= PROXIMITY_LIMIT:
<span style="color:#666; user-select:none;">247</span>              clue.white_leds = True
<span style="color:#666; user-select:none;">248</span>              while clue.proximity &gt;= PROXIMITY_LIMIT:
<span style="color:#666; user-select:none;">249</span>                  r, g, b, w = clue.color
<span style="color:#666; user-select:none;">250</span>                  clue.pixel.fill(((r &gt;&gt; 8) &amp; 0xFF, (g &gt;&gt; 8) &amp; 0xFF, (b &gt;&gt; 8) &amp; 0xFF))
<span style="color:#666; user-select:none;">251</span>                  change_advertisement(
<span style="color:#666; user-select:none;">252</span>                      ((r &amp; 0xFF00) &lt;&lt; 8) + (g &amp; 0xFF00) + ((b &gt;&gt; 8) &amp; 0xFF)
<span style="color:#666; user-select:none;">253</span>                  )
<span style="color:#666; user-select:none;">254</span>                  time.sleep(0.1)
<span style="color:#666; user-select:none;">255</span>              clue.white_leds = False
<span style="color:#666; user-select:none;">256</span>  
<span style="color:#666; user-select:none;">257</span>      elif current_mode == MODE_COLOR_SELECT:
<span style="color:#666; user-select:none;">258</span>          # current selection preview
<span style="color:#666; user-select:none;">259</span>          color_select_preview_bmp[0, 0] = cur_color
<span style="color:#666; user-select:none;">260</span>  
<span style="color:#666; user-select:none;">261</span>          # a button was pressed
<span style="color:#666; user-select:none;">262</span>          if cur_a and not prev_a:
<span style="color:#666; user-select:none;">263</span>              print(&quot;a button&quot;)
<span style="color:#666; user-select:none;">264</span>              # increment currently selected color index
<span style="color:#666; user-select:none;">265</span>              cur_color += 1
<span style="color:#666; user-select:none;">266</span>              # reset to 0 if it&#x27;s too big
<span style="color:#666; user-select:none;">267</span>              if cur_color &gt;= len(color_options):
<span style="color:#666; user-select:none;">268</span>                  cur_color = 0
<span style="color:#666; user-select:none;">269</span>              print(cur_color)
<span style="color:#666; user-select:none;">270</span>          # b button was pressed
<span style="color:#666; user-select:none;">271</span>          if cur_b and not prev_b:
<span style="color:#666; user-select:none;">272</span>              print(&quot;b button&quot;)
<span style="color:#666; user-select:none;">273</span>              # advertise new color selection
<span style="color:#666; user-select:none;">274</span>              change_advertisement(color_options[cur_color])
<span style="color:#666; user-select:none;">275</span>  
<span style="color:#666; user-select:none;">276</span>              # go back to patchwork mode
<span style="color:#666; user-select:none;">277</span>              current_mode = MODE_SHOW_PATCHWORK
<span style="color:#666; user-select:none;">278</span>              # remove color select background
<span style="color:#666; user-select:none;">279</span>              group.remove(color_select_group)
<span style="color:#666; user-select:none;">280</span>              group.remove(color_preview_group)
<span style="color:#666; user-select:none;">281</span>              make_white()
<span style="color:#666; user-select:none;">282</span>              draw_grid()
<span style="color:#666; user-select:none;">283</span>      prev_a = cur_a
<span style="color:#666; user-select:none;">284</span>      prev_b = cur_b
</pre>
        </div>
        </div>

</div>
</main>
</div>
</div>


</body>
</html>

