<commit_before>package main

import (
	"io"
	"rtos"

	"stm32/hal/dma"
	"stm32/hal/gpio"
	"stm32/hal/irq"
	"stm32/hal/system"
	"stm32/hal/system/timer/systick"
	"stm32/hal/usart"
)

var tts *usart.Driver

func init() {
	system.SetupPLL(8, 1, 48/8)
	systick.Setup(2e6)

	gpio.A.EnableClock(true)
	tx := gpio.A.Pin(9)

	tx.Setup(&gpio.Config{Mode: gpio.Alt})
	tx.SetAltFunc(gpio.USART1_AF1)
	d := dma.DMA1
	d.EnableClock(true)
	tts = usart.NewDriver(usart.USART1, d.Channel(2, 0), nil, nil)
	tts.Periph().EnableClock(true)
	tts.Periph().SetBaudRate(115200)
	tts.Periph().Enable()
	tts.EnableTx()

	rtos.IRQ(irq.USART1).Enable()
	rtos.IRQ(irq.DMA1_Channel2_3).Enable()
}

type MorseWriter struct {
	W io.Writer
}

func (w *MorseWriter) Write(s []byte) (n int, err error) {
	var buf [8]byte
	for _, c := range s {
		if c < ' ' {
			continue
		}
		if 'a' <= c && c <= 'z' {
			c -= 'a' - 'A' // Convert to upper case.
		}
		if c > 'Z' {
			continue
		}
		var symbol morseSymbol
		if c == ' ' {
			symbol.length = 2
			buf[0] = ' '
			buf[1] = ' '
		} else {
			symbol = morseSymbols[c-'!']
			for i := uint(0); i < uint(symbol.length); i++ {
				if (symbol.code>>i)&1 != 0 {
					buf[i] = '-'
				} else {
					buf[i] = '.'
				}
			}
		}
		buf[symbol.length] = ' '
		m, err := w.W.Write(buf[:symbol.length+1])
		n += m
		if err != nil {
			return n, err
		}
	}
	return n, nil
}

func main() {
	w := &MorseWriter{tts}
	io.WriteString(w, "Hello, World!\r\n")
}

func ttsISR() {
	tts.ISR()
}

func ttsDMAISR() {
	tts.TxDMAISR()
}

//c:__attribute__((section(".ISRs")))
var ISRs = [...]func(){
	irq.USART1:          ttsISR,
	irq.DMA1_Channel2_3: ttsDMAISR,
}

type morseSymbol struct {
	code, length byte
}

//emgo:const
var morseSymbols = [...]morseSymbol{
	{1<<0 | 1<<1 | 1<<2, 4}, // ! ---.<commit_msg>examples/f030-demo-board/morseuart: Small change.<commit_after>package main

import (
	"io"
	"rtos"

	"stm32/hal/dma"
	"stm32/hal/gpio"
	"stm32/hal/irq"
	"stm32/hal/system"
	"stm32/hal/system/timer/systick"
	"stm32/hal/usart"
)

var tts *usart.Driver

func init() {
	system.SetupPLL(8, 1, 48/8)
	systick.Setup(2e6)

	gpio.A.EnableClock(true)
	tx := gpio.A.Pin(9)

	tx.Setup(&gpio.Config{Mode: gpio.Alt})
	tx.SetAltFunc(gpio.USART1_AF1)
	d := dma.DMA1
	d.EnableClock(true)
	tts = usart.NewDriver(usart.USART1, d.Channel(2, 0), nil, nil)
	tts.Periph().EnableClock(true)
	tts.Periph().SetBaudRate(115200)
	tts.Periph().Enable()
	tts.EnableTx()

	rtos.IRQ(irq.USART1).Enable()
	rtos.IRQ(irq.DMA1_Channel2_3).Enable()
}

type MorseWriter struct {
	W io.Writer
}

func (w *MorseWriter) Write(s []byte) (n int, err error) {
	var buf [8]byte
	for _, c := range s {
		if c < ' ' {
			continue
		}
		if 'a' <= c && c <= 'z' {
			c -= 'a' - 'A' // Convert to upper case.
		}
		if c > 'Z' {
			continue
		}
		var symbol morseSymbol
		if c == ' ' || c == '\n' {
			symbol.length = 2
			buf[0] = ' '
			buf[1] = ' '
		} else {
			symbol = morseSymbols[c-'!']
			for i := uint(0); i < uint(symbol.length); i++ {
				if (symbol.code>>i)&1 != 0 {
					buf[i] = '-'
				} else {
					buf[i] = '.'
				}
			}
		}
		buf[symbol.length] = ' '
		m, err := w.W.Write(buf[:symbol.length+1])
		n += m
		if err != nil {
			return n, err
		}
	}
	return n, nil
}

func main() {
	s := "Hello, World!\r\n"
	w := &MorseWriter{tts}

	io.WriteString(tts, s)
	io.WriteString(w, s)
}

func ttsISR() {
	tts.ISR()
}

func ttsDMAISR() {
	tts.TxDMAISR()
}

//c:__attribute__((section(".ISRs")))
var ISRs = [...]func(){
	irq.USART1:          ttsISR,
	irq.DMA1_Channel2_3: ttsDMAISR,
}

type morseSymbol struct {
	code, length byte
}

//emgo:const
var morseSymbols = [...]morseSymbol{
	{1<<0 | 1<<1 | 1<<2, 4}, // ! ---.
{'fraction_non_alphanumeric': 0.20332063146434404, 'fraction_numerical': 0.030756668481219378, 'mean_word_length': 2.860294117647059, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1215733', 'n_tokens_mistral': 1864, 'n_tokens_neox': 1699, 'n_words': 318}