Skip to content

Commit 7a88b93

Browse files
committed
doc and sample for custom scripting
1 parent d98dcc7 commit 7a88b93

File tree

3 files changed

+126
-8
lines changed

3 files changed

+126
-8
lines changed

README.md

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -331,16 +331,21 @@ will show you all the i/o ports you can play with.
331331

332332
## Initialization
333333

334-
When the DemoBoard object is created, you _may_ give it a parameter to indicate how you intend to use it.
335334

336-
If not specifed, the value in [config.ini](src/config.ini) DEFAULT section `mode` will be used.
335+
Since only _one_ demoboard exists, in general it's safer and more efficient to use an accessor method to get ahold of the singleton:
337336

337+
```
338+
tt = DemoBoard.get()
339+
```
340+
341+
The first time this is used, the instance is created, using the `mode` setting in the DEFAULT section of [config.ini](src/config.ini) to define how it's being used (i.e. the RP2 driving the inputs or using external means).
338342

343+
When the DemoBoard object is created, you _may_ give it a parameter to indicate how you intend to use it, rather than rely on config.ini.
339344
Possible values are:
340345

341346
```
342347
# use ini value
343-
tt = DemoBoard() # whatever was in DEFAULT.mode of config.ini
348+
tt = DemoBoard.get() # whatever was in DEFAULT.mode of config.ini
344349
345350
346351
# safe mode, the default
@@ -357,10 +362,10 @@ tt = DemoBoard(RPMode.ASIC_RP_CONTROL)
357362
# ASIC drives only management pins all else are inputs
358363
tt = DemoBoard(RPMode.ASIC_MANUAL_INPUTS)
359364
360-
361-
362365
```
363366

367+
and from that point on, you can use the `DemoBoard.get()` to access that object.
368+
364369
If you've played with the pin mode (direction), you've loaded a project that modified the mode or you just want to change modes, you can set the attribute
365370

366371
```
@@ -369,6 +374,35 @@ If you've played with the pin mode (direction), you've loaded a project that mod
369374
tt.mode = RPMode.ASIC_RP_CONTROL
370375
```
371376

377+
378+
### Write Your Own Script
379+
380+
The default `main.py` does basic setup, reads the config.ini, loads the default project etc.
381+
382+
If you want to have some particular code run on boot, you can write your own script. There's a little bit of required boilerplate to get this working, as the SDK needs to figure out which board it's running on and which carrier/chip is present.
383+
384+
A full example that you can use for this is in [custom script main.py](./examples/custom_script_main.py), and you might want to start there.
385+
386+
There are some useful hints in there, related to memory, logging and more, but the short of it is that what you really need is:
387+
388+
```
389+
# to load the detector and demoboard modules
390+
391+
from ttboard.boot.demoboard_detect import DemoboardDetect
392+
from ttboard.demoboard import DemoBoard
393+
394+
# to first probe the board/breakout
395+
DemoboardDetect.probe()
396+
397+
# and then get a handle to the DemoBoard object
398+
tt = DemoBoard.get()
399+
400+
# pretty much it
401+
```
402+
403+
See the [custom script main.py](./examples/custom_script_main.py) for all the details.
404+
405+
372406
### Automatic Load and Default Config
373407

374408
The [config.ini](src/config.ini) file has a **DEFAULT** section that may be used to specify the demo board mode, default project to enable and other things.

src/examples/custom_script_main.py

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
'''
2+
Created on Nov 13, 2025
3+
4+
Sample boilerplate for custom scripts
5+
6+
7+
@author: Pat Deegan
8+
@copyright: Copyright (C) 2025 Pat Deegan, https://psychogenic.com
9+
'''
10+
11+
12+
### Memory protection ###
13+
# Reading in a bunch of files tends to frag the memory
14+
# so we can set a low threshold on the garbage collector
15+
# here, and clean up post init
16+
import gc
17+
# stash the current value for garbage
18+
GCThreshold = gc.threshold()
19+
# start very aggressive, to keep thing defragged
20+
# as we read in ini and JSON files, etc
21+
gc.threshold(80000)
22+
23+
24+
25+
26+
### Logging ###
27+
# uPython has no logging built-in, but we have an implementation
28+
# that behaves nicely here, if you want it
29+
import ttboard.log as logging
30+
logging.basicConfig(level=logging.DEBUG)
31+
32+
33+
### TT demoboard ###
34+
# this is what you need to detect the board/shuttle and
35+
# get access to the DemoBoard object
36+
from ttboard.boot.demoboard_detect import DemoboardDetect
37+
from ttboard.demoboard import DemoBoard
38+
# load anything else you need...
39+
40+
41+
# clean up memory
42+
gc.collect()
43+
# end by being so aggressive on collection
44+
gc.threshold(GCThreshold)
45+
46+
47+
48+
49+
### init code ###
50+
# some globals
51+
# logging handle
52+
log = logging.getLogger(__name__)
53+
# TT demoboard handle
54+
tt = None # instantiate this after probing
55+
56+
# detect the board, carrier and shuttle
57+
if DemoboardDetect.probe():
58+
log.info('Detected ' + DemoboardDetect.PCB_str())
59+
else:
60+
log.error('Hm, could not figure out the DB/shuttle?')
61+
62+
# create and access the TT demoboard singleton
63+
tt = DemoBoard.get()
64+
65+
66+
67+
68+
69+
### user code ###
70+
# do whatever you like here
71+
# say load a project, set input byte and start clocking
72+
73+
log.info("Loading factory test")
74+
tt.shuttle.tt_um_factory_test.enable()
75+
tt.ui_in = 1
76+
tt.clock_project_PWM(1e6) # clock it at 1M
77+
78+
print(tt.uo_out)
79+
80+
81+
82+
83+

src/main.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88
99
If you want to use the SDK, all
1010
you really need is something like
11-
12-
tt = DemoBoard()
11+
12+
DemoboardDetect.probe()
13+
tt = DemoBoard.get()
1314
1415
Then you can
1516
# enable test project
@@ -50,7 +51,7 @@ def startup():
5051
# construct DemoBoard
5152
# either pass an appropriate RPMode, e.g. RPMode.ASIC_RP_CONTROL
5253
# or have "mode = ASIC_RP_CONTROL" in ini DEFAULT section
53-
ttdemoboard = DemoBoard()
54+
ttdemoboard = DemoBoard.get()
5455
print("\n\n")
5556
print(f"The '{colors.color('tt', 'red')}' object is available.")
5657
print()

0 commit comments

Comments
 (0)