jim-at-jibba/micropython.nvim

github github
programming-languages-support
stars 19
issues 0
subscribers 2
forks 0
CREATED

2024-04-16

UPDATED

7 days ago


micropython_nvim

Theme: duskfox

Introduction

micropython_nvim is a plugin that aims to make it easier and more enjoyable to work on micropython projects in Neovim. See the quickstart section to get started.

N.B. If you open an existing project that has an .ampy configuration file in the root directory, the plugin will automatically configure the port and baud rate for you.

IMPORTANT This plugin assumes you are opening Neovim at the root of the project. Some commands will not behave in the expected way if you choose not to do this.

Goals

  • Allows you to run and upload your python files directly to your chosen micro-controller straight from Neovim
  • Allows general file management
  • Allows easy management of port, baudrate, and other settings
  • Allows easy set up of project environment
    • Create a new project, with project specific settings
  • Easy access to the REPL

Features

  • Run local python files on your micro-controller
  • Upload local python files to your micro-controller
  • REPL access
  • File management
  • Project initialisation

Requirements

Quickstart

  • Install micropython_nvim using your preferred package manager
  • Add a keybind to run function
-- Lua
vim.keymap.set("n", "<leader>mr", require("micropython_nvim").run)
  • Follow the project setup steps on automatically create the necessary files and directories for a new project.

Next steps

Installation

{
    "jim-at-jibba/micropython.nvim",
    dependencies = { "akinsho/toggleterm.nvim", "stevearc/dressing.nvim" },
}
use {
    "jim-at-jibba/micropython.nvim",
    requires = { "akinsho/toggleterm.nvim", "stevearc/dressing.nvim" },
}

Usage

  • :MPRun runs current buffer on the micro-controller
  • :MPSetPort sets the port, in both the .ampy configuration file and Neovim global variable
  • :MPSetBaud sets the baudrate in the .ampy configuration file and Neovim global variable
  • :MPSetStubs sets the stubs for the board in requirments.txt ready for installation
  • :MPRepl opens the REPL
  • :MPInit initalizes the project with basic settings and files. See project setup
  • :MPUpload uploads the current buffer to the micro-controller
  • :MPEraseOne deletes single file or folder from device.
  • :MPUploadAll uploads all files in the project. This command also accepts file or folder names to ignore i.e :MPUploadAll test.py unused and auto ignores the following files. Currently, you can not ignore files that are not in the root directory.
  local ignore_list = {
    ['.git'] = true,
    ['requirements.txt'] = true,
    ['.ampy'] = true,
    ['.vscode'] = true,
    ['.gitignore'] = true,
    ['project.pymakr'] = true,
    ['env'] = true,
    ['venv'] = true,
    ['__pycache__'] = true,
    ['.python-version'] = true,
    ['.micropy/'] = true,
    ['micropy.json'] = true,
  }

Project Setup

Steps to initialize a project

  • Create a new directory for your project
  • Optional but highly recommended create a virtual environment
  • Run :MPInit in the project directory, this will create the necessary files and directories. This includes:
    • main.py
from machine import Pin
from time import sleep

led = Pin("LED", Pin.OUT)

while True:
    led.value(not led.value())
    print("LED is ON" if led.value() else "LED is OFF")
    sleep(0.5)
  • .ampy configuration file
AMPY_BAUD=115200
# AMPY_PORT=
# Fix for macOS users' "Could not enter raw repl"; try 2.0 and lower from there:
# AMPY_DELAY=0.5
  • requirments.txt file
adafruit-ampy
rshell
micropython-rp2-stubs
ruff
  • pyrightconfig.json file
{
  "reportMissingModuleSource": false
}
  • :MPSetPort to set the port
  • :MPSetStubs to set the stubs for the board
  • :MPSetBaud to set the baudrate if not the same as the default 115200
  • pip install -r requirments.txt to install the required packages

Now you be able to run the project using :MPRun.

Statusline

A statusline component can be easily added to show whether a buffer is tagged.

Lualine Component

require("lualine").setup({
    sections = {
        lualine_b = {
            {
              require("micropython_nvim").statusline,
              cond = package.loaded["micropython_nvim"] and require("micropython_nvim").exists,
            },
        }
    }
})

Not implemented (yet)

  • upload all files in a directory
  • delete individual files
  • delete all files
  • get files from the device file system

Inspiration and Thanks