<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[write word with I2C]]></title><description><![CDATA[<p dir="auto">Is there a more elegant way to do this:</p>
<pre><code>def writeByte(self,addess,data):
        self.i2c.writeto_mem(self.address, address, data)

def writeWord(self,address,data):
        msb = ((data &amp; 0xFF00) &gt;&gt; 8)
	lsb = (data &amp; 0x00FF)
        self.writeByte(address,msb)
        self.writeByte(address+1,lsb)
</code></pre>
]]></description><link>https://forum.pycom.io/topic/3809/write-word-with-i2c</link><generator>RSS for Node</generator><lastBuildDate>Wed, 12 Aug 2026 14:43:48 GMT</lastBuildDate><atom:link href="https://forum.pycom.io/topic/3809.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 29 Sep 2018 02:55:01 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to write word with I2C on Sat, 29 Sep 2018 02:55:44 GMT]]></title><description><![CDATA[<p dir="auto">Is there a more elegant way to do this:</p>
<pre><code>def writeByte(self,addess,data):
        self.i2c.writeto_mem(self.address, address, data)

def writeWord(self,address,data):
        msb = ((data &amp; 0xFF00) &gt;&gt; 8)
	lsb = (data &amp; 0x00FF)
        self.writeByte(address,msb)
        self.writeByte(address+1,lsb)
</code></pre>
]]></description><link>https://forum.pycom.io/post/22515</link><guid isPermaLink="true">https://forum.pycom.io/post/22515</guid><dc:creator><![CDATA[cmisztur]]></dc:creator><pubDate>Sat, 29 Sep 2018 02:55:44 GMT</pubDate></item><item><title><![CDATA[Reply to write word with I2C on Sat, 29 Sep 2018 06:41:08 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="https://forum.pycom.io/uid/2906">@cmisztur</a> Instead of creating the symbols msb and lsb, you can to that calculation directly in the calls to writeByte(). That saves a little bit of memory &amp; time, even if it makes the intention less clear.</p>
]]></description><link>https://forum.pycom.io/post/22524</link><guid isPermaLink="true">https://forum.pycom.io/post/22524</guid><dc:creator><![CDATA[robert-hh]]></dc:creator><pubDate>Sat, 29 Sep 2018 06:41:08 GMT</pubDate></item><item><title><![CDATA[Reply to write word with I2C on Sat, 29 Sep 2018 22:12:59 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="https://forum.pycom.io/uid/98">@robert-hh</a> so this should work?</p>
<pre><code>self.i2c.writeto_mem(self.address, address, [ ((data &amp; 0xFF00) &gt;&gt; 8), (data &amp; 0x00FF)], addrsize=8 )
</code></pre>
]]></description><link>https://forum.pycom.io/post/22536</link><guid isPermaLink="true">https://forum.pycom.io/post/22536</guid><dc:creator><![CDATA[cmisztur]]></dc:creator><pubDate>Sat, 29 Sep 2018 22:12:59 GMT</pubDate></item><item><title><![CDATA[Reply to write word with I2C on Sun, 30 Sep 2018 06:06:29 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="https://forum.pycom.io/uid/2906">@cmisztur</a> If you just want a single call, then you could also use struct:</p>
<pre><code>self.i2c.writeto_mem(self.address, address, struct.pack(&quot;&gt;H&quot;, data), addrsize=8 )
</code></pre>
<p dir="auto">But no, I just was referring to a writeWord method:</p>
<pre><code>def writeWord(self,address,data):
    self.writeByte(address,  (data  &gt;&gt; 8) &amp; 0xFF)
    self.writeByte(address + 1, data &amp; 0xFF)
</code></pre>
<p dir="auto">That avoids the creating of two symbols &amp; objects.</p>
]]></description><link>https://forum.pycom.io/post/22542</link><guid isPermaLink="true">https://forum.pycom.io/post/22542</guid><dc:creator><![CDATA[robert-hh]]></dc:creator><pubDate>Sun, 30 Sep 2018 06:06:29 GMT</pubDate></item><item><title><![CDATA[Reply to write word with I2C on Mon, 10 Dec 2018 16:19:05 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="https://forum.pycom.io/uid/98">@robert-hh</a> Thanks for the tips.  I ended up doing below, which does save 32bytes over struct.pack.</p>
<pre><code>self.tempBuff = bytearray(3)

def _writeWord(self,address,data):
        #self.i2c.writeto_mem(self.address, address, struct.pack(&quot;&gt;H&quot;, data), addrsize=8 )
        self.tempBuff[0] = address
        self.tempBuff[1] = data &gt;&gt; 8
        self.tempBuff[2] = data &amp; 0xff
        self.i2c.writeto(self.address, self.tempBuff)
</code></pre>
]]></description><link>https://forum.pycom.io/post/24101</link><guid isPermaLink="true">https://forum.pycom.io/post/24101</guid><dc:creator><![CDATA[cmisztur]]></dc:creator><pubDate>Mon, 10 Dec 2018 16:19:05 GMT</pubDate></item></channel></rss>