Exemplo n.º 1
0
// StringToBytes converts the body into a slice of bytes with the given charset encoding.
//
// CRLF line break is enforced.
// Line break are inserted if a line is longer than 1000 characters (including CRLF).
func StringToBody(str, encoding string) ([]byte, error) {
	in := bufio.NewScanner(bytes.NewBufferString(str))
	out := new(bytes.Buffer)

	var err error
	var line []byte
	for in.Scan() {
		line = in.Bytes()
		for {
			// Lines can not be longer that 1000 characters including CRLF.
			n := min(len(line), 1000-2)

			out.Write(line[:n])
			out.WriteString("\r\n")

			line = line[n:]
			if len(line) == 0 {
				break
			}
		}
	}

	translator, err := charset.TranslatorTo(encoding)
	if err != nil {
		return out.Bytes(), err
	}

	_, translated, err := translator.Translate(out.Bytes(), true)
	return translated, err
}
Exemplo n.º 2
0
func (lcd *LCD) WriteString(s string, sleepChar, sleepWord int) {
	tr, _ := charset.TranslatorTo("windows-1251")
	_, s1251, _ := tr.Translate([]byte(s), true)

	for _, r := range s1251 {
		if r != '\n' {
			lcd.Data(byte(r))
		} else {
			lcd.CommandAddress(0x40)
		}
		if r == ' ' && sleepWord > 0 {
			time.Sleep(time.Duration(sleepWord) * time.Millisecond)
		} else if sleepChar > 0 {
			time.Sleep(time.Duration(sleepChar) * time.Millisecond)
		}
	}
}