Skip to content

Latest commit

 

History

History
172 lines (139 loc) · 3.94 KB

OrangePiZero.md

File metadata and controls

172 lines (139 loc) · 3.94 KB

CAN'T GET STABLE. RANDOM FLICKERING.

Orange Pi Zero, Armbian 5.60 (Stretch) 4.14.70-sunxi

Settings

sudo armbian-config

  • Wifi
  • Timezone

Wiring

https://i.stack.imgur.com/O03j0.jpg

  • 5V
  • GND
  • MOSI (SPI1 MOSI/GPIO15/Pin19)

SPI-device

There is no /dev/spidev* -devices at start. We can't use /dev/spidev0.0, because it's used by flash-memory. Let's make /dev/spidev1.0 by editing /boot/armbianEnv.txt:

overlays=spi-spidev usbhost2 usbhost3
param_spidev_spi_bus=1
param_spidev_max_freq=100000000

(you can also use sudo armbian-config: System settings -> Edit boot environment)

Reboot

crw------- 1 root root 153, 0 Oct 1 10:45 /dev/spidev1.0

Let's make it accessible without root. /etc/udev/rules.d/50-spi.rules:

SUBSYSTEM=="spidev", GROUP="spiuser", MODE="0660"
sudo udevadm control --reload-rules
sudo groupadd spiuser
sudo adduser "$USER" spiuser

(remember that adding to group doesn't come active until new login)

sudo modprobe -r spidev
sudo modprobe spidev

crw-rw---- 1 root spiuser 153, 0 Oct 1 10:56 /dev/spidev1.0

Python 3

sudo apt install python3-pip python3-dev python3-setuptools zlib1g-dev libjpeg-dev
sudo python3 -m pip install wheel
sudo python3 -m pip install pillow
sudo python3 -m pip install pyserial
sudo python3 -m pip install numpy

(Numpy installation takes about 16minutes)

sudo python3 -m pip install spidev
sudo python3 -m pip install git+https://github.com/joosteto/ws2812-spi

Let's make ws2812 compatible with Python 3:

sudo sed -i 's/str(err)/(str(err))/g' /usr/local/lib/python3.5/dist-packages/ws2812.py

Hack for first LED -problem (works only without numpy?):

sudo sed -i 's/tx=[]/tx=[0x00]/g' /usr/local/lib/python3.5/dist-packages/ws2812.py

Hack for first LED -problem (with numpy?):

def write2812_numpy4(spi,data):
  data = np.insert(data, 0, [0,0,0], axis=0)

Test SPI

import spidev
spi = spidev.SpiDev()
spi.open(1, 0)
to_send = [0x01, 0x02, 0x03]
spi.xfer(to_send)

Test ws2812

import spidev
import ws2812
spi = spidev.SpiDev()
spi.open(1,0)

#write 4 WS2812's, with the following colors: red, green, blue, yellow
ws2812.write2812(spi, [[10,0,0], [0,10,0], [0,0,10], [10, 10, 0]])

Code

import numpy as np
import time
import spidev
import ws2812
import colorsys

spi = spidev.SpiDev()
spi.open(1,0)

data = np.zeros((50,3), dtype=np.uint8)

while 1:
  for i in range(50):
    data[i] = np.array(colorsys.hsv_to_rgb(i/50, 1, 1))*255
  ws2812.write2812(spi, data)
  time.sleep(0.5)

# data = np.array([[10,0,0], [0,10,0], [0,0,10], [10, 10, 0]])
# ws2812.write2812(spi, data)

# for i in range(50): data[i] = [a * 255 for a in colorsys.hsv_to_rgb(i/50, 1, 1)]
# for i in range(50): data[i] = np.array(colorsys.hsv_to_rgb(i/50, 1, 1))*255

# https://github.com/joosteto/ws2812-spi/issues/2
# https://github.com/joosteto/ws2812-spi/issues/6
import numpy as np
import time
import spidev
import colorsys

spi = spidev.SpiDev()
spi.open(1,0)
spi.max_speed_hz = int(4/1.05e-6)

data = np.zeros((50,3), dtype=np.uint8)

def write2812(spi, colors):
  tx = [0x00] + [ int(
    ((byte >> (2 * ibit + 1)) & 1) * 0x60 +
    ((byte >> (2 * ibit + 0)) & 1) * 0x06 +
    0x88 )
    for rgb in colors
    for byte in rgb
    for ibit in range(3, -1, -1)
  ]
  spi.writebytes(tx)
  
frame = 0
while 1:
  for i in range(50):
    h = float((frame + i) % 50) / 50
    data[i] = np.array(colorsys.hsv_to_rgb(h, 1, 1))*255
  write2812(spi, data)
  time.sleep(0.05)
  frame += 1

Links