謎's キッチン

謎のひとりごと。Amazon欲しい物リストはこちら: https://www.amazon.co.jp/hz/wishlist/ls/CCPOV7C6JTD2

スマート音声リモコンのAladdin Remolessを分解したみた

スマート音声リモコンのAladdin Remolessがサービス終了で安くなってたので買って分解してみたけど、ハードウェアとしては百度の小度智能音箱大金剛(↓)と同じもののようだ(ただNANDはFORESEEじゃなくてXTXの何かだった)。
www.52audio.com

さてここからどうしよう?とりあえず今は立て込んでるから後回しで。

avr-gccでArduino IDE用の*.inoファイルを一発コンパイルする

Arduinoを組み込んだ自作仮想アナログ時計 (gsclock) のインストールをより簡単にしようと思い、今回はArduino IDE無しでArduino IDE向けソースをコンパイルする方法を探してみました。(なお同様の目的のものとしてarduino-mkarduino-builderarduino-cliなどもあるようなので普通の人はそっちを試した方が良いかもしれません。)

まずはAVR向けのクロスコンパイラ、標準Cライブラリ、Arduinoコアライブラリを入れます。

sudo apt install gcc-avr avr-libc arduino-core-avr

ここで注意ですが、Arduinoコアライブラリは未コンパイルの状態のようです。そのため本当は事前コンパイルをしてライブラリを作るのが正攻法だと思うのですが、今回は横着して自作プログラムと同時にライブラリもコンパイルすることにします。

問題はArduinoコアライブラリにC言語C++言語が混在しているためコマンドラインでオプションを一括指定ができないことです。これも普通ならMakefileを使ってどうこうするところなのですが、他に良い方法は無いかなと探したところGCCにはcc1 (gcc相当)とcc1plus (g++相当)に渡すパラメータを個別に変更できるSpecsファイルというものがあるようです。今回はそれを使ってみます。

まずはAVR向けgccの標準Specsファイルをダンプします。

avr-gcc -dumpspecs > avr-gcc-arduino.specs

次にavr-gcc-arduino.specsのcc1とcc1plusのパラメータを変更します。

*cc1:
-%(cc1_n_flash) %(cc1_errata_skip) %(cc1_rmw) %(cc1_non_bit_addressable_registers_mask) %(cc1_absdata)
+%(cc1_n_flash) %(cc1_errata_skip) %(cc1_rmw) %(cc1_non_bit_addressable_registers_mask) %(cc1_absdata) -flto -fuse-linker-plugin
*cc1plus:
-%(cc1) %{!frtti:-fno-rtti} %{!fenforce-eh-specs:-fno-enforce-eh-specs} %{!fexceptions:-fno-exceptions}
+%(cc1) %{!frtti:-fno-rtti} %{!fenforce-eh-specs:-fno-enforce-eh-specs} %{!fexceptions:-fno-exceptions} -std=c++14 -fno-threadsafe-statics -fno-exceptions

まずはhello worldを試すことにします。以下をhello.inoとして保存します。

#include <Arduino.h> /*これは今の所外せません*/

void setup() {
  Serial.begin(9600);
}

void loop() {
   Serial.println("Hello, World, from avr-gcc!");
   delay(500);
}

そんで作成したSpecsファイルを使ってそれをコンパイルしてみます。今回はMPUがatmega328p 16MHzのArduino Uno R3向けです。

avr-gcc -specs=avr-gcc-arduino.specs -Os -Wall -mmcu=atmega328p -ffunction-sections -fdata-sections -DF_CPU=16000000L -DARDUINO=10807 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR -I/usr/share/arduino/hardware/arduino/avr/cores/arduino -I/usr/share/arduino/hardware/arduino/avr/variants/standard  /usr/share/arduino/hardware/arduino/avr/cores/arduino/*.cpp  /usr/share/arduino/hardware/arduino/avr/cores/arduino/*.c -x c++ hello.ino -o hello.elf

バイスにアップロードできるようにhex形式に変換します。

avr-objcopy -O ihex hello.elf hello.hex

バイスにアップロードするためのavrdudeをインストールします。

sudo apt install avrdude

avrdudeでアップロードします。今回のポートは /dev/ttyACM0 でした。

$ avrdude -c arduino -p atmega328p -b 115200 -P /dev/ttyACM0 -U flash:w:hello.hex

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.00s

avrdude: Device signature = 0x1e950f (probably m328p)
avrdude: NOTE: "flash" memory has been specified, an erase cycle will be performed
         To disable this feature, specify the -D option.
avrdude: erasing chip
avrdude: reading input file "hello.hex"
avrdude: input file hello.hex auto detected as Intel Hex
avrdude: writing flash (6502 bytes):

Writing | ################################################## | 100% 1.04s

avrdude: 6502 bytes of flash written
avrdude: verifying flash memory against hello.hex:
avrdude: load data flash data from input file hello.hex:
avrdude: input file hello.hex auto detected as Intel Hex
avrdude: input file hello.hex contains 6502 bytes
avrdude: reading on-chip flash data:

Reading | ################################################## | 100% 0.84s

avrdude: verifying ...
avrdude: 6502 bytes of flash verified

avrdude: safemode: Fuses OK (E:00, H:00, L:00)

avrdude done.  Thank you.

ためしにシリアルコンソールに繋いでみます:

$ tail -f /dev/ttyACM0
Hel, World, from avr-gcc!
Hello, World, from avr-gcc!
Hello, World, from avr-gcc!
Hello, World, from avr-gcc!
Hello, World, from avr-gcc!
Hello, World, from avr-gcc!
Hello, World, from avr-gcc!
Hello, World, from avr-gcc!

ちゃんと動いてますね。ただ機種固定しちゃって柔軟性が下がってたり、ヒューマンエラーが起きやすくなってたりするのであんまりオススメはしません。今後どうするかは迷い中です。

仮想アナログ時計作った

ずっと年月表示付きのアナログ時計が欲しかったんだけど何故か見つからなくて困ってたので自分で作ることに。ついでにずっと試したかったArduinoにもやっと手を出したので組み込んで見ました。

フォントは普段使いのフォントで、テクスチャはrawpixelで良い感じのCC0画像を探してたら消去法でこんな感じに。さてここからどうしよう。

github.com

 

GTKベースのStable Diffusionのフロントエンドを作った

ブログは放置状態でしたが裏ではちょっと機械学習に手を出してて、今日はGTKベースのStable Diffusionのフロントエンドを作ってました。機能は少ないですが高速かつプロンプトの編集に特化したのが特徴です(※GPUにも依ります)。
本格的な生成は機能豊富なWeb UIの方が良いですが、Web UIはキビキビさが足りなくてプロンプト実験のストレスになってたので、こういうの欲しいなぁと思ったらGTKの制限に引っかからずに(!)作れた感じです。
https://raw.githubusercontent.com/nazodane/gtk_stable_diffusion/master/screenshot.png

今のところまだLinuxでしか動かないはずなのでWindows版はプルリクエストに期待しましょう。

インストールはこちらから


pip install gtk-stable-diffusion

ソースコードはこちらから
github.com


0.0.2.1 でプロンプトの強調に対応しました。単語を Ctrl+↑/↓ すると強調と弱化が出来ます。

Shadertoyのシェーダを表示するBlenderアドオン作った

Shadertoyのシェーダを表示するBlenderアドオンを作ってみました。


github.com
まだ基本的なシェーダにしか対応していませんが中々良い感じに動いてます。ただBlender APIの制限も見えてきたので互換性を上げるのは大変そうな感じします…。

電卓のBlenderアドオンを作った

Gnome Calculatorを参考に電卓のアドオンを作ってみました。BlenderのUIに限界があるので妥協に妥協を重ねていますが、それなりに動く形には出来ました。
github.com
ここからどうするかは未定ですが、もっとPythonに寄せた方が良さそうだと思ったので、そっちを作りたいです。