@startuml
title Enkodovanie UART: encode_uart_byte_cfg

start

:set_high = 1 << pin_bit;
:set_low = 1 << (pin_bit + 16);
:nbits = cfg.data_bits, idx = 0;

:START bit (LOW)\nout[idx++] = set_low;

repeat :Data bity (LSB first)
  :out[idx++] = ((data >> i) & 1) ?\nset_high : set_low;
repeat while (i < nbits?) is (ano)
->nie;

if (cfg.parity != None?) then (ano)
  :ones = data.count_ones() & 1;
  :par_bit = (Even) ? ones==1 : ones==0;
  :out[idx++] = par_bit ? set_high : set_low;
endif

repeat :STOP bity (HIGH)
  :out[idx++] = set_high;
repeat while (zvysne stop bity?) is (ano)
->nie;

:Return idx;

stop
@enduml
