Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions examples/pybadge/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package main

import (
"fmt"
"image/color"
"time"

"machine"

"tinygo.org/x/drivers/st7735"

"tinygo.org/x/tinyfont/proggy"
"tinygo.org/x/tinyterm"
)

var (
display = st7735.New(machine.SPI1, machine.TFT_RST, machine.TFT_DC, machine.TFT_CS, machine.TFT_LITE)

terminal = tinyterm.NewTerminal(&display)

black = color.RGBA{0, 0, 0, 255}
white = color.RGBA{255, 255, 255, 255}
red = color.RGBA{255, 0, 0, 255}
blue = color.RGBA{0, 0, 255, 255}
green = color.RGBA{0, 255, 0, 255}

font = &proggy.TinySZ8pt7b
)

func main() {
machine.SPI1.Configure(machine.SPIConfig{
SCK: machine.SPI1_SCK_PIN,
SDO: machine.SPI1_SDO_PIN,
SDI: machine.SPI1_SDI_PIN,
Frequency: 8000000,
})

display.Configure(st7735.Config{
Rotation: st7735.ROTATION_90,
})

width, height := display.Size()
_, _ = width, height

display.FillScreen(black)

terminal.Configure(&tinyterm.Config{
Font: font,
FontHeight: 10,
FontOffset: 6,
UseSoftwareScroll: true,
})
for {
time.Sleep(50 * time.Millisecond)
fmt.Fprintf(terminal, "\ntime: %d", time.Now().UnixNano())
}
}
24 changes: 15 additions & 9 deletions tinyterm.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package tinyterm
import (
"bytes"
"fmt"
"image"
"image/color"
"strconv"
"strings"
Expand All @@ -30,7 +29,6 @@ type Terminal struct {
height int16
scroll int16

//buffer []byte // buffer for the next that has been written to the terminal
rows int16 // number of rows in the text buffer
cols int16 // number of columns in the text buffer
next int16 // index in the buffer at which next char will be put
Expand All @@ -44,17 +42,18 @@ type Terminal struct {
fontWidth int16
fontHeight int16
fontOffset int16

useSoftwareScroll bool
}

type Config struct {
ScreenBounds image.Rectangle
Font *tinyfont.Font
FontHeight int16
FontOffset int16
Font *tinyfont.Font
FontHeight int16
FontOffset int16
UseSoftwareScroll bool
}

func (t *Terminal) Configure(config *Config) {

t.state = stateInput
t.params = bytes.NewBuffer(make([]byte, 32))

Expand All @@ -71,7 +70,7 @@ func (t *Terminal) Configure(config *Config) {
t.rows = t.height / t.fontHeight
t.cols = t.width / t.fontWidth

//t.buffer = make([]byte, t.rows*t.cols)
t.useSoftwareScroll = config.UseSoftwareScroll
t.scroll = t.fontHeight
t.lf()
}
Expand Down Expand Up @@ -290,6 +289,13 @@ func (t *Terminal) cr() {
func (t *Terminal) lf() {
t.next = 0
t.scroll = (t.scroll + t.fontHeight) % (t.rows * t.fontHeight)
t.display.SetScroll((t.scroll + t.fontHeight) % t.height)
if t.useSoftwareScroll {
// blank screen if we have reached bottom
if t.scroll == 0 {
t.display.FillRectangle(0, 0, t.width, t.height, t.attrs.bgcol)
}
} else {
t.display.SetScroll((t.scroll + t.fontHeight) % t.height)
}
t.display.FillRectangle(0, t.scroll, t.width, t.fontHeight, t.attrs.bgcol)
}