]> sigrok.org Git - libsigrokdecode.git/blobdiff - decoders/rgb_led_spi/pd.py
avr_isp: Add more parts
[libsigrokdecode.git] / decoders / rgb_led_spi / pd.py
index 44c1837b66cc66ddf389bf0f1e3e74a7fcc87d3f..899a64a6ba621892958ce93f9d3e6417e92923d6 100644 (file)
 ## GNU General Public License for more details.
 ##
 ## You should have received a copy of the GNU General Public License
-## along with this program; if not, write to the Free Software
-## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
+## along with this program; if not, see <http://www.gnu.org/licenses/>.
 ##
 
 import sigrokdecode as srd
 
+( ANN_RGB, ) = range(1)
+
 class Decoder(srd.Decoder):
-    api_version = 1
+    api_version = 3
     id = 'rgb_led_spi'
-    name = 'RGB LED (SPI mode)'
-    longname = 'RGB LED string decoder (SPI mode)'
-    desc = 'Generic RGB LED string protocol (RGB values clocked over SPI).'
-    license = 'gplv2'
+    name = 'RGB LED (SPI)'
+    longname = 'RGB LED string decoder (SPI)'
+    desc = 'RGB LED string protocol (RGB values clocked over SPI).'
+    license = 'gplv2+'
     inputs = ['spi']
-    outputs = ['rgb_led_spi']
-    probes = []
-    optional_probes = []
-    options = {}
-    annotations = [
-        ['rgb', 'RGB values'],
-    ]
+    outputs = []
+    tags = ['Display']
+    annotations = (
+        ('rgb', 'RGB value'),
+    )
+
+    def __init__(self):
+        self.reset()
 
-    def __init__(self, **kwargs):
-        self.cmd_ss, self.cmd_es = 0, 0
+    def reset(self):
+        self.ss_cmd = None
         self.mosi_bytes = []
 
     def start(self):
         self.out_ann = self.register(srd.OUTPUT_ANN)
 
-    def putx(self, data):
-        self.put(self.cmd_ss, self.cmd_es, self.out_ann, data)
+    def putg(self, ss, es, cls, text):
+        self.put(ss, es, self.out_ann, [cls, text])
 
     def decode(self, ss, es, data):
-        ptype, mosi, miso = data
+        ptype = data[0]
 
-        # Only care about data packets.
+        # Grab the payload of three DATA packets. These hold the
+        # RGB values (in this very order).
         if ptype != 'DATA':
             return
-        self.ss, self.es = ss, es
-
-        if len(self.mosi_bytes) == 0:
-            self.cmd_ss = ss
+        _, mosi, _ = data
+        if not self.mosi_bytes:
+            self.ss_cmd = ss
         self.mosi_bytes.append(mosi)
-
-        # RGB value == 3 bytes
-        if len(self.mosi_bytes) != 3:
+        if len(self.mosi_bytes) < 3:
             return
 
-        red, green, blue = self.mosi_bytes
-        rgb_value  = int(red) << 16
-        rgb_value |= int(green) << 8
-        rgb_value |= int(blue)
-
-        self.cmd_es = es
-        self.putx([0, ["#%.6x" % rgb_value]])
-        self.mosi_bytes = []
+        # Emit annotations. Invalidate accumulated details as soon as
+        # they were processed, to prepare the next iteration.
+        ss_cmd, es_cmd = self.ss_cmd, es
+        self.ss_cmd = None
+        red, green, blue = self.mosi_bytes[:3]
+        self.mosi_bytes.clear()
+        rgb_value = int(red) << 16 | int(green) << 8 | int(blue)
+        self.putg(ss_cmd, es_cmd, ANN_RGB, ['#{:06x}'.format(rgb_value)])