Skip to content

Instantly share code, notes, and snippets.

@rjw57
Last active May 10, 2017 11:33
Show Gist options
  • Save rjw57/de45d809f6ded769918f0c9cf6acdb05 to your computer and use it in GitHub Desktop.
Save rjw57/de45d809f6ded769918f0c9cf6acdb05 to your computer and use it in GitHub Desktop.
Julia Mandlebrot SenseHat example
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"using Colors\n",
"using SenseHat"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"6"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"LED = led_matrix()\n",
"6"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"WARNING: Method definition julia(Any, Any) in module Main at In[4]:3 overwritten at In[14]:3.\n"
]
},
{
"data": {
"text/plain": [
"julia (generic function with 1 method)"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"function julia(z, c)\n",
" #z = complex(c)\n",
" maxit = 50\n",
" cols = [RGB(HSV(360*h, 1, sqrt(h))) for h in linspace(0, 1, maxit)]\n",
" \n",
" finalit = maxit\n",
" for it in 1:maxit\n",
" z *= z\n",
" z += c\n",
" \n",
" if abs(z) > 2 && finalit == maxit\n",
" finalit = it\n",
"# return cols[it]\n",
" end\n",
" end\n",
" \n",
" cols[finalit]\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": 59,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"WARNING: Method definition mandlebrot(Base.Complex) in module Main at In[49]:2 overwritten at In[59]:2.\n"
]
},
{
"data": {
"text/plain": [
"mandlebrot (generic function with 2 methods)"
]
},
"execution_count": 59,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"function mandlebrot(c::Complex)\n",
" z = complex(c)\n",
" maxit = 20\n",
" cols = [RGB(HSV(360*h, 1, sqrt(h))) for h in linspace(0, 1, maxit)]\n",
" \n",
" for it in 1:maxit\n",
" z *= z\n",
" z += c\n",
" \n",
" if abs(z) > 2\n",
" return cols[it]\n",
" end\n",
" end\n",
" \n",
" return cols[maxit]\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": 60,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"WARNING: Method definition mrender(Any, Any, Any, Any) in module Main at In[50]:2 overwritten at In[60]:2.\n"
]
},
{
"data": {
"text/plain": [
"mrender (generic function with 1 method)"
]
},
"execution_count": 60,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"function mrender(minx, maxx, miny, maxy)\n",
" LED[:] = [mandlebrot(x + im*y) for x in linspace(minx, maxx, 8), y in linspace(miny, maxy, 8)]\n",
" #LED[:] = [julia(x + im*y, c) for x in linspace(minx, maxx, 8), y in linspace(miny, maxy, 8)]\n",
" true\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": 61,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"WARNING: Method definition render(Any, Any, Any, Any, Any) in module Main at In[51]:3 overwritten at In[61]:3.\n"
]
},
{
"data": {
"text/plain": [
"render (generic function with 2 methods)"
]
},
"execution_count": 61,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"function render(minx, maxx, miny, maxy, c)\n",
" #LED[:] = [mandlebrot(x + im*y) for x in linspace(minx, maxx, 8), y in linspace(miny, maxy, 8)]\n",
" LED[:] = [julia(x + im*y, c) for x in linspace(minx, maxx, 8), y in linspace(miny, maxy, 8)]\n",
" true\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": 62,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"WARNING: Method definition f(Int32) in module Main at In[54]:1 overwritten at In[62]:1.\n",
"WARNING: Method definition f(Any) in module Main at In[54]:2 overwritten at In[62]:2.\n"
]
},
{
"data": {
"text/plain": [
"f (generic function with 2 methods)"
]
},
"execution_count": 62,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"f(x::Int) = \"$x is int\"\n",
"f(x) = \"$x is unknown\""
]
},
{
"cell_type": "code",
"execution_count": 63,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\"5.0 is unknown\""
]
},
"execution_count": 63,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"f(5.)"
]
},
{
"cell_type": "code",
"execution_count": 64,
"metadata": {},
"outputs": [],
"source": [
"w = 1.\n",
"r = w\n",
"\n",
"zoomx = 0.001643721971153\n",
"zoomy = 0.822467633298876\n",
"\n",
"n = 5\n",
"for a in linspace(0, n*2*3.141, 200*n)\n",
" #r = sin(0.5 + a/2)\n",
" cx = sin(a) * r - 0.5\n",
" cy = cos(a/3) * r\n",
" #w *= 0.95\n",
" mrender(-0.5*w+cx, 0.5*w+cx, -0.5*w+cy, 0.5*w+cy)\n",
" #sleep(0.1)\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": 72,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"add3 (generic function with 1 method)"
]
},
"execution_count": 72,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"function add3(x::Int)\n",
" x + 3\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": 77,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\t.text\n",
"Filename: In[72]\n",
"Source line: 2\n",
"\tadd\tr0, r0, #3\n",
"\tbx\tlr\n"
]
}
],
"source": [
"@code_native add3(5)"
]
},
{
"cell_type": "code",
"execution_count": 79,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n",
"<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
"<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\"\n",
" width=\"120.0mm\" height=\"120.0mm\"\n",
" shape-rendering=\"crispEdges\">\n",
"<rect x=\"0.0mm\" y=\"0.0mm\"\n",
" width=\"14.0mm\" height=\"14.0mm\"\n",
" fill=\"#000000\" stroke=\"none\" />\n",
"<rect x=\"15.0mm\" y=\"0.0mm\"\n",
" width=\"14.0mm\" height=\"14.0mm\"\n",
" fill=\"#000000\" stroke=\"none\" />\n",
"<rect x=\"30.0mm\" y=\"0.0mm\"\n",
" width=\"14.0mm\" height=\"14.0mm\"\n",
" fill=\"#000000\" stroke=\"none\" />\n",
"<rect x=\"45.0mm\" y=\"0.0mm\"\n",
" width=\"14.0mm\" height=\"14.0mm\"\n",
" fill=\"#000000\" stroke=\"none\" />\n",
"<rect x=\"60.0mm\" y=\"0.0mm\"\n",
" width=\"14.0mm\" height=\"14.0mm\"\n",
" fill=\"#000000\" stroke=\"none\" />\n",
"<rect x=\"75.0mm\" y=\"0.0mm\"\n",
" width=\"14.0mm\" height=\"14.0mm\"\n",
" fill=\"#CE3D31\" stroke=\"none\" />\n",
"<rect x=\"90.0mm\" y=\"0.0mm\"\n",
" width=\"14.0mm\" height=\"14.0mm\"\n",
" fill=\"#CE3D31\" stroke=\"none\" />\n",
"<rect x=\"105.0mm\" y=\"0.0mm\"\n",
" width=\"14.0mm\" height=\"14.0mm\"\n",
" fill=\"#000000\" stroke=\"none\" />\n",
"<rect x=\"0.0mm\" y=\"15.0mm\"\n",
" width=\"14.0mm\" height=\"14.0mm\"\n",
" fill=\"#000000\" stroke=\"none\" />\n",
"<rect x=\"15.0mm\" y=\"15.0mm\"\n",
" width=\"14.0mm\" height=\"14.0mm\"\n",
" fill=\"#000000\" stroke=\"none\" />\n",
"<rect x=\"30.0mm\" y=\"15.0mm\"\n",
" width=\"14.0mm\" height=\"14.0mm\"\n",
" fill=\"#000000\" stroke=\"none\" />\n",
"<rect x=\"45.0mm\" y=\"15.0mm\"\n",
" width=\"14.0mm\" height=\"14.0mm\"\n",
" fill=\"#000000\" stroke=\"none\" />\n",
"<rect x=\"60.0mm\" y=\"15.0mm\"\n",
" width=\"14.0mm\" height=\"14.0mm\"\n",
" fill=\"#CE3D31\" stroke=\"none\" />\n",
"<rect x=\"75.0mm\" y=\"15.0mm\"\n",
" width=\"14.0mm\" height=\"14.0mm\"\n",
" fill=\"#D6615A\" stroke=\"none\" />\n",
"<rect x=\"90.0mm\" y=\"15.0mm\"\n",
" width=\"14.0mm\" height=\"14.0mm\"\n",
" fill=\"#D6615A\" stroke=\"none\" />\n",
"<rect x=\"105.0mm\" y=\"15.0mm\"\n",
" width=\"14.0mm\" height=\"14.0mm\"\n",
" fill=\"#CE3D31\" stroke=\"none\" />\n",
"<rect x=\"0.0mm\" y=\"30.0mm\"\n",
" width=\"14.0mm\" height=\"14.0mm\"\n",
" fill=\"#000000\" stroke=\"none\" />\n",
"<rect x=\"15.0mm\" y=\"30.0mm\"\n",
" width=\"14.0mm\" height=\"14.0mm\"\n",
" fill=\"#000000\" stroke=\"none\" />\n",
"<rect x=\"30.0mm\" y=\"30.0mm\"\n",
" width=\"14.0mm\" height=\"14.0mm\"\n",
" fill=\"#3A9A29\" stroke=\"none\" />\n",
"<rect x=\"45.0mm\" y=\"30.0mm\"\n",
" width=\"14.0mm\" height=\"14.0mm\"\n",
" fill=\"#3A9A29\" stroke=\"none\" />\n",
"<rect x=\"60.0mm\" y=\"30.0mm\"\n",
" width=\"14.0mm\" height=\"14.0mm\"\n",
" fill=\"#CE3D31\" stroke=\"none\" />\n",
"<rect x=\"75.0mm\" y=\"30.0mm\"\n",
" width=\"14.0mm\" height=\"14.0mm\"\n",
" fill=\"#D6615A\" stroke=\"none\" />\n",
"<rect x=\"90.0mm\" y=\"30.0mm\"\n",
" width=\"14.0mm\" height=\"14.0mm\"\n",
" fill=\"#D6615A\" stroke=\"none\" />\n",
"<rect x=\"105.0mm\" y=\"30.0mm\"\n",
" width=\"14.0mm\" height=\"14.0mm\"\n",
" fill=\"#CE3D31\" stroke=\"none\" />\n",
"<rect x=\"0.0mm\" y=\"45.0mm\"\n",
" width=\"14.0mm\" height=\"14.0mm\"\n",
" fill=\"#000000\" stroke=\"none\" />\n",
"<rect x=\"15.0mm\" y=\"45.0mm\"\n",
" width=\"14.0mm\" height=\"14.0mm\"\n",
" fill=\"#3A9A29\" stroke=\"none\" />\n",
"<rect x=\"30.0mm\" y=\"45.0mm\"\n",
" width=\"14.0mm\" height=\"14.0mm\"\n",
" fill=\"#63AE52\" stroke=\"none\" />\n",
"<rect x=\"45.0mm\" y=\"45.0mm\"\n",
" width=\"14.0mm\" height=\"14.0mm\"\n",
" fill=\"#63AE52\" stroke=\"none\" />\n",
"<rect x=\"60.0mm\" y=\"45.0mm\"\n",
" width=\"14.0mm\" height=\"14.0mm\"\n",
" fill=\"#3A9A29\" stroke=\"none\" />\n",
"<rect x=\"75.0mm\" y=\"45.0mm\"\n",
" width=\"14.0mm\" height=\"14.0mm\"\n",
" fill=\"#CE3D31\" stroke=\"none\" />\n",
"<rect x=\"90.0mm\" y=\"45.0mm\"\n",
" width=\"14.0mm\" height=\"14.0mm\"\n",
" fill=\"#CE3D31\" stroke=\"none\" />\n",
"<rect x=\"105.0mm\" y=\"45.0mm\"\n",
" width=\"14.0mm\" height=\"14.0mm\"\n",
" fill=\"#000000\" stroke=\"none\" />\n",
"<rect x=\"0.0mm\" y=\"60.0mm\"\n",
" width=\"14.0mm\" height=\"14.0mm\"\n",
" fill=\"#000000\" stroke=\"none\" />\n",
"<rect x=\"15.0mm\" y=\"60.0mm\"\n",
" width=\"14.0mm\" height=\"14.0mm\"\n",
" fill=\"#3A9A29\" stroke=\"none\" />\n",
"<rect x=\"30.0mm\" y=\"60.0mm\"\n",
" width=\"14.0mm\" height=\"14.0mm\"\n",
" fill=\"#63AE52\" stroke=\"none\" />\n",
"<rect x=\"45.0mm\" y=\"60.0mm\"\n",
" width=\"14.0mm\" height=\"14.0mm\"\n",
" fill=\"#63AE52\" stroke=\"none\" />\n",
"<rect x=\"60.0mm\" y=\"60.0mm\"\n",
" width=\"14.0mm\" height=\"14.0mm\"\n",
" fill=\"#3A9A29\" stroke=\"none\" />\n",
"<rect x=\"75.0mm\" y=\"60.0mm\"\n",
" width=\"14.0mm\" height=\"14.0mm\"\n",
" fill=\"#9459B5\" stroke=\"none\" />\n",
"<rect x=\"90.0mm\" y=\"60.0mm\"\n",
" width=\"14.0mm\" height=\"14.0mm\"\n",
" fill=\"#9459B5\" stroke=\"none\" />\n",
"<rect x=\"105.0mm\" y=\"60.0mm\"\n",
" width=\"14.0mm\" height=\"14.0mm\"\n",
" fill=\"#000000\" stroke=\"none\" />\n",
"<rect x=\"0.0mm\" y=\"75.0mm\"\n",
" width=\"14.0mm\" height=\"14.0mm\"\n",
" fill=\"#000000\" stroke=\"none\" />\n",
"<rect x=\"15.0mm\" y=\"75.0mm\"\n",
" width=\"14.0mm\" height=\"14.0mm\"\n",
" fill=\"#000000\" stroke=\"none\" />\n",
"<rect x=\"30.0mm\" y=\"75.0mm\"\n",
" width=\"14.0mm\" height=\"14.0mm\"\n",
" fill=\"#3A9A29\" stroke=\"none\" />\n",
"<rect x=\"45.0mm\" y=\"75.0mm\"\n",
" width=\"14.0mm\" height=\"14.0mm\"\n",
" fill=\"#3A9A29\" stroke=\"none\" />\n",
"<rect x=\"60.0mm\" y=\"75.0mm\"\n",
" width=\"14.0mm\" height=\"14.0mm\"\n",
" fill=\"#9459B5\" stroke=\"none\" />\n",
"<rect x=\"75.0mm\" y=\"75.0mm\"\n",
" width=\"14.0mm\" height=\"14.0mm\"\n",
" fill=\"#AD79BD\" stroke=\"none\" />\n",
"<rect x=\"90.0mm\" y=\"75.0mm\"\n",
" width=\"14.0mm\" height=\"14.0mm\"\n",
" fill=\"#AD79BD\" stroke=\"none\" />\n",
"<rect x=\"105.0mm\" y=\"75.0mm\"\n",
" width=\"14.0mm\" height=\"14.0mm\"\n",
" fill=\"#9459B5\" stroke=\"none\" />\n",
"<rect x=\"0.0mm\" y=\"90.0mm\"\n",
" width=\"14.0mm\" height=\"14.0mm\"\n",
" fill=\"#000000\" stroke=\"none\" />\n",
"<rect x=\"15.0mm\" y=\"90.0mm\"\n",
" width=\"14.0mm\" height=\"14.0mm\"\n",
" fill=\"#000000\" stroke=\"none\" />\n",
"<rect x=\"30.0mm\" y=\"90.0mm\"\n",
" width=\"14.0mm\" height=\"14.0mm\"\n",
" fill=\"#000000\" stroke=\"none\" />\n",
"<rect x=\"45.0mm\" y=\"90.0mm\"\n",
" width=\"14.0mm\" height=\"14.0mm\"\n",
" fill=\"#000000\" stroke=\"none\" />\n",
"<rect x=\"60.0mm\" y=\"90.0mm\"\n",
" width=\"14.0mm\" height=\"14.0mm\"\n",
" fill=\"#9459B5\" stroke=\"none\" />\n",
"<rect x=\"75.0mm\" y=\"90.0mm\"\n",
" width=\"14.0mm\" height=\"14.0mm\"\n",
" fill=\"#AD79BD\" stroke=\"none\" />\n",
"<rect x=\"90.0mm\" y=\"90.0mm\"\n",
" width=\"14.0mm\" height=\"14.0mm\"\n",
" fill=\"#AD79BD\" stroke=\"none\" />\n",
"<rect x=\"105.0mm\" y=\"90.0mm\"\n",
" width=\"14.0mm\" height=\"14.0mm\"\n",
" fill=\"#9459B5\" stroke=\"none\" />\n",
"<rect x=\"0.0mm\" y=\"105.0mm\"\n",
" width=\"14.0mm\" height=\"14.0mm\"\n",
" fill=\"#000000\" stroke=\"none\" />\n",
"<rect x=\"15.0mm\" y=\"105.0mm\"\n",
" width=\"14.0mm\" height=\"14.0mm\"\n",
" fill=\"#000000\" stroke=\"none\" />\n",
"<rect x=\"30.0mm\" y=\"105.0mm\"\n",
" width=\"14.0mm\" height=\"14.0mm\"\n",
" fill=\"#000000\" stroke=\"none\" />\n",
"<rect x=\"45.0mm\" y=\"105.0mm\"\n",
" width=\"14.0mm\" height=\"14.0mm\"\n",
" fill=\"#000000\" stroke=\"none\" />\n",
"<rect x=\"60.0mm\" y=\"105.0mm\"\n",
" width=\"14.0mm\" height=\"14.0mm\"\n",
" fill=\"#000000\" stroke=\"none\" />\n",
"<rect x=\"75.0mm\" y=\"105.0mm\"\n",
" width=\"14.0mm\" height=\"14.0mm\"\n",
" fill=\"#9459B5\" stroke=\"none\" />\n",
"<rect x=\"90.0mm\" y=\"105.0mm\"\n",
" width=\"14.0mm\" height=\"14.0mm\"\n",
" fill=\"#9459B5\" stroke=\"none\" />\n",
"<rect x=\"105.0mm\" y=\"105.0mm\"\n",
" width=\"14.0mm\" height=\"14.0mm\"\n",
" fill=\"#000000\" stroke=\"none\" />\n",
"</svg>"
],
"text/plain": [
"8×8 Array{SenseHat.LED.RGB565,2}:\n",
" RGB565{N0f8}(0.0,0.0,0.0) … RGB565{N0f8}(0.0,0.0,0.0) \n",
" RGB565{N0f8}(0.0,0.0,0.0) RGB565{N0f8}(0.81,0.24,0.19)\n",
" RGB565{N0f8}(0.0,0.0,0.0) RGB565{N0f8}(0.81,0.24,0.19)\n",
" RGB565{N0f8}(0.0,0.0,0.0) RGB565{N0f8}(0.0,0.0,0.0) \n",
" RGB565{N0f8}(0.0,0.0,0.0) RGB565{N0f8}(0.0,0.0,0.0) \n",
" RGB565{N0f8}(0.0,0.0,0.0) … RGB565{N0f8}(0.58,0.35,0.71)\n",
" RGB565{N0f8}(0.0,0.0,0.0) RGB565{N0f8}(0.58,0.35,0.71)\n",
" RGB565{N0f8}(0.0,0.0,0.0) RGB565{N0f8}(0.0,0.0,0.0) "
]
},
"execution_count": 79,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"LED[:] = SenseHat.JULIA_LOGO"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"w = 2.\n",
"r = w\n",
"\n",
"\n",
"\n",
"n = 5\n",
"for a in linspace(0, n*2*3.141, 200*n)\n",
" r = sin(0.5 + a/2)\n",
" cx = sin(a) * r #- 0.5\n",
" cy = cos(a/3) * r\n",
" render(-0.5*w, 0.5*w, -0.5*w, 0.5*w, cx + im*cy)\n",
" #sleep(0.1)\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Julia 0.5.1",
"language": "julia",
"name": "julia-0.5"
},
"language_info": {
"file_extension": ".jl",
"mimetype": "application/julia",
"name": "julia",
"version": "0.5.1"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment