Skip to content

Instantly share code, notes, and snippets.

@cathalmccabe
Created March 9, 2018 11:05
Show Gist options
  • Save cathalmccabe/1552d55c229272355cf5ed9382f46ee7 to your computer and use it in GitHub Desktop.
Save cathalmccabe/1552d55c229272355cf5ed9382f46ee7 to your computer and use it in GitHub Desktop.
My test notebook
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
"# Demonstration of boolean functions using the logic tools overlay\n",
"\n",
"## Requirements\n",
"\n",
"*The demo uses the Logictools overlay included in the Pynq-Z1 v2.0 image\n",
"\n",
"## Slideshow\n",
"\n",
"This demo is intended to be run as a slideshow. \n",
"\n",
"Go to View > Cell Toolbar > Slideshow to see the *Slideshow options* for each cell. From here you can select which slides will be included or excluded from the presentation. \n",
"\n",
"Code cells can be executed from the slideshow view by clicking the code cell, and pressing CTRL + ENTER\n",
"\n",
"\n",
"## Instructions to run the demo:\n",
"\n",
"* Before entering the slideshow view, click Cell > Run All to execute all the code in the notebook. \n",
"\n",
"In slideshow mode, use the cursor keys (left right) to navigate through the presentation\n",
"\n",
"* Press Alt + r to enter the slideshow and hide this view. (Exit slideshow mode with the same keys Alt + r)\n"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": true,
"slideshow": {
"slide_type": "skip"
}
},
"outputs": [],
"source": [
"import ipywidgets as widgets\n",
"from ipywidgets import interact\n",
"from IPython.display import display\n",
"\n",
"from pynq.overlays.logictools import LogicToolsOverlay\n",
"logictools_olay = LogicToolsOverlay('logictools.bit') # Download the logictools overlay"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": true,
"slideshow": {
"slide_type": "skip"
}
},
"outputs": [],
"source": [
"def update_boolean_generator(sender):\n",
" led = output.value.replace(\"E\", \"\") # Change LED to LD\n",
" led = led.replace(\"NOT \", \"~\") \n",
" \n",
" logic = []\n",
" for i in [logic0.value, logic1.value, logic2.value]:\n",
" if i is 'AND' :\n",
" logic.append('&')\n",
" elif i is 'OR' :\n",
" logic.append('|')\n",
" elif i is 'XOR' :\n",
" logic.append('^')\n",
" \n",
" inputs = []\n",
" for i in [input0.value, input1.value, input2.value, input3.value]:\n",
" if i is not '-' :\n",
" i = i.replace(\"NOT \", \"~\") \n",
" inputs.append(i)\n",
" \n",
" #Build logic function\n",
" # Assumes inputs[0] always exists\n",
" function = led + '=' + inputs[0]\n",
"\n",
" for i, j in zip(logic, inputs[1:4]):\n",
" function = function + i + j\n",
"\n",
" # Apply the function to the boolean generator \n",
" boolean_generator = logictools_olay.boolean_generator\n",
" boolean_generator.reset()\n",
" boolean_generator.setup([function])\n",
" boolean_generator.run()"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": true,
"slideshow": {
"slide_type": "skip"
}
},
"outputs": [],
"source": [
"output = widgets.Select(\n",
" options=['LED0', 'LED1', 'LED2', 'LED3'],\n",
" value='LED0',\n",
" description='LED',\n",
" disabled=False)\n",
"output.layout.width='400px'\n",
"\n",
"input0 = widgets.Select(\n",
" options=['PB0', 'PB1', 'PB2', 'PB3', 'NOT PB0', 'NOT PB1', 'NOT PB2', 'NOT PB3', '-'],\n",
" value='PB0',\n",
" #description=' = ',\n",
" disabled=False)\n",
"\n",
"input1 = widgets.Select(\n",
" options=['PB0', 'PB1', 'PB2', 'PB3', 'NOT PB0', 'NOT PB1', 'NOT PB2', 'NOT PB3', '-'],\n",
" value='PB1',\n",
" #description='PB',\n",
" disabled=False)\n",
"\n",
"input2 = widgets.Select(\n",
" options=['PB0', 'PB1', 'PB2', 'PB3', 'NOT PB0', 'NOT PB1', 'NOT PB2', 'NOT PB3', '-'],\n",
" value='-',\n",
" #description='PB',\n",
" disabled=False)\n",
"\n",
"input3 = widgets.Select(\n",
" options=['PB0', 'PB1', 'PB2', 'PB3', 'NOT PB0', 'NOT PB1', 'NOT PB2', 'NOT PB3', '-'],\n",
" value='-',\n",
" #description='PB',\n",
" disabled=False)\n",
"\n",
"logic0 = widgets.Select(\n",
" options=['AND', 'OR', 'XOR', '-'],\n",
" value='AND',\n",
" disabled=False)\n",
"\n",
"logic1 = widgets.Select(\n",
" options=['AND', 'OR', 'XOR', '-'],\n",
" value='-',\n",
" disabled=False)\n",
"\n",
"logic2 = widgets.Select(\n",
" options=['AND', 'OR', 'XOR', '-'],\n",
" value='-',\n",
" disabled=False)\n",
"\n",
"output.observe(update_boolean_generator)\n",
"logic0.observe(update_boolean_generator)\n",
"logic1.observe(update_boolean_generator)\n",
"logic2.observe(update_boolean_generator)\n",
"input0.observe(update_boolean_generator)\n",
"input1.observe(update_boolean_generator)\n",
"input2.observe(update_boolean_generator)\n",
"input3.observe(update_boolean_generator)\n",
"\n",
"items = [output, input0, logic0, input1, logic1, input2, logic2, input3]\n",
"update_boolean_generator(_)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"### Make a boolean function \n",
"\n",
"Inputs can be **PB0**, **PB1**, **PB2**, **PB3** (and their inverse **NOT PB0**, **NOT PB1** ...) for each of the 4 pushbuttons\n",
"\n",
"Outputs can be **LD0**, **LD1**, **LD2**, **LD3** for each of the four LEDs\n",
"\n",
"Operations can be \n",
"* **&** : AND\n",
"* **|** : OR\n",
"* **^** : XOR "
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"Use the dropdown boxes to select the logic function that will be implemented using Pushbuttons, and LEDs as input and output"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "c3cbb104190040adb272b281883a8b0d"
}
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"widgets.Box(items)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"source": [
"Test the logic function by pressing the pushbuttons on the board and check the correct LED lights up."
]
}
],
"metadata": {
"celltoolbar": "Slideshow",
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.3"
},
"widgets": {
"application/vnd.jupyter.widget-state+json": {
"state": {},
"version_major": 1,
"version_minor": 0
}
}
},
"nbformat": 4,
"nbformat_minor": 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment