[{"element_type":"markdown","content":"\u003cp\u003eRobert and I will doing a recording about CircuitPython. These are the examples we'll cover in the recording.\u003c/p\u003e\n","metadata":{"markdown":"Robert and I will doing a recording about CircuitPython. These are the examples we'll cover in the recording."}},{"element_type":"markdown","content":"\u003ch1\u003eGetting started\u003c/h1\u003e\n\n\u003cp\u003eGo to \u003ca href=\"https://circuitpython.org/downloads\"\u003ehttps://circuitpython.org/downloads\u003c/a\u003e to download the appropriate UF2. Hold boot button on plugging in and then drag and drop the file.\u003c/p\u003e\n\n\u003cp\u003eSee this guide for workflow details: \u003ca href=\"https://learn.adafruit.com/welcome-to-circuitpython\"\u003ehttps://learn.adafruit.com/welcome-to-circuitpython\u003c/a\u003e\u003c/p\u003e\n\n\u003cp\u003eSee this guide for more simple API examples: \u003ca href=\"https://learn.adafruit.com/circuitpython-essentials/\"\u003ehttps://learn.adafruit.com/circuitpython-essentials/\u003c/a\u003e\u003c/p\u003e\n","metadata":{"markdown":"# Getting started\nGo to https://circuitpython.org/downloads to download the appropriate UF2. Hold boot button on plugging in and then drag and drop the file.\n\nSee this guide for workflow details: https://learn.adafruit.com/welcome-to-circuitpython\n\nSee this guide for more simple API examples: https://learn.adafruit.com/circuitpython-essentials/"}},{"element_type":"markdown","content":"\u003ch2\u003eBlinky\u003c/h2\u003e\n","metadata":{"markdown":"## Blinky"}},{"element_type":"code","content":"import digitalio\nimport board\nimport time\n\nled = digitalio.DigitalInOut(board.LED)\nled.direction = digitalio.Direction.OUTPUT\n\nwhile True:\n    led.value = True\n    time.sleep(0.5)\n    led.value = False\n    time.sleep(0.5)","metadata":{"language":"auto","linenums":false}},{"element_type":"markdown","content":"\u003ch2\u003eLibraries\u003c/h2\u003e\n\n\u003cp\u003eThey come in the bundle zip for manual install. I'll do that. \u003ccode class=\"inline\"\u003ecircup\u003c/code\u003e is an automated way to do it: \u003ca href=\"https://github.com/adafruit/circup\"\u003ehttps://github.com/adafruit/circup\u003c/a\u003e\u003c/p\u003e\n\n\u003cp\u003eBundle zip is here: \u003ca href=\"https://github.com/adafruit/Adafruit_CircuitPython_Bundle\"\u003ehttps://github.com/adafruit/Adafruit_CircuitPython_Bundle\u003c/a\u003e Get the py version.\u003c/p\u003e\n","metadata":{"markdown":"## Libraries\nThey come in the bundle zip for manual install. I'll do that. `circup` is an automated way to do it: https://github.com/adafruit/circup\n\nBundle zip is here: https://github.com/adafruit/Adafruit_CircuitPython_Bundle Get the py version."}},{"element_type":"markdown","content":"\u003ch2\u003eNeoPixel Blink\u003c/h2\u003e\n","metadata":{"markdown":"## NeoPixel Blink"}},{"element_type":"code","content":"import digitalio\nimport board\nimport time\n\nimport neopixel\n\npixels = neopixel.NeoPixel(board.NEOPIXEL, 12, brightness=0.2)\n\nwhile True:\n    pixels.fill(0x000000)\n    time.sleep(0.5)\n    pixels.fill(0xff0000)\n    time.sleep(0.5)","metadata":{"language":"auto","linenums":false}},{"element_type":"markdown","content":"\u003ch2\u003eNeoPixel on press\u003c/h2\u003e\n","metadata":{"markdown":"## NeoPixel on press"}},{"element_type":"code","content":"import digitalio\nimport board\nimport time\n\nimport neopixel\n\npixels = neopixel.NeoPixel(board.NEOPIXEL, 12, brightness=0.2)\n\nfirst_key = digitalio.DigitalInOut(board.KEY1)\nfirst_key.pull = digitalio.Pull.UP\n\nwhile True:\n    color = 0x000000\n    if not first_key.value:\n        color = 0xff0000\n    pixels.fill(color)\n    time.sleep(0.05)","metadata":{"language":"auto","linenums":false}},{"element_type":"markdown","content":"\u003ch2\u003eNeoPixel color from distance\u003c/h2\u003e\n","metadata":{"markdown":"## NeoPixel color from distance"}},{"element_type":"code","content":"import digitalio\nimport board\nimport time\n\nimport neopixel\nimport adafruit_vcnl4200\nimport rainbowio\n\npixels = neopixel.NeoPixel(board.NEOPIXEL, 12, brightness=0.2)\n\ni2c = board.STEMMA_I2C()\n\nsensor = adafruit_vcnl4200.Adafruit_VCNL4200(i2c)\n\nwhile True:\n    prox = sensor.proximity\n    print(f\"Proximity is: {prox}\")\n    pixels.fill(rainbowio.colorwheel(min(prox, 255)))\n    time.sleep(0.05)","metadata":{"language":"auto","linenums":false}},{"element_type":"markdown","content":"\u003ch2\u003eKey up and down\u003c/h2\u003e\n","metadata":{"markdown":"## Key up and down"}},{"element_type":"code","content":"import digitalio\nimport board\nimport time\n\nimport neopixel\n\npixels = neopixel.NeoPixel(board.NEOPIXEL, 12, brightness=0.2)\n\nfirst_key = digitalio.DigitalInOut(board.KEY1)\nfirst_key.pull = digitalio.Pull.UP\n\nkey_up = True\nwhile True:\n    pressed = not first_key.value\n    if not pressed and not key_up:\n        pixels.fill(0x000000)\n        key_up = True\n        print(\"up\")\n    elif pressed and key_up:\n        pixels.fill(0xff0000)\n        print(\"down\")\n        key_up = False","metadata":{"language":"auto","linenums":false}},{"element_type":"markdown","content":"\u003ch2\u003eType 1\u003c/h2\u003e\n","metadata":{"markdown":"## Type 1"}},{"element_type":"code","content":"import digitalio\nimport board\nimport time\n\nimport neopixel\n\nimport usb_hid\nfrom adafruit_hid.keyboard import Keyboard\nfrom adafruit_hid.keyboard_layout_us import KeyboardLayoutUS\nfrom adafruit_hid.keycode import Keycode\n\npixels = neopixel.NeoPixel(board.NEOPIXEL, 12, brightness=0.2)\n\nfirst_key = digitalio.DigitalInOut(board.KEY1)\nfirst_key.pull = digitalio.Pull.UP\n\nkeyboard = Keyboard(usb_hid.devices)\nkeyboard_layout = KeyboardLayoutUS(keyboard)  # We're in the US :)\n\n# print(dir(Keycode))\n\nkey_up = True\nwhile True:\n    pressed = not first_key.value\n    if not pressed and not key_up:\n        pixels.fill(0x000000)\n        key_up = True\n        print(\"up\")\n        keyboard.release_all()\n    elif pressed and key_up:\n        pixels.fill(0xff0000)\n        print(\"down\")\n        key_up = False\n        # KEYPAD_ONE doesn't work because we may need numlock\n        keyboard.press(Keycode.ONE)","metadata":{"language":"auto","linenums":false}},{"element_type":"markdown","content":"\u003ch2\u003eNon-US Layout\u003c/h2\u003e\n\n\u003cp\u003eGenerated a colemak keyboard layout from \u003ca href=\"https://github.com/Neradoc/Circuitpython_Keyboard_Layouts\"\u003ehttps://github.com/Neradoc/Circuitpython_Keyboard_Layouts\u003c/a\u003e\u003c/p\u003e\n","metadata":{"markdown":"## Non-US Layout\nGenerated a colemak keyboard layout from https://github.com/Neradoc/Circuitpython_Keyboard_Layouts"}},{"element_type":"code","content":"import digitalio\nimport board\nimport time\n\nimport neopixel\n\nimport usb_hid\nfrom adafruit_hid.keyboard import Keyboard\nfrom keyboard_layout_win_cmk import KeyboardLayout\nfrom adafruit_hid.keycode import Keycode\n\npixels = neopixel.NeoPixel(board.NEOPIXEL, 12, brightness=0.2)\n\nkeyboard = Keyboard(usb_hid.devices)\nkeyboard_layout = KeyboardLayout(keyboard)  # We're in the US :)\n\nprint(dir(Keycode))\n\nbutton = digitalio.DigitalInOut(board.BUTTON)\nbutton.pull = digitalio.Pull.UP\nlast_value = True\nwhile True:\n    value_now = button.value\n    if value_now == last_value:\n        continue\n    if value_now:\n        pixels[0] = 0x000000\n        print(\"up\")\n        keyboard.release_all()\n    else:\n        pixels[0] = 0x0000FF\n        print(\"down\")\n        # KEYPAD_ONE doesn't work because we may need numlock\n        keyboard_layout.write(\"hello\")\n    last_value = value_now","metadata":{"language":"auto","linenums":false}},{"element_type":"markdown","content":"\u003ch2\u003eNetworking\u003c/h2\u003e\n\n\u003cp\u003eGuide here: \u003ca href=\"https://learn.adafruit.com/networking-in-circuitpython/making-http-andhttps-requests\"\u003ehttps://learn.adafruit.com/networking-in-circuitpython/making-http-andhttps-requests\u003c/a\u003e\u003c/p\u003e\n\n\u003cp\u003eEdit settings.toml to include wifi info:\u003c/p\u003e\n","metadata":{"markdown":"## Networking\nGuide here: https://learn.adafruit.com/networking-in-circuitpython/making-http-andhttps-requests\n\nEdit settings.toml to include wifi info:"}},{"element_type":"code","content":"CIRCUITPY_WIFI_SSID=\"myssid\"\nCIRCUITPY_WIFI_PASSWORD=\"mypassword\"\nCIRCUITPY_WEB_API_PASSWORD=\"hello\"","metadata":{"language":"auto","linenums":false}},{"element_type":"markdown","content":"\u003ch2\u003eBasic HTTP Request\u003c/h2\u003e\n\n\u003cp\u003eFrom: \u003ca href=\"https://learn.adafruit.com/networking-in-circuitpython/making-http-andhttps-requests\"\u003ehttps://learn.adafruit.com/networking-in-circuitpython/making-http-andhttps-requests\u003c/a\u003e\u003c/p\u003e\n","metadata":{"markdown":"## Basic HTTP Request\nFrom: https://learn.adafruit.com/networking-in-circuitpython/making-http-andhttps-requests"}},{"element_type":"code","content":"import adafruit_connection_manager\nimport wifi\n\nimport adafruit_requests\n\nTEXT_URL = \"http://wifitest.adafruit.com/testwifi/index.html\"\n\n# Initalize Wifi, Socket Pool, Request Session\npool = adafruit_connection_manager.get_radio_socketpool(wifi.radio)\nssl_context = adafruit_connection_manager.get_radio_ssl_context(wifi.radio)\nrequests = adafruit_requests.Session(pool, ssl_context)\n\nwith requests.get(TEXT_URL) as response:\n    print(response.text)","metadata":{"language":"auto","linenums":false}},{"element_type":"markdown","content":"\u003ch2\u003eBasic Blink HTTPServer\u003c/h2\u003e\n\n\u003cp\u003eSlimmed down from: \u003ca href=\"https://learn.adafruit.com/pico-w-http-server-with-circuitpython/code-the-pico-w-http-server\"\u003ehttps://learn.adafruit.com/pico-w-http-server-with-circuitpython/code-the-pico-w-http-server\u003c/a\u003e\u003c/p\u003e\n","metadata":{"markdown":"## Basic Blink HTTPServer\nSlimmed down from: https://learn.adafruit.com/pico-w-http-server-with-circuitpython/code-the-pico-w-http-server"}},{"element_type":"code","content":"# SPDX-FileCopyrightText: 2023 Liz Clark for Adafruit Industries\n#\n# SPDX-License-Identifier: MIT\n\nimport os\nimport time\nimport ipaddress\nimport wifi\nimport socketpool\nimport busio\nimport board\nimport microcontroller\nimport displayio\nimport terminalio\n\nfrom digitalio import DigitalInOut, Direction\nfrom adafruit_httpserver import Server, Request, Response, POST\n\n\n#  onboard LED setup\nled = DigitalInOut(board.LED)\nled.direction = Direction.OUTPUT\nled.value = False\n\nprint(\"Connected to WiFi\")\npool = socketpool.SocketPool(wifi.radio)\nserver = Server(pool, \"/static\", debug=False)\n\n#  variables for HTML\n#  font for HTML\nfont_family = \"monospace\"\n\n#  the HTML script\n#  setup as an f string\n#  this way, can insert string variables from code.py directly\n#  of note, use {{ and }} if something from html *actually* needs to be in brackets\n#  i.e. CSS style formatting\ndef webpage():\n    html = f\"\"\"\n    \u003c!DOCTYPE html\u003e\n    \u003chtml\u003e\n    \u003chead\u003e\n    \u003cmeta http-equiv=\"Content-type\" content=\"text/html;charset=utf-8\"\u003e\n    \u003cmeta name=\"viewport\" content=\"width=device-width, initial-scale=1\"\u003e\n    \u003cstyle\u003e\n    html{{font-family: {font_family}; background-color: lightgrey;\n    display:inline-block; margin: 0px auto; text-align: center;}}\n      h1{{color: deeppink; width: 200; word-wrap: break-word; padding: 2vh; font-size: 35px;}}\n      p{{font-size: 1.5rem; width: 200; word-wrap: break-word;}}\n      .button{{font-family: {font_family};display: inline-block;\n      background-color: black; border: none;\n      border-radius: 4px; color: white; padding: 16px 40px;\n      text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}}\n      p.dotted {{margin: auto;\n      width: 75%; font-size: 25px; text-align: center;}}\n    \u003c/style\u003e\n    \u003c/head\u003e\n    \u003cbody\u003e\n    \u003ctitle\u003ePico W HTTP Server\u003c/title\u003e\n    \u003ch1\u003ePico W HTTP Server\u003c/h1\u003e\n    \u003cbr\u003e\n    \u003cp class=\"dotted\"\u003eThis is a Pico W running an HTTP server with CircuitPython.\u003c/p\u003e\n    \u003cbr\u003e\n    \u003cp class=\"dotted\"\u003e\n    \u003ch1\u003eControl the LED on the Pico W with these buttons:\u003c/h1\u003e\u003cbr\u003e\n    \u003cform accept-charset=\"utf-8\" method=\"POST\"\u003e\n    \u003cbutton class=\"button\" name=\"LED ON\" value=\"ON\" type=\"submit\"\u003eLED ON\u003c/button\u003e\u003c/a\u003e\u003c/p\u003e\u003c/form\u003e\n    \u003cp\u003e\u003cform accept-charset=\"utf-8\" method=\"POST\"\u003e\n    \u003cbutton class=\"button\" name=\"LED OFF\" value=\"OFF\" type=\"submit\"\u003eLED OFF\u003c/button\u003e\u003c/a\u003e\u003c/p\u003e\u003c/form\u003e\n    \u003c/body\u003e\u003c/html\u003e\n    \"\"\"\n    return html\n\n#  route default static IP\n@server.route(\"/\")\ndef base(request: Request):  # pylint: disable=unused-argument\n    #  serve the HTML f string\n    #  with content type text/html\n    return Response(request, f\"{webpage()}\", content_type='text/html')\n\n#  if a button is pressed on the site\n@server.route(\"/\", POST)\ndef buttonpress(request: Request):\n    #  get the raw text\n    raw_text = request.raw_request.decode(\"utf8\")\n    # print(raw_text)\n    #  if the led on button was pressed\n    if \"ON\" in raw_text:\n        #  turn on the onboard LED\n        led.value = True\n        print(\"on\")\n    #  if the led off button was pressed\n    if \"OFF\" in raw_text:\n        #  turn the onboard LED off\n        led.value = False\n        print(\"off\")\n    #  respond\n    return Response(request, f\"{webpage()}\", content_type='text/html')\n\nprint(\"starting server..\")\n# startup the server\ntry:\n    server.start(str(wifi.radio.ipv4_address))\n    print(\"Listening on http://%s:%d\" % (wifi.radio.ipv4_address, server.port))\n#  if the server fails to begin, restart the pico w\nexcept OSError:\n    time.sleep(5)\n    print(\"restarting..\")\n    microcontroller.reset()\n\nping_address = ipaddress.ip_address(\"8.8.4.4\")\nnext_ping = time.monotonic()\n\nwhile True:\n    #  every 30 seconds ping server to verify connection\n    if next_ping \u003c time.monotonic():\n        if wifi.radio.ping(ping_address) is None:\n            print(\"lost connection\")\n        else:\n            print(\"connected\")\n        next_ping = time.monotonic() + 30\n\n    #  poll the server for incoming/outgoing requests\n    server.poll()","metadata":{"language":"python","linenums":false}}]