{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "1487d453-698d-4f3a-a8b1-3f0b4e54c7e9",
   "metadata": {},
   "source": [
    "# Praktické ukážky v MicroPythone"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a3f33b6b-a5bf-4776-bb10-329f0ae98e92",
   "metadata": {},
   "source": [
    "<div style=\"text-align:center\">\n",
    "    <img src=\"obrazky/obalka.jpg\" width=45%/>\n",
    "</div>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "afbdc202-ed11-4473-b31f-17334d64258c",
   "metadata": {},
   "source": [
    "# MikroPython"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7fd4adcb-b1d9-4823-b73a-a701a6c8eae8",
   "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": "markdown",
   "id": "9b492736-7b54-40f7-8d8c-8abde7ca6392",
   "metadata": {
    "jp-MarkdownHeadingCollapsed": true
   },
   "source": [
    "# Úloha\n",
    "\n",
    "Implementujte kombinačný logický obvod (pozri obrázok) na mikrokontroléri ESP32 pomocou:\n",
    "\n",
    "* B-funkcie.\n",
    "* Pravdivostnej tabuľky."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ae05be27-ae4b-4d12-b8db-51e07928c1ee",
   "metadata": {},
   "source": [
    "<div style=\"text-align:center\">\n",
    "    $f(A,B,C) = \\bar A B \\bar C + \\bar A \\bar B C + ABC$\n",
    "</div>\n",
    "\n",
    "<div style=\"text-align:center\">\n",
    "    <img src=\"obrazky/blokova_schema.svg\" width=25%/>\n",
    "</div>\n",
    "\n",
    "\n",
    "<div style=\"text-align:center\">\n",
    "    Obr. 1: Bloková schéma kombinačného obvodu\n",
    "</div>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c610a04e-6fb5-46a1-be8c-2c24645d3bfd",
   "metadata": {
    "jp-MarkdownHeadingCollapsed": true
   },
   "source": [
    "# Riešenie"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1597a903-0cbd-4e29-96c8-cecd5c3b1c3b",
   "metadata": {},
   "source": [
    "<div style=\"text-align:center\">\n",
    "    <img src=\"obrazky/schema_zapojenia.png\" width=50%/>\n",
    "</div>\n",
    "\n",
    "<div style=\"text-align:center\">\n",
    "    Obr. 2: Schéma zapojenia hardvérového riešenia\n",
    "</div>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b8a81d77-1483-4bbf-abe4-ff7844df47d9",
   "metadata": {},
   "source": [
    "## Riešenie pomocou B-funkcie"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "747f22d6-bd70-4acb-9f75-97f7c963a10d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "..............................."
     ]
    }
   ],
   "source": [
    "%serialconnect\n",
    "\n",
    "from machine import Pin\n",
    "import time\n",
    "\n",
    "# definovanie vstupov a výstupov\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",
    "# hlavný program\n",
    "while True:\n",
    "    # načítanie hodnôt vstupov\n",
    "    A = pinA.value() \n",
    "    B = pinB.value()\n",
    "    C = pinC.value()\n",
    "    \n",
    "    # výpočet hodnoty vystupu pomocou B-funkcie\n",
    "    Y = (not A) and B and (not C) or       \\\n",
    "        (not A) and (not B) and C or       \\\n",
    "         A and B and C\n",
    "    \n",
    "    # alternatívny zápis\n",
    "    # Y = (not A) * B * (not C) + (not A) * (not B) * C + A * B * C\n",
    "    # Y = (~A) & B & (~C) | (~A) & (~B) & C | A & B & C\n",
    "  \n",
    "    # zapísanie výslednej hodnoty\n",
    "    pinY.value(Y)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c3f72b33-74a5-4a63-b273-92a30a8d3154",
   "metadata": {},
   "source": [
    "## Riešenie pomocou pravdivostnej tabuľky"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ec73b597-8744-4a5a-a1e1-eddaf5e3416c",
   "metadata": {},
   "outputs": [],
   "source": [
    "%serialconnect\n",
    "\n",
    "from machine import Pin\n",
    "import time\n",
    "\n",
    "# definovanie vstupov a výstupov\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 tabuľky\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)"
   ]
  }
 ],
 "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
}
