概要
TunerStudioを、究める。
一から、iniファイルを作ってみる。
ダッシュボードのゲージが全部動く、プログラム。
spiからfpgaを叩いてみた。
写真
サンプルコード
from machine import Pin, SPI
import time
import shrike
import sys
import math
import struct
import uselect
signature = 'MShift v0.01'
SCK = 2
CS = 1
MOSI = 3
MISO = 0
FPGA_RESET = 14
shrike.reset()
shrike.flash("template.bin")
reset_pin = Pin(FPGA_RESET, Pin.OUT, value = 1)
reset_pin.value(0)
time.sleep_ms(100)
reset_pin.value(1)
time.sleep_ms(100)
cs = Pin(CS, Pin.OUT, value = 1)
spi = SPI(0, baudrate = 1_000_000, polarity = 0, phase = 0, bits = 8, firstbit = SPI.MSB, sck = Pin(SCK), mosi = Pin(MOSI), miso = Pin(MISO))
def spi_exchange(value):
tx = bytes([value])
rx = bytearray(1)
cs.value(0)
spi.write_readinto(tx, rx)
cs.value(1)
return rx[0]
def get_outputs():
t = time.ticks_ms()
sin_fast = math.sin(t * 2.0)
sin_slow = math.sin(t * 0.63)
rpm = max(0, int(3000 + sin_slow * 2000))
tdc = float(10.0 + sin_fast * 10.0)
ign = float(25.0 + sin_slow * 15.0)
dwell = float(3.5 + sin_fast * 0.5)
inj_ang = 90.0
inj_dur = float(7.0 + sin_slow * 5.0)
air = 0
clt = int(500 + sin_slow * 100)
tps = int(512 + sin_slow * 511)
map_raw = int(400 + sin_slow * 300)
batt = int(835 + sin_fast * 65)
o2 = int(7 + sin_slow * 5)
o2 = spi_exchange(o2)
status = 0b00100001 if (int(t) % 2 == 1) else 0b00000100
return struct.pack('<HfffffHHHHHHH', rpm, tdc, ign, dwell, inj_ang, inj_dur, air, clt, tps, map_raw, batt, o2, status)
poll = uselect.poll()
poll.register(sys.stdin, uselect.POLLIN)
while True:
if poll.poll(100):
input_byte = sys.stdin.buffer.read(1)
if input_byte == b'F':
print(signature, end='')
elif input_byte == b'C':
print(signature, end='')
elif input_byte == b'O':
data_block = get_outputs()
sys.stdout.buffer.write(data_block)
以上。
