{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 129,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import struct\n",
    "\n",
    "import serial\n",
    "import PIL\n",
    "import numpy as np\n",
    "from matplotlib import pyplot as plt\n",
    "%matplotlib inline\n",
    "\n",
    "import crc"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "digit_mask = PIL.Image.open('scans/digit.png')\n",
    "seg_masks = [ np.asarray(PIL.Image.open('segmasks/seg{}.png'.format(i)), dtype=np.float)[:,:,0]/255.0 for i in range(1,9) ]\n",
    "digit_h, digit_w = seg_masks[0].shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 193,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "pic = PIL.Image.open('circle.jpg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 194,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "dpi = 600\n",
    "scale = 1000/5091\n",
    "stride_x, stride_y = 1.0*dpi*scale, 38.8/25.4*dpi*scale"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 195,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "mask_w, mask_h = int(8*stride_x)+1, int(4*stride_y)+1\n",
    "resized = np.asarray(pic.resize((mask_w, mask_h), PIL.Image.BILINEAR).convert('HSV'), dtype=np.float)[:,:,2]/255"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 196,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def sub(resized, x, y):\n",
    "    return resized[int(y*stride_y):int(y*stride_y)+digit_h, int(x*stride_x):int(x*stride_x)+digit_w]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 197,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def digit_values(resized, x, y):\n",
    "    img = sub(resized, x, y)/255.0\n",
    "    for mask in seg_masks:\n",
    "        yield np.average(np.multiply(mask, img))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 198,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def make_vals(resized):\n",
    "    subvals = np.zeros((8, 4, 8), dtype=np.float)\n",
    "    for x in range(8):\n",
    "        for y in range(4):\n",
    "            subvals[x,y] = list(digit_values(resized, x, y))\n",
    "    return subvals"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 204,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def send_packet(ser, resized, addr=5):\n",
    "    gamma = 2\n",
    "    out = np.clip(np.power(make_vals(resized), 1/gamma)*255, 0, 255).astype(np.uint8)\n",
    "    pkt = b''\n",
    "    for x in range(8):\n",
    "        for y in range(4):\n",
    "            pkt += bytes(out[x,y,:]) + b'\\x00\\x00'\n",
    "    pkt += b'\\x03\\x00\\x00\\x00'\n",
    "    pkt += struct.pack('I', crc.crc(pkt))\n",
    "    pkt = bytes([0x40 | addr]) + pkt\n",
    "    ser.write(pkt)\n",
    "    return out"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 205,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[3, 3, 3, 3, 3, 3, 3, 1],\n",
       "        [3, 3, 3, 3, 3, 3, 3, 1],\n",
       "        [3, 3, 3, 3, 3, 3, 3, 1],\n",
       "        [3, 3, 3, 3, 3, 3, 3, 1]],\n",
       "\n",
       "       [[3, 3, 3, 3, 3, 3, 3, 1],\n",
       "        [3, 3, 3, 3, 3, 3, 3, 0],\n",
       "        [2, 3, 0, 3, 3, 3, 3, 1],\n",
       "        [3, 3, 3, 3, 3, 3, 3, 1]],\n",
       "\n",
       "       [[3, 3, 3, 3, 3, 3, 3, 1],\n",
       "        [1, 2, 0, 0, 0, 0, 0, 0],\n",
       "        [0, 0, 0, 0, 0, 0, 0, 0],\n",
       "        [3, 3, 3, 3, 3, 3, 3, 1]],\n",
       "\n",
       "       [[3, 3, 3, 3, 3, 3, 3, 1],\n",
       "        [0, 0, 0, 0, 0, 0, 0, 0],\n",
       "        [0, 0, 0, 0, 0, 0, 0, 0],\n",
       "        [0, 2, 2, 3, 3, 3, 3, 1]],\n",
       "\n",
       "       [[3, 3, 3, 3, 3, 3, 3, 1],\n",
       "        [0, 0, 0, 0, 0, 0, 0, 0],\n",
       "        [0, 0, 0, 0, 0, 0, 0, 0],\n",
       "        [0, 2, 2, 3, 3, 3, 3, 1]],\n",
       "\n",
       "       [[3, 3, 3, 3, 3, 3, 3, 1],\n",
       "        [1, 0, 2, 0, 0, 0, 0, 0],\n",
       "        [0, 0, 0, 0, 0, 0, 0, 0],\n",
       "        [3, 3, 3, 3, 3, 3, 3, 1]],\n",
       "\n",
       "       [[3, 3, 3, 3, 3, 3, 3, 1],\n",
       "        [3, 3, 3, 3, 0, 3, 1, 1],\n",
       "        [2, 0, 3, 2, 1, 3, 3, 1],\n",
       "        [3, 3, 3, 3, 3, 3, 3, 1]],\n",
       "\n",
       "       [[3, 3, 3, 3, 3, 3, 3, 1],\n",
       "        [3, 3, 3, 3, 3, 3, 3, 1],\n",
       "        [3, 3, 3, 3, 3, 3, 3, 1],\n",
       "        [3, 3, 3, 3, 3, 3, 3, 1]]], dtype=uint8)"
      ]
     },
     "execution_count": 205,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ser = serial.Serial('/dev/serial/by-id/usb-FTDI_FT232R_USB_UART_A90JRHXH-if00-port0', 2000000)\n",
    "send_packet(ser, resized)"
   ]
  }
 ],
 "metadata": {
  "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.5.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}