{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "b8a81d77-1483-4bbf-abe4-ff7844df47d9",
   "metadata": {},
   "source": [
    "## Príklad č. 1: Riadenie výstupov pomocou B-funkcií"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e6c39763-75e5-471b-a55a-a1f76bd2a1ba",
   "metadata": {},
   "source": [
    "<div style=\"text-align:center\">\n",
    "    <img src=\"obrazky/obalka.jpg\" width=70%/>\n",
    "</div>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5d556eae-608a-4158-b2b9-1e25508fbb76",
   "metadata": {},
   "source": [
    "* Zjednodušený Python pre mikrokontroléry.\n",
    "* Umožňuje ovládať piny, senzory a iné periférie.\n",
    "* Beží na doskách ako ESP32, ESP8266, STM32, Raspberry Pi Pico.\n",
    "* Má interaktívnu konzolu (REPL) – dá sa skúšať kód priamo.\n",
    "* Výhody: jednoduchý, prehľadný, rýchle prototypovanie.\n",
    "* Nevýhody: nižší výkon a väčšia pamäťová náročnosť než C/C++.\n",
    "* Oficiálna dokumentácia: https://docs.micropython.org/en/latest/esp32/tutorial/intro.html"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "747f22d6-bd70-4acb-9f75-97f7c963a10d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[34mConnecting to --port=COM3 --baud=115200 \u001b[0m\n",
      "\u001b[34mReady.\n",
      "\u001b[0m..............................................\u001b[34m\n",
      "\n",
      "*** Sending Ctrl-C\n",
      "\n",
      "\u001b[0m"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Traceback (most recent call last):\n",
      "  File \"<stdin>\", line 23, in <module>\n",
      "KeyboardInterrupt: \n"
     ]
    }
   ],
   "source": [
    "%serialconnect\n",
    "\n",
    "from machine import Pin\n",
    "import time\n",
    "\n",
    "# definovanie vstupov a vystupov\n",
    "pinA = Pin(25, Pin.IN)\n",
    "pinB = Pin(26, Pin.IN)\n",
    "pinC = Pin(27, Pin.IN)\n",
    "pinY = Pin(18, Pin.OUT)\n",
    "\n",
    "# hlavny program\n",
    "while True:\n",
    "    # nacitanie hodnot vstupov\n",
    "    A = pinA.value() \n",
    "    B = pinB.value()\n",
    "    C = pinC.value()\n",
    "    \n",
    "    # vypocet hodnoty vystupu pomocou funkcie\n",
    "    Y = A * (not B) + C + (not A) * B * C\n",
    "    # Y = A and (not B) or C or (not A) and B and C\n",
    "    # Y = A & (~B) | C | (~A) & B & C\n",
    "    \n",
    "    # zapisanie vyslednej hodnoty\n",
    "    pinY.value(Y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2da2da3b-f309-4877-9012-80a8b6aa9a52",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "c3f72b33-74a5-4a63-b273-92a30a8d3154",
   "metadata": {},
   "source": [
    "## Príklad č. 2: Riadenie výstupov pomocou pravdivostnej tabuľky"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "ec73b597-8744-4a5a-a1e1-eddaf5e3416c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[34mConnecting to --port=COM6 --baud=115200 \u001b[0m\n",
      "MicroPython v1.22.2 on 2024-02-22; Generic ESP32 module with ESP32\n",
      "Type \"help()\" for more information.\n",
      ">>>[reboot detected 0]repl is in normal command mode\n",
      "[\\r\\x03\\x03] b'\\r\\n>>> '\n",
      "[\\r\\x01] b'\\r\\n>>> \\r\\nraw REPL; CTRL-B to exit\\r\\n>' \u001b[34mReady.\n",
      "\u001b[0m................................................................................................\n",
      "**[ys] <class 'serial.serialutil.SerialException'>\n",
      "**[ys] ClearCommError failed (PermissionError(13, 'Access is denied.', None, 5))\n",
      "\n",
      "\n",
      "**[ys] <class 'serial.serialutil.SerialException'>\n",
      "**[ys] ClearCommError failed (PermissionError(13, 'Access is denied.', None, 5))\n",
      "\n"
     ]
    }
   ],
   "source": [
    "%serialconnect\n",
    "\n",
    "from machine import Pin\n",
    "import time\n",
    "\n",
    "# definovanie vstupov a vystupov\n",
    "pinA = Pin(25, Pin.IN)\n",
    "pinB = Pin(26, Pin.IN)\n",
    "pinC = Pin(27, Pin.IN)\n",
    "pinY = Pin(5, Pin.OUT)\n",
    "\n",
    "# definovanie pravdivostnej tabulky\n",
    "tab = {\n",
    "  (0, 0, 0): 0,\n",
    "  (0, 0, 1): 1,\n",
    "  (0, 1, 0): 1,\n",
    "  (0, 1, 1): 0,\n",
    "  (1, 0, 0): 0,\n",
    "  (1, 0, 1): 0,\n",
    "  (1, 1, 0): 0,\n",
    "  (1, 1, 1): 1\n",
    "}\n",
    "\n",
    "# hlavny program\n",
    "while True:\n",
    "    # nacitanie hodnot vstupov\n",
    "    A = pinA.value() \n",
    "    B = pinB.value()\n",
    "    C = pinC.value()\n",
    "    \n",
    "    # vypocet hodnoty vystupu pomocou tabulky\n",
    "    Y = tab[A, B, C]\n",
    "    \n",
    "    # zapisanie vyslednej hodnoty\n",
    "    pinY.value(Y)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ae05be27-ae4b-4d12-b8db-51e07928c1ee",
   "metadata": {},
   "source": [
    "<div style=\"text-align:center\">\n",
    "    <img src=\"images/schematic1.png\" width=70%/>\n",
    "</div>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0dd3c112-d34b-4f25-92b5-b56eef478cc6",
   "metadata": {},
   "source": [
    "$a^2 = b + c$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f031b8a5-fdb4-4be0-982c-aec47d72f41e",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "MicroPython - USB",
   "language": "micropython",
   "name": "micropython"
  },
  "language_info": {
   "codemirror_mode": "python",
   "file_extension": ".py",
   "mimetype": "text/python",
   "name": "micropython"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
