dcc multi-train, flutter, raspberry pi pico, bluetooth, source in the description

111 Просмотры
Издатель
dcc multi-train, flutter, raspberry pi pico, bluetooth, source in the description
replace _GT_ with greater than symbol
replace _LT_ with less than symbol
youtube dont like angle brackets
formatted code with angle brackets -- http://crus.in/codes/dcc_multi.txt

dcc booster - https://www.pololu.com/product/1212
15v power supply - http://www.amazon.com/exec/obidos/ASIN/B08N4CQKFX
controller - $4 raspberry pi pico

#micropython program on the raspberry pi pico
import array, time, rp2, struct
from machine import UART, Pin

uart=UART(1,9600)
packet = bytearray(b'\xff\xff\xfe\xff\xff\xff\xff\xff')
idle = bytearray(b'\xff\xfd\xfe\x00\x7f\xc0\x00\x00')
buffer = bytearray(b'\x00\x00\x00')

def init():
global packet
packet = bytearray(b'\xff\xff\xfe\xff\xff\xff\xff\xff')

def assemble_packet(address, speed):
# bit counts starts at zero
global packet
checksum = address ^ speed
packet[3] = address
packet[4] = speed _GT__GT_ 1
packet[5] = checksum _GT__GT_ 2

if not((speed _GT__GT_ 7) & 1):
packet[5] |= 1 _LT__LT_ 7

temp = checksum _LT__LT_ 6
packet[6] = temp | 0x3f

@rp2.asm_pio(set_init=(rp2.PIO.OUT_LOW,rp2.PIO.OUT_HIGH), out_shiftdir=rp2.PIO.SHIFT_LEFT, autopull=True)
def dcc():
label("bitloop")
set(pins,1) [20]
out(x, 1)
jmp(not_x, "do_zero")
set(pins,2) [21]
jmp("bitloop")
label("do_zero")
nop() [16]
set(pins,2) [30]
nop() [8]

sm = rp2.StateMachine(0, dcc, freq=400000, set_base=Pin(16))
sm.active(1)

while True:
if uart.any() _GT_ 0:
uart.readinto(buffer)
if buffer[0] == 0x24:
init()
assemble_packet(buffer[1],buffer[2])
w1,w2=struct.unpack('_GT_II',packet)
sm.put(w1)
sm.put(w2)
time.sleep_ms(5)

else:
w1,w2=struct.unpack('_GT_II',idle)
sm.put(w1)
sm.put(w2)
time.sleep_ms(5)

// android app flutter program
//// filename: main.dart
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:group_button/group_button.dart';
import './blecontroller.dart';

void main() =_GT_ runApp(GetMaterialApp(home: Home()));

class Home extends StatelessWidget {
@override Widget build(context) {
final BleController ble = Get.put(BleController());

void processSantaFe(int i){switch(i){
case 0:{ble.send(0);}break;
case 1:{ble.send(1);}break;
case 2:{ble.send(2);}break;}}

void processSwitcher(int i){switch(i){
case 0:{ble.send(3);}break;
case 1:{ble.send(4);}break;
case 2:{ble.send(5);}break;}}

return Scaffold(
appBar: AppBar(title: Text('DCC multi train demo')),
body: Column(children:[

SizedBox(height:50),
ElevatedButton(onPressed:ble.connect,child:Obx(()=_GT_Text('${ble.status.value}'))),
SizedBox(height:50),

Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children:[Text('Santa Fe',style:TextStyle(fontSize:40)),
GroupButton(isRadio:true,spacing:10,
onSelected:(index,isSelected)=_GT_processSantaFe(index),
buttons:["Stop","Forward","Reverse"],
selectedTextStyle: TextStyle(fontSize:40,color:Colors.red),
unselectedTextStyle: TextStyle(fontSize:40,color:Colors.blue),
borderRadius:BorderRadius.circular(5.0),),]),

SizedBox(height:60),

Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children:[Text('Switcher',style:TextStyle(fontSize:40)),
GroupButton(isRadio:true,spacing:10,
onSelected:(index,isSelected)=_GT_processSwitcher(index),
buttons:["Stop","Forward","Reverse"],
selectedTextStyle: TextStyle(fontSize:40,color:Colors.red),
unselectedTextStyle: TextStyle(fontSize:40,color:Colors.blue),
borderRadius:BorderRadius.circular(5.0),),]),

]),);}}

//// filename: blecontroller.dart
import 'package:flutter/material.dart';
import 'package:flutter_reactive_ble/flutter_reactive_ble.dart';
import 'package:get/get.dart';
import 'dart:async';

class BleController {
final frb = FlutterReactiveBle();
late StreamSubscription _LT_ConnectionStateUpdate_GT_ c;
late QualifiedCharacteristic tx;
List_LT_int_GT_v=[0x24,0x00,0x00];
var status = 'connect to bluetooth'.obs;

send(int i) async {
switch(i){
case 0: {v[1]=0x14;v[2]=0x41;}break;
case 1: {v[1]=0x14;v[2]=0x75;}break;
case 2: {v[1]=0x14;v[2]=0x55;}break;
case 3: {v[1]=0x03;v[2]=0x41;}break;
case 4: {v[1]=0x03;v[2]=0x75;}break;
case 5: {v[1]=0x03;v[2]=0x55;}break;}
await frb.writeCharacteristicWithoutResponse(tx,value:v);}

void connect() async {
status.value = 'connecting...';
c = frb.connectToDevice(id: 'A4:DA:32:55:06:1E').listen((state){
if (state.connectionState == DeviceConnectionState.connected){
status.value = 'connected!';

tx = QualifiedCharacteristic(
serviceId: Uuid.parse("0000ffe0-0000-1000-8000-00805f9b34fb"),
characteristicId: Uuid.parse("0000ffe1-0000-1000-8000-00805f9b34fb"),
deviceId:'A4:DA:32:55:06:1E');}});}}
Категория
Язык программирования Dart
Комментариев нет.