Advertisement
Guest User

Untitled

a guest
May 29th, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.01 KB | None | 0 0
  1. ##
  2. ## This file is part of the libsigrokdecode project.
  3. ##
  4. ## Copyright (C) 2014 Torsten Duwe <duwe@suse.de>
  5. ## Copyright (C) 2014 Sebastien Bourdelin <sebastien.bourdelin@savoirfairelinux.com>
  6. ##
  7. ## This program is free software; you can redistribute it and/or modify
  8. ## it under the terms of the GNU General Public License as published by
  9. ## the Free Software Foundation; either version 2 of the License, or
  10. ## (at your option) any later version.
  11. ##
  12. ## This program is distributed in the hope that it will be useful,
  13. ## but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ## GNU General Public License for more details.
  16. ##
  17. ## You should have received a copy of the GNU General Public License
  18. ## along with this program; if not, write to the Free Software
  19. ## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  20. ##
  21.  
  22. import sigrokdecode as srd
  23. from collections import deque
  24. class SamplerateError(Exception):
  25. pass
  26.  
  27. def normalize_time(t):
  28. if t >= 1.0:
  29. return '%.3f s (%.3f Hz)' % (t, (1/t))
  30. elif t >= 0.001:
  31. if 1/t/1000 < 1:
  32. return '%.3f ms (%.3f Hz)' % (t * 1000.0, (1/t))
  33. else:
  34. return '%.3f ms (%.3f kHz)' % (t * 1000.0, (1/t)/1000)
  35. elif t >= 0.000001:
  36. if 1/t/1000/1000 < 1:
  37. return '%.3f μs (%.3f kHz)' % (t * 1000.0 * 1000.0, (1/t)/1000)
  38. else:
  39. return '%.3f μs (%.3f MHz)' % (t * 1000.0 * 1000.0, (1/t)/1000/1000)
  40. elif t >= 0.000000001:
  41. if 1/t/1000/1000/1000:
  42. return '%.3f ns (%.3f MHz)' % (t * 1000.0 * 1000.0 * 1000.0, (1/t)/1000/1000)
  43. else:
  44. return '%.3f ns (%.3f GHz)' % (t * 1000.0 * 1000.0 * 1000.0, (1/t)/1000/1000/1000)
  45. else:
  46. return '%f' % t
  47.  
  48. class Decoder(srd.Decoder):
  49. api_version = 2
  50. id = 'timing'
  51. name = 'Timing'
  52. longname = 'Timing calculation with Frequency and Averaging'
  53. desc = 'Calculate time between edges.'
  54. license = 'gplv2+'
  55. inputs = ['logic']
  56. outputs = ['timing']
  57. channels = (
  58. {'id': 'data', 'name': 'Data', 'desc': 'Data line'},
  59. )
  60. annotations = (
  61. ('time', 'Time'),
  62. ('average', 'Average'),
  63. )
  64. annotation_rows = (
  65. ('time', 'Time', (0,)),
  66. ('average', 'Average', (1,)),
  67. )
  68. options = (
  69. { 'id': 'avg-period', 'desc': 'Averaging Period', 'default': 100 },
  70. )
  71. def __init__(self, **kwargs):
  72. self.samplerate = None
  73. self.oldpin = None
  74. self.last_samplenum = None
  75. self.last_n = deque()
  76. self.chunks = 0
  77.  
  78. def metadata(self, key, value):
  79. if key == srd.SRD_CONF_SAMPLERATE:
  80. self.samplerate = value
  81.  
  82. def start(self):
  83. self.out_ann = self.register(srd.OUTPUT_ANN)
  84.  
  85. def decode(self, ss, es, data):
  86. if not self.samplerate:
  87. raise SamplerateError('Cannot decode without samplerate.')
  88.  
  89. for (samplenum, (pin,)) in data:
  90.  
  91. if self.oldpin is None:
  92. self.oldpin = pin
  93. self.last_samplenum = samplenum
  94. continue
  95.  
  96. # It seems that the decoder gets passed the same samples
  97. # over and over again sometimes, so we have to make sure
  98. # we are only looking at samples we have not already seen
  99. # otherwise a big mess happens.
  100. if self.last_samplenum >= samplenum:
  101. continue
  102.  
  103. if self.oldpin != pin:
  104. # print('%d %s' % (samplenum, pin))
  105. samples = samplenum - self.last_samplenum
  106. t = samples / self.samplerate
  107. self.chunks += 1
  108.  
  109. # Don't insert the first chunk into the averaging as it is
  110. # not complete probably.
  111. if self.last_samplenum is None or self.chunks < 2:
  112. # Report the timing normalized.
  113. self.put(self.last_samplenum, samplenum, self.out_ann,
  114. [0, [normalize_time(t)]])
  115. else:
  116. if t > 0:
  117. self.last_n.append(t)
  118. # print (self.last_n)
  119.  
  120. if len(self.last_n) > self.options['avg-period']:
  121. self.last_n.popleft()
  122.  
  123. #if len(self.last_n) < 2:
  124. # self.put(self.last_samplenum, samplenum, self.out_ann,
  125. # [0, [normalize_time(t) ]])
  126. #else:
  127. # Report the timing normalized.
  128. self.put(self.last_samplenum, samplenum, self.out_ann,
  129. [0, [normalize_time(t)]])
  130.  
  131. self.put(self.last_samplenum, samplenum, self.out_ann,
  132. [1, [normalize_time(sum(self.last_n)/len(self.last_n))]])
  133.  
  134. # Store data for next round.
  135.  
  136. self.last_samplenum = samplenum
  137. self.oldpin = pin
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement