|
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-
-
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
-
- <title>pyad2usb.util — pyad2usb documentation</title>
-
- <link rel="stylesheet" href="../../_static/default.css" type="text/css" />
- <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
-
- <script type="text/javascript">
- var DOCUMENTATION_OPTIONS = {
- URL_ROOT: '../../',
- VERSION: '',
- COLLAPSE_INDEX: false,
- FILE_SUFFIX: '.html',
- HAS_SOURCE: true
- };
- </script>
- <script type="text/javascript" src="../../_static/jquery.js"></script>
- <script type="text/javascript" src="../../_static/underscore.js"></script>
- <script type="text/javascript" src="../../_static/doctools.js"></script>
- <link rel="top" title="pyad2usb documentation" href="../../index.html" />
- <link rel="up" title="Module code" href="../index.html" />
- </head>
- <body>
- <div class="related">
- <h3>Navigation</h3>
- <ul>
- <li class="right" style="margin-right: 10px">
- <a href="../../genindex.html" title="General Index"
- accesskey="I">index</a></li>
- <li class="right" >
- <a href="../../py-modindex.html" title="Python Module Index"
- >modules</a> |</li>
- <li><a href="../../index.html">pyad2usb documentation</a> »</li>
- <li><a href="../index.html" accesskey="U">Module code</a> »</li>
- </ul>
- </div>
-
- <div class="document">
- <div class="documentwrapper">
- <div class="bodywrapper">
- <div class="body">
-
- <h1>Source code for pyad2usb.util</h1><div class="highlight"><pre>
- <span class="sd">"""</span>
- <span class="sd">Provides utility classes for the AD2USB devices.</span>
- <span class="sd">"""</span>
-
- <span class="kn">import</span> <span class="nn">ad2usb</span>
- <span class="kn">import</span> <span class="nn">time</span>
- <span class="kn">import</span> <span class="nn">traceback</span>
- <span class="kn">import</span> <span class="nn">threading</span>
-
- <div class="viewcode-block" id="NoDeviceError"><a class="viewcode-back" href="../../pyad2usb.html#pyad2usb.util.NoDeviceError">[docs]</a><span class="k">class</span> <span class="nc">NoDeviceError</span><span class="p">(</span><span class="ne">Exception</span><span class="p">):</span>
- <span class="sd">"""</span>
- <span class="sd"> No devices found.</span>
- <span class="sd"> """</span>
- <span class="k">pass</span>
- </div>
- <div class="viewcode-block" id="CommError"><a class="viewcode-back" href="../../pyad2usb.html#pyad2usb.util.CommError">[docs]</a><span class="k">class</span> <span class="nc">CommError</span><span class="p">(</span><span class="ne">Exception</span><span class="p">):</span>
- <span class="sd">"""</span>
- <span class="sd"> There was an error communicating with the device.</span>
- <span class="sd"> """</span>
- <span class="k">pass</span>
- </div>
- <div class="viewcode-block" id="TimeoutError"><a class="viewcode-back" href="../../pyad2usb.html#pyad2usb.util.TimeoutError">[docs]</a><span class="k">class</span> <span class="nc">TimeoutError</span><span class="p">(</span><span class="ne">Exception</span><span class="p">):</span>
- <span class="sd">"""</span>
- <span class="sd"> There was a timeout while trying to communicate with the device.</span>
- <span class="sd"> """</span>
- <span class="k">pass</span>
- </div>
- <div class="viewcode-block" id="InvalidMessageError"><a class="viewcode-back" href="../../pyad2usb.html#pyad2usb.util.InvalidMessageError">[docs]</a><span class="k">class</span> <span class="nc">InvalidMessageError</span><span class="p">(</span><span class="ne">Exception</span><span class="p">):</span>
- <span class="sd">"""</span>
- <span class="sd"> The format of the panel message was invalid.</span>
- <span class="sd"> """</span>
- <span class="k">pass</span>
- </div>
- <div class="viewcode-block" id="Firmware"><a class="viewcode-back" href="../../pyad2usb.html#pyad2usb.util.Firmware">[docs]</a><span class="k">class</span> <span class="nc">Firmware</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
- <span class="sd">"""</span>
- <span class="sd"> Represents firmware for the AD2USB/AD2SERIAL devices.</span>
- <span class="sd"> """</span>
-
- <span class="c"># Constants</span>
- <span class="n">STAGE_START</span> <span class="o">=</span> <span class="mi">0</span>
- <span class="n">STAGE_WAITING</span> <span class="o">=</span> <span class="mi">1</span>
- <span class="n">STAGE_BOOT</span> <span class="o">=</span> <span class="mi">2</span>
- <span class="n">STAGE_LOAD</span> <span class="o">=</span> <span class="mi">3</span>
- <span class="n">STAGE_UPLOADING</span> <span class="o">=</span> <span class="mi">4</span>
- <span class="n">STAGE_DONE</span> <span class="o">=</span> <span class="mi">5</span>
-
- <span class="nd">@staticmethod</span>
- <div class="viewcode-block" id="Firmware.upload"><a class="viewcode-back" href="../../pyad2usb.html#pyad2usb.util.Firmware.upload">[docs]</a> <span class="k">def</span> <span class="nf">upload</span><span class="p">(</span><span class="n">dev</span><span class="p">,</span> <span class="n">filename</span><span class="p">,</span> <span class="n">progress_callback</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
- <span class="sd">"""</span>
- <span class="sd"> Uploads firmware to an AD2USB/AD2SERIAL device.</span>
- <span class="sd"> """</span>
-
- <span class="k">def</span> <span class="nf">do_upload</span><span class="p">():</span>
- <span class="sd">"""</span>
- <span class="sd"> Perform the actual firmware upload to the device.</span>
- <span class="sd"> """</span>
- <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">filename</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
- <span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">f</span><span class="p">:</span>
- <span class="n">line</span> <span class="o">=</span> <span class="n">line</span><span class="o">.</span><span class="n">rstrip</span><span class="p">()</span>
-
- <span class="k">if</span> <span class="n">line</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="s">':'</span><span class="p">:</span>
- <span class="n">dev</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">line</span> <span class="o">+</span> <span class="s">"</span><span class="se">\r</span><span class="s">"</span><span class="p">)</span>
- <span class="n">res</span> <span class="o">=</span> <span class="n">dev</span><span class="o">.</span><span class="n">read_line</span><span class="p">(</span><span class="n">timeout</span><span class="o">=</span><span class="mf">10.0</span><span class="p">)</span>
-
- <span class="k">if</span> <span class="n">progress_callback</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
- <span class="n">progress_callback</span><span class="p">(</span><span class="n">Firmware</span><span class="o">.</span><span class="n">STAGE_UPLOADING</span><span class="p">)</span>
-
- <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mf">0.05</span><span class="p">)</span>
-
- <span class="k">def</span> <span class="nf">read_until</span><span class="p">(</span><span class="n">pattern</span><span class="p">,</span> <span class="n">timeout</span><span class="o">=</span><span class="mf">0.0</span><span class="p">):</span>
- <span class="sd">"""</span>
- <span class="sd"> Read characters until a specific pattern is found or the timeout is hit.</span>
- <span class="sd"> """</span>
- <span class="k">def</span> <span class="nf">timeout_event</span><span class="p">():</span>
- <span class="n">timeout_event</span><span class="o">.</span><span class="n">reading</span> <span class="o">=</span> <span class="bp">False</span>
-
- <span class="n">timeout_event</span><span class="o">.</span><span class="n">reading</span> <span class="o">=</span> <span class="bp">True</span>
-
- <span class="n">timer</span> <span class="o">=</span> <span class="bp">None</span>
- <span class="k">if</span> <span class="n">timeout</span> <span class="o">></span> <span class="mi">0</span><span class="p">:</span>
- <span class="n">timer</span> <span class="o">=</span> <span class="n">threading</span><span class="o">.</span><span class="n">Timer</span><span class="p">(</span><span class="n">timeout</span><span class="p">,</span> <span class="n">timeout_event</span><span class="p">)</span>
- <span class="n">timer</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
-
- <span class="n">buf</span> <span class="o">=</span> <span class="s">''</span>
- <span class="n">position</span> <span class="o">=</span> <span class="mi">0</span>
-
- <span class="k">while</span> <span class="n">timeout_event</span><span class="o">.</span><span class="n">reading</span><span class="p">:</span>
- <span class="k">try</span><span class="p">:</span>
- <span class="n">char</span> <span class="o">=</span> <span class="n">dev</span><span class="o">.</span><span class="n">read</span><span class="p">()</span>
-
- <span class="k">if</span> <span class="n">char</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span> <span class="ow">and</span> <span class="n">char</span> <span class="o">!=</span> <span class="s">''</span><span class="p">:</span>
- <span class="k">if</span> <span class="n">char</span> <span class="o">==</span> <span class="n">pattern</span><span class="p">[</span><span class="n">position</span><span class="p">]:</span>
- <span class="n">position</span> <span class="o">=</span> <span class="n">position</span> <span class="o">+</span> <span class="mi">1</span>
- <span class="k">if</span> <span class="n">position</span> <span class="o">==</span> <span class="nb">len</span><span class="p">(</span><span class="n">pattern</span><span class="p">):</span>
- <span class="k">break</span>
- <span class="k">else</span><span class="p">:</span>
- <span class="n">position</span> <span class="o">=</span> <span class="mi">0</span>
-
- <span class="k">except</span> <span class="ne">Exception</span><span class="p">,</span> <span class="n">err</span><span class="p">:</span>
- <span class="k">pass</span>
-
- <span class="k">if</span> <span class="n">timer</span><span class="p">:</span>
- <span class="k">if</span> <span class="n">timer</span><span class="o">.</span><span class="n">is_alive</span><span class="p">():</span>
- <span class="n">timer</span><span class="o">.</span><span class="n">cancel</span><span class="p">()</span>
- <span class="k">else</span><span class="p">:</span>
- <span class="k">raise</span> <span class="n">TimeoutError</span><span class="p">(</span><span class="s">'Timeout while waiting for line terminator.'</span><span class="p">)</span>
-
- <span class="k">def</span> <span class="nf">stage_callback</span><span class="p">(</span><span class="n">stage</span><span class="p">):</span>
- <span class="k">if</span> <span class="n">progress_callback</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
- <span class="n">progress_callback</span><span class="p">(</span><span class="n">stage</span><span class="p">)</span>
-
- <span class="k">if</span> <span class="n">dev</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
- <span class="k">raise</span> <span class="n">NoDeviceError</span><span class="p">(</span><span class="s">'No device specified for firmware upload.'</span><span class="p">)</span>
-
- <span class="n">stage_callback</span><span class="p">(</span><span class="n">Firmware</span><span class="o">.</span><span class="n">STAGE_START</span><span class="p">)</span>
-
- <span class="k">if</span> <span class="n">dev</span><span class="o">.</span><span class="n">is_reader_alive</span><span class="p">():</span>
- <span class="c"># Close the reader thread and wait for it to die, otherwise</span>
- <span class="c"># it interferes with our reading.</span>
- <span class="n">dev</span><span class="o">.</span><span class="n">stop_reader</span><span class="p">()</span>
- <span class="k">while</span> <span class="n">dev</span><span class="o">.</span><span class="n">_read_thread</span><span class="o">.</span><span class="n">is_alive</span><span class="p">():</span>
- <span class="n">stage_callback</span><span class="p">(</span><span class="n">Firmware</span><span class="o">.</span><span class="n">STAGE_WAITING</span><span class="p">)</span>
- <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
-
- <span class="c"># Reboot the device and wait for the boot loader.</span>
- <span class="n">stage_callback</span><span class="p">(</span><span class="n">Firmware</span><span class="o">.</span><span class="n">STAGE_BOOT</span><span class="p">)</span>
- <span class="n">dev</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">"="</span><span class="p">)</span>
- <span class="n">read_until</span><span class="p">(</span><span class="s">'!boot'</span><span class="p">,</span> <span class="n">timeout</span><span class="o">=</span><span class="mf">15.0</span><span class="p">)</span>
-
- <span class="c"># Get ourselves into the boot loader and wait for indication</span>
- <span class="c"># that it's ready for the firmware upload.</span>
- <span class="n">stage_callback</span><span class="p">(</span><span class="n">Firmware</span><span class="o">.</span><span class="n">STAGE_LOAD</span><span class="p">)</span>
- <span class="n">dev</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">"="</span><span class="p">)</span>
- <span class="n">read_until</span><span class="p">(</span><span class="s">'!load'</span><span class="p">,</span> <span class="n">timeout</span><span class="o">=</span><span class="mf">15.0</span><span class="p">)</span>
-
- <span class="c"># And finally do the upload.</span>
- <span class="n">do_upload</span><span class="p">()</span>
- <span class="n">stage_callback</span><span class="p">(</span><span class="n">Firmware</span><span class="o">.</span><span class="n">STAGE_DONE</span><span class="p">)</span></div></div>
- </pre></div>
-
- </div>
- </div>
- </div>
- <div class="sphinxsidebar">
- <div class="sphinxsidebarwrapper">
- <div id="searchbox" style="display: none">
- <h3>Quick search</h3>
- <form class="search" action="../../search.html" method="get">
- <input type="text" name="q" />
- <input type="submit" value="Go" />
- <input type="hidden" name="check_keywords" value="yes" />
- <input type="hidden" name="area" value="default" />
- </form>
- <p class="searchtip" style="font-size: 90%">
- Enter search terms or a module, class or function name.
- </p>
- </div>
- <script type="text/javascript">$('#searchbox').show(0);</script>
- </div>
- </div>
- <div class="clearer"></div>
- </div>
- <div class="related">
- <h3>Navigation</h3>
- <ul>
- <li class="right" style="margin-right: 10px">
- <a href="../../genindex.html" title="General Index"
- >index</a></li>
- <li class="right" >
- <a href="../../py-modindex.html" title="Python Module Index"
- >modules</a> |</li>
- <li><a href="../../index.html">pyad2usb documentation</a> »</li>
- <li><a href="../index.html" >Module code</a> »</li>
- </ul>
- </div>
- <div class="footer">
- © Copyright 2013, Author.
- Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.2b1.
- </div>
- </body>
- </html>
|