GithubHelp home page GithubHelp logo

vtt-info / hackpwm Goto Github PK

View Code? Open in Web Editor NEW

This project forked from webdeb/nanoshim

0.0 0.0 0.0 52 KB

This is a full rp2040 (aka pico) micropython project which implements some cool stuff and controls multiple PWM outputs. It can be extended to use all of the 8 PWM slices, which are provided by the pico. Actually it can be extended much more than that and I had to force me to stop implementing more fancy stuff.

Python 100.00%

hackpwm's Introduction

nanoSHIM aka hackPWM

This is a full rp2040 (aka pico) micropython project which implements some cool stuff and controls multiple PWM outputs. It can be extended to use all of the 8 PWM slices, which are provided by the pico. Actually it can be extended much more than that and I had to force me to stop implementing more fancy stuff.

I build this project with the aim to learn python, and the pico. To see what the rp2040 offers, and how to build things with it. So I played around.

Implemented Features

  • One main Button as input (mostly used as Exit-Button)
  • Two Rotary Encoder inputs
  • ssd1306 128x64 to display all the cool stuff, and as an example an image of Nikolas Tesla eyes with a framebuffer
  • A simple rendering engine to render a Menu with two-part text items (static: <-> dynamic) like (Contrast:    128) and active menu items.
  • Usage of pythons OO-Model to simplify the creation of similar UIs (like Menus, with items, which are boring to code..)
  • Storage settings are stored on the built-in flash memory in a simple json file.
  • Simplified version of JSONPath to save stuff in a deep data structure without typed getters and setters (what can go wrong..)
  • Usage of uctypes to access rp2040 registers directly from micropython
  • Usage of micropythons port of the PWM driver.
  • Usage of the PWM-to-PWM feature, to control one signal by another.
  • Thats it. you can read.

Use it, extend it and if you are not too lazy to click a button, star this repo.

Detailed information

look into the main.py file, and follow the code into the rebbit whole.

Run it

don't, but if you do, do it.

Thanks

Thanks to all, who helped me during my first baby steps into python, micropython, pico and the world of microcontrollers. Maybe thats not my last project. Star this repo. (But don't double-click)

UIListProgram

The UIListProgram class simplifies (ui_program.py)

The simples usage of the UIListProgram is the MainMenu (program_menu.py)

from ui_program import UIListProgram

class MainMenu(UIListProgram):
  def __init__(self, open_pwm, open_settings):
    # Title for the display
    self.title = "Menu"
    # Items to render and which can be selected with the left encoder
    self.items = [
      # Text of the item and handler for the main button.
      { "text": "PWM", "handle_button": open_pwm },
      { "text": "Board Settings", "handle_button": open_settings }
    ]

    super().__init__()

The following is a an extended example of how to use the UIListProgram class. In this example the Settings class is created. program_settings.py This is using dynamic list items, for left<->right text. It also uses the second encoder to change the actual setting.

from ui_program import UIListProgram
from rotary import Rotary
import machine

class Settings(UIListProgram):
  title = "Settings"

  def __init__(self, on_exit):
    self.items = [
      {
        # Will render to "Contrast:     255" (see display.py#render_menu)
        "text": ["Contrast:", self.get_contrast ],
        # This handler will be called when the user has selected this item
        # with left encoder and is rotating the right encoder
        "handle_change": self.handle_contrast_change
      },
      {
        "text": ["Freq:", self.get_freq],
        "handle_change": self.handle_freq_change
      }
    ]

    # Also implemented in the UIListProgram, here we just assign the handler.
    self.handle_button = on_exit

    # Also implemented in the UIListProgram, here we just assign the handler.
    super().__init__()

  # This is the actual class specific logic
  def get_freq(self):
    return str(int(machine.freq()/1_000_000)) + "MHz"

  def handle_freq_change(self, event):
    freq = int(machine.freq() / 1_000_000)
    if (event == Rotary.INC):
      freq += 1
    elif (event == Rotary.DEC):
      freq -= 1

    # limit the boundary to set the freq..
    machine.freq(max(100, min(130, freq)) * 1_000_000)

  def get_contrast(self):
    return str(self.display.get_contrast())

  def handle_contrast_change(self, event):
    contrast = self.display.get_contrast()
    if (event == Rotary.INC):
      contrast += 10
    elif (event == Rotary.DEC):
      contrast -= 10
    self.display.set_contrast(contrast)

hackpwm's People

Contributors

webdeb avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.