Welcome to the Pycom forum Forum
    • \[\[global:header.categories\]\]
    • \[\[global:header.recent\]\]
    • \[\[global:header.tags\]\]
    • \[\[global:header.popular\]\]
    • Login

    Explore Pybytes | Official Documentation | Report a Firmware Bug/Issue | GitHub

    write word with I2C

    Scheduled Pinned Locked Moved MicroPython
    i2cbit shift
    5 Posts 2 Posters 838 Views 2 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • C Offline
      cmisztur
      last edited by cmisztur

      Is there a more elegant way to do this:

      def writeByte(self,addess,data):
              self.i2c.writeto_mem(self.address, address, data)
      
      def writeWord(self,address,data):
              msb = ((data & 0xFF00) >> 8)
      	lsb = (data & 0x00FF)
              self.writeByte(address,msb)
              self.writeByte(address+1,lsb)
      
      robert-hhR 1 Reply Last reply Reply Quote 0
      • robert-hhR Offline
        robert-hh @cmisztur
        last edited by

        @cmisztur 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 & time, even if it makes the intention less clear.

        C 2 Replies Last reply Reply Quote 1
        • C Offline
          cmisztur @robert-hh
          last edited by

          @robert-hh so this should work?

          self.i2c.writeto_mem(self.address, address, [ ((data & 0xFF00) >> 8), (data & 0x00FF)], addrsize=8 )
          
          robert-hhR 1 Reply Last reply Reply Quote 0
          • robert-hhR Offline
            robert-hh @cmisztur
            last edited by robert-hh

            @cmisztur If you just want a single call, then you could also use struct:

            self.i2c.writeto_mem(self.address, address, struct.pack(">H", data), addrsize=8 )
            

            But no, I just was referring to a writeWord method:

            def writeWord(self,address,data):
                self.writeByte(address,  (data  >> 8) & 0xFF)
                self.writeByte(address + 1, data & 0xFF)
            

            That avoids the creating of two symbols & objects.

            1 Reply Last reply Reply Quote 1
            • C Offline
              cmisztur @robert-hh
              last edited by

              @robert-hh Thanks for the tips. I ended up doing below, which does save 32bytes over struct.pack.

              self.tempBuff = bytearray(3)
              
              def _writeWord(self,address,data):
                      #self.i2c.writeto_mem(self.address, address, struct.pack(">H", data), addrsize=8 )
                      self.tempBuff[0] = address
                      self.tempBuff[1] = data >> 8
                      self.tempBuff[2] = data & 0xff
                      self.i2c.writeto(self.address, self.tempBuff)
              
              1 Reply Last reply Reply Quote 0

              Hello! It looks like you're interested in this conversation, but you don't have an account yet.

              Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

              With your input, this post could be even better 💗

              Register Login
              • 1 / 1
              • First post
                Last post

              Pycom on Twitter

              Pycom