Skip to content

Instantly share code, notes, and snippets.

@benevpi
Created July 1, 2025 11:03
Show Gist options
  • Save benevpi/d3c15d853f63d2f008e757421b623768 to your computer and use it in GitHub Desktop.
Save benevpi/d3c15d853f63d2f008e757421b623768 to your computer and use it in GitHub Desktop.
#include <Python.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "piolib.h"
#include "ws2812.pio.h"
static PyObject* init(PyObject *self, PyObject *args) {
int sm;
uint offset;
uint gpio = 2;
int pass;
PIO pio;
printf("loading pio0\n");
pio = pio0;
printf("loaded pio0\n");
sm = pio_claim_unused_sm(pio, true);
printf("sdfsd\n");
pio_sm_config_xfer(pio, sm, PIO_DIR_TO_SM, 256, 1);
printf("sdfsdfsd\n");
offset = pio_add_program(pio, &ws2812_program);
printf("Loaded program at %d, using sm %d, gpio %d with PIO %d", offset, sm, gpio, pio);
Py_RETURN_NONE;
}
//what if we re-setup the pio each time. Is this really necessary? Maybe look into using module state memory
static PyObject* colour(PyObject *self, PyObject *args) {
PIO pio;
int sm;
uint offset;
uint gpio = 2;
int pass;
int red;
int green;
int blue;
printf("loading pio0\n");
pio = pio0;
printf("loaded pio0\n");
sm = pio_claim_unused_sm(pio, true);
printf("sdfsd\n");
pio_sm_config_xfer(pio, sm, PIO_DIR_TO_SM, 256, 1);
printf("sdfsdfsd\n");
offset = pio_add_program(pio, &ws2812_program);
printf("Loaded program at %d, using sm %d, gpio %d with PIO %d", offset, sm, gpio, pio);
if (!PyArg_ParseTuple(args, "iii", &red, &green, &blue))
return NULL;
sm = pio_claim_unused_sm(pio, true);
pio_sm_config_xfer(pio, sm, PIO_DIR_TO_SM, 256, 1);
offset = pio_add_program(pio, &ws2812_program);
printf("Loaded program at %d, using sm %d, gpio %d\n", offset, sm, gpio);
pio_sm_clear_fifos(pio, sm);
pio_sm_set_clkdiv(pio, sm, 1.0);
ws2812_program_init(pio, sm, offset, gpio, 800000.0, false);
int pixels = 60; // note, this should really be a parameter, but let's get onto that later
uint8_t databuf[pixels * 4];
printf("creating data \n");
for (int i = 0; i < pixels; i++)
{
databuf[3*i + 0] = red;
databuf[3*i + 1] = green;
databuf[3*i + 2] = blue;
}
printf("created data \n");
pio_sm_xfer_data(pio, sm, PIO_DIR_TO_SM, sizeof(databuf), databuf);
printf("sent data \n");
sleep_ms(1000);
pio_sm_unclaim(pio, sm);
pio_close(pio);
Py_RETURN_NONE;
}
// Exported methods are collected in a table
PyMethodDef method_table[] = {
{"init", (PyCFunction) init, METH_VARARGS, "Method docstring"},
{"colour", (PyCFunction) colour, METH_VARARGS, "Method docstring"},
{NULL, NULL, 0, NULL} // Sentinel value ending the table
};
// A struct contains the definition of a module
PyModuleDef mymodule_module = {
PyModuleDef_HEAD_INIT,
"mymodule", // Module name
"This is the module docstring",
10000, // Optional size of the module state memory
method_table,
NULL, // Optional slot definitions
NULL, // Optional traversal function
NULL, // Optional clear function
NULL // Optional module deallocation function
};
// The module init function
PyMODINIT_FUNC PyInit_mymodule(void) {
return PyModule_Create(&mymodule_module);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment