BV1AL 之無所不記

2021-04-25

辨識是否在WSL或正常Linux環境

 例用python來播放網路影音的script, 為了在標準Linux或是在WSL環境都能通用,
首先需要辨識環境,再啟用對應的播放軟體。

我們可以用 os.uname() 或 platform.uname() 來判別是在正常Linux環境或是在WSL
下的Linux環境,再呼叫指定的播放器。

在WSL環境的Linux可以直接執行Windows的播放器,例如VLC, 它在WSL裡的路徑是
/mnt/c/Program\ Files/VideoLAN/VLC/vlc.exe

platform.uname()或是os.uname() 如果是在WSL裡的linux執行會列出:
uname_result(system='Linux', node='MY_LAPTOP', release='5.4.72-microsoft-standard-WSL2', version='xxxxxx', machine='x86_64')

import csv, platform, os
from sys import argv, exit

LIST="""\
Classic Portland,http://allclassical-ice.streamguys.com/ac96kmp3
Classic NL,http://playerservices.streamtheworld.com/pls/CLASSICFM.pls
Clarinet obbo,http://213.141.131.10:8002/clarinet
Caprice cello, http://79.111.14.76:8002/cello
Chamber music,http://chambermusic.stream.publicradio.org/chambermusic.mp3
WMNF,http://stream.wmnf.org:8000/wmnf_hd3
Taiwan PBS,http://stream.pbs.gov.tw:1935/live/mp3:PBS/playlist.m3u8
ICRT,http://live.leanstream.co/ICRTFM-MP3"""

MENU = csv.reader(LIST.split('\n'))
n = 1

if len(argv) == 1:
   for x,y in MENU:
       if n%3 == 0:
          END = '\n'
       else:
          END = '\t'
       print('\033[32;1m'+str(n)+'.\033[m' +x,end=END)
       n+=1
   print('\n')
   exit(1)
else:
   for x,y in MENU:
       if argv[1].isdigit():
           if n == int(argv[1]):
               MusicLink = y
               break
           else:
               n+=1

if 'WSL' in platform.uname().release:
      PLAYER = '/mnt/c/Program\ Files/VideoLAN/VLC/vlc.exe'
else:
      PLAYER = 'mpv'

os.system('%s %s '%(PLAYER,MusicLink))

 

標籤: , , ,

2021-04-05

在Linux對Raspberry Pi Pico改裝micropython

 這是在純文字的 Linux shell 裡對 Pico 進行的基礎動作。
(也可以用來對 BBC micro:bit 動作)

使用 lsusb 可以看到這個 Pico 是這樣
Bus 001 Device 017: ID 2e8a:0003 Raspberry Pi RP2 Boot
再用 cat /proc/partitions 可以看到它預設 partition 已經割出一個
   8       32     131072 sdc
   8       33     131071 sdc1
(因為我已經插了一支 USB 佔掉 sdb, 所以這裡顯示 sdc)

如果用 fdisk -l 可以看到
/dev/sdc1 1 262143 262143 128M e W95 FAT16 (LBA)

在這裡可以下載 micropython for Pico
https://micropython.org/download/rp2-pico/

rp2-pico-20210403-unstable-v1.14-132-gd87f42b0e.uf2 (目前最新的)

按住 Pico USB旁邊的 BOOTSEL 然後插上 micro USB 到你的電腦
用手動掛載 Pico:
mount /dev/sdc1 /media/sdc1
再把下載來的 micropython 的 uf2 檔直接 copy 到 Pico 的 partition 1
cp rp2-pico-20210205-unstable-v1.14-8-g1f800cac3.uf2 /media/sdc1

然後 Pico 自己會 reboot 進入 micropython mode

使用 miniterm (只要裝了 python serial 就會有的一個好用 terminal)
miniterm /dev/ttyACM0 115200 --raw  就連進 Pico 的 micropython 提示符號

標籤: , , , , , ,