import ctypes, struct
#import numpy as np

class RttBuf(object):
    def __init__(self, arr):
        self.sName,  self.pBuffer,  self.SizeOfBuffer,  self.WrOff,  self.RdOff,  self.Flags = arr

class RttCB(object):
    def __init__(self, buf):
        self.acID, self.MaxNumUpBuf,  self.MaxNumDownBuf  = struct.unpack("16sii", buf.raw[:24])
        print("len:", len(buf.raw[24:(48+0*24)]))
        for i in range(0, self.MaxNumUpBuf):
            print("i", i)
            self.aUp = RttBuf(struct.unpack("6L", buf.raw[(24+24*i):(48+24*i)]))
        
        aDown_start = 48+i*24
        for i in range(0, self.MaxNumDownBuf):
            self.aDown = RttBuf(struct.unpack("6L", buf.raw[(aDown_start + 24*i): (aDown_start+24+24*i)]))
            
        
class RTT(object):
    "RTT api"
    def __init__(self, parent=None):
        print("hello")
        
    def upBuff0Empty(self, rtt_addr, len):
        buf = ctypes.create_string_buffer(len)
        self.jlink.JLINKARM_ReadMem(rtt_addr, len, buf)
        rtt_cb = RttCB(buf)
        print(rtt_cb.aUp[0].sName)
        
        upBuffEnd = 24+24*1
        downBuffStart = 24+24*MaxNumUpBuf
        downBuffEnd = downBuffStart+24
        upBuffs0 = struct.unpack("PPiiii", buf.raw[24:upBuffEnd])
        downBuffs0 = struct.unpack("PPiiii", buf.raw[downBuffStart:downBuffEnd])
        self.upBuff0 = RttBuf(upBuffs0)
        self.downBuff0 = RttBuf(downBuffs0)
        print("sName:%x"%(self.upBuff0.sName))
        print("pBuffer:%x"%(self.upBuff0.pBuffer))
        print("SizeOfBuffer:%d"%(self.upBuff0.SizeOfBuffer))
        print("WrOff:%d"%(self.upBuff0.WrOff))
        print("RdOff:%d"%(self.upBuff0.RdOff))
        print("Flags:%d"%(self.upBuff0.Flags))
        return (self.upBuff0.RdOff == self.upBuff0.WrOff)

@@ -234,16 +234,34 @@
 elf, buf
+, upport, downport
 ):%0A     
@@ -463,22 +463,30 @@
     
-print(%22i%22, i)%0A
+if (i == upport):%0A    
     
@@ -644,24 +644,59 @@
 umDownBuf):%0A
+            if(i == downport):%0A    
             
@@ -928,17 +928,16 @@
 f upBuff
-0
 Empty(se
@@ -953,16 +953,34 @@
 ddr, len
+, upport, downport
 ):%0A     
@@ -1085,16 +1085,21 @@
         
+self.
 rtt_cb =
@@ -1112,44 +1112,89 @@
 (buf
-)%0A        print(rtt_cb.aUp%5B0%5D.sName)
+, upport, downport)%0A        return self.rtt_cb.aUp.RdOff == self.rtt_cb.aUp.WrOff
 %0A   
@@ -1207,268 +1207,243 @@
     
-   
+def
  upBuff
-End = 24+24*1%0A        downBuffStart = 24+24*MaxNumUpBuf%0A        downBuffEnd = downBuffStart+24%0A        upBuffs0 = struct.unpack(%22PPiiii%22, buf.raw%5B24:upBuffEnd%5D)%0A        downBuffs0 = struct.unpack(%22PPiiii%22, buf.raw%5BdownBuffStart:downBuffEnd%5D)%0A
+Read(self, rtt_addr, upport,  downport):%0A        if self.rtt_cb.aUp.RdOff %3C self.rtt_cb.aUp.WrOff:%0A            len = self.rtt_cb.aUp.WrOff - self.rtt_cb.aUp.RdOff%0A            buf = ctypes.create_string_buffer(len)%0A   
         
 self
@@ -1442,38 +1442,94 @@
     
+ 
 self.
-upBuff0 = RttBuf(upBuffs0
+jlink.JLINKARM_ReadMem(self.rtt_cb.aUp.pBuffer+self.rtt_cb.aUp.RdOff,  len,  buf
 )%0A  
@@ -1538,201 +1538,340 @@
     
+    
 self.
-downBuff0 = RttBuf(downBuffs0)%0A        print(%22sName:%25x%22%25(self.upBuff0.sName))%0A        print(%22pBuffer:%25x%22%25(self.upBuff0.pBuffer))%0A        print(%22SizeOfBuffer:%25d%22%25(self.upBuff0.SizeOfB
+rtt_cb.aUp.RdOff += len%0A            str = ctypes.create_string_buffer(struct.pack(%22L%22, self.rtt_cb.aUp.RdOff))%0A            self.jlink.JLINKARM_WriteMem(rtt_addr+24+upport*24+16, 4, str)%0A        else:%0A            len = self.rtt_cb.aUp.SizeOfBuffer - self.rtt_cb.aUp.RdOff +1%0A            buf = ctypes.create_string_b
 uffer
-)
+(len
 )%0A  
@@ -1880,195 +1880,306 @@
     
-print(%22WrOff:%25d%22%25(self.upBuff0.WrOff))%0A        print(%22RdOff:%25d%22%25(self.upBuff0.RdOff))%0A        print(%22Flags:%25d%22%25(self.upBuff0.Flags))%0A        return (self.upBuff0.RdOff == self.upBuff0.WrOff)
+    self.jlink.JLINKARM_ReadMem(self.rtt_cb.aUp.pBuffer+self.rtt_cb.aUp.RdOff, len, buf)%0A            self.rtt_cb.aUp.RdOff = 0%0A            str = ctypes.create_string_buffer(struct.pack(%22L%22, self.rtt_cb.aUp.RdOff))%0A            self.jlink.JLINKARM_WriteMem(rtt_addr+24+upport*24+16, 4, str)%0A            
 %0A

add upBuffEmpty and upBuffRead function.