Skip to content

Commit

Permalink
Add FormatSsd130x data type support
Browse files Browse the repository at this point in the history
  • Loading branch information
ohmtech-rdi committed Sep 20, 2023
1 parent 18aed4f commit 5d58d06
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 1 deletion.
2 changes: 2 additions & 0 deletions build-system/erbb/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,8 @@ def source_context (self):
def source_context_part (self, part):
if part == 'name':
return adapter.SourceContext.from_token (self.name_identifier)
elif part == 'type':
return adapter.SourceContext.from_token (self.type_identifier)

return super (Data, self).source_context_part (part) # pragma: no cover

Expand Down
73 changes: 72 additions & 1 deletion build-system/erbb/generators/data/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,12 @@ def generate_declaration_data (self, module, data):
elif data.type_ == 'AudioSample':
return self.generate_declaration_data_audio_sample (module, data)

elif data.type_ == 'FormatSsd130x':
return self.generate_declaration_data_format_ssd130x (data)

else:
err = error.Error ()
context = data.data_type.source_context
context = data.source_context_part ('type')
err.add_error ("Undefined type '%s'" % context, context)
err.add_context (context)
raise err
Expand Down Expand Up @@ -155,6 +158,7 @@ def generate_declaration_data_audio_sample (self, module, data):

return content


#--------------------------------------------------------------------------

def generate_declaration_data_audio_sample_mono (self, file, data):
Expand All @@ -173,6 +177,38 @@ def generate_declaration_data_audio_sample_planar (self, file, data):
return ' static const erb::AudioSamplePlanar <float, %d, %d> %s;\n' % (file.frames, file.channels, data.name)


#--------------------------------------------------------------------------

def generate_declaration_data_format_ssd130x (self, data):

import imageio.v3 as iio
try:
im = iio.imread (data.file.path)

except OSError:
err = error.Error ()
context = data.file.source_context
err.add_error ("File '%s' not found" % context, context)
err.add_context (context)
raise err

height = im.shape [0]
if (height % 8) != 0:
err = error.Error ()
context = data.file.source_context
err.add_error ("File '%s' height is not a multiple of 8" % context, context)
err.add_context (context)
raise err

width = im.shape [1]
size = width * height // 8

content = ' static const std::array <uint8_t, %d> %s;\n' % (size, data.name);

return content



#--------------------------------------------------------------------------

def generate_module_definition (self, path, module):
Expand Down Expand Up @@ -218,6 +254,9 @@ def generate_definition_data (self, module, data):
elif data.type_ == 'AudioSample':
return self.generate_definition_data_audio_sample (module, data)

elif data.type_ == 'FormatSsd130x':
return self.generate_definition_data_format_ssd130x (module, data)

else:
err = error.Error ()
context = data.data_type.source_context
Expand Down Expand Up @@ -341,3 +380,35 @@ def generate_definition_data_audio_sample_mono (self, module, data, file):
content += '};\n'

return content


#--------------------------------------------------------------------------

def generate_definition_data_format_ssd130x (self, module, data):

import imageio.v3 as iio
try:
im = iio.imread (data.file.path)

except OSError:
err = error.Error ()
context = data.file.source_context
err.add_error ("File '%s' not found" % context, context)
err.add_context (context)
raise err

height = im.shape [0]
width = im.shape [1]
size = width * height // 8

bytes = bytearray (size)
for x in range (width):
for y in range (height):
if im [y][x]:
bytes [x + (y // 8) * width] |= 1 << (y % 8)

content = 'const std::array <uint8_t, %d> %sData::%s = {' % (size, module.name, data.name);
content += ', '.join (map (lambda byte: '0x' + format (byte, "02x"), bytes))
content += '};\n'

return content
4 changes: 4 additions & 0 deletions requirements/build-system.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ svg2mod>=1.2
SoundFile>=0.12.1
numpy>=1.21.0

# Display Format

imageio>=2.31.1

# ST-link detection

pyserial==3.5

0 comments on commit 5d58d06

Please sign in to comment.