Permalink
Browse files

add mfs

  • Loading branch information...
cnlohr committed Feb 8, 2013
1 parent b2d9716 commit ce01867ea84c00692ee75180f7f1ec182149c958
Showing with 288 additions and 0 deletions.
  1. +13 −0 mfs/Makefile
  2. +63 −0 mfs/mfs.c
  3. +29 −0 mfs/mfs.h
  4. +137 −0 mfs/mfsmaker.c
  5. +30 −0 mfs/test/index.html
  6. +16 −0 mfs/test/theme.css
View
@@ -0,0 +1,13 @@
+all : mfsmaker test_fs.c
+
+mfsmaker : mfsmaker.o
+ gcc -o $@ $^
+
+test_fs.c : test mfsmaker
+ ./mfsmaker $< $@
+
+clean :
+ rm -rf *.o *~ test_fs.c mfsmaker
+
+
+
View
@@ -0,0 +1,63 @@
+#include "mfs.h"
+#include <avr/pgmspace.h>
+
+extern const unsigned char PROGMEM mfsfat[];
+extern const unsigned char PROGMEM mfsdata[];
+//const unsigned char * readptr;
+
+uint16_t * offset;
+
+int8_t MFSOpenFile( const char * fname, struct MFSFileInfo * mfi )
+{
+ const char * fatptr = mfsfat;
+ uint8_t match;
+ uint8_t i;
+ char c;
+
+ while( 1 )
+ {
+ i = 0;
+ match = 1;
+ while( c = pgm_read_byte( fatptr++ ) )
+ {
+ if( c != fname[i++] )
+ {
+ match = 0;
+ }
+ }
+ if( fname[i] )
+ {
+ match = 0;
+ }
+
+ //No filename (end of FAT)
+ if( i == 0 )
+ {
+ return -1;
+ }
+
+ fatptr += 2; //Skip reserved word.
+ mfi->offset = pgm_read_byte( fatptr++ ) << 8;
+ mfi->offset |= pgm_read_byte( fatptr++ );
+ mfi->filelen = pgm_read_byte( fatptr++ ) << 8;
+ mfi->filelen |= pgm_read_byte( fatptr++ );
+
+ if( match )
+ {
+ return 0;
+ }
+ }
+}
+
+void MFSStartReadFile( struct MFSFileInfo * f )
+{
+// readptr = &mfsdata[f->offset];
+ offset = &f->offset;
+}
+
+uint8_t MFSRead8()
+{
+ return pgm_read_word( &mfsdata[(*offset)++]);
+}
+
+
View
@@ -0,0 +1,29 @@
+#ifndef _MFS_H
+#define _MFS_H
+
+#include <stdint.h>
+
+//Multi-purpose PROGMEM-based FS (for AVRs)
+
+struct MFSFileInfo
+{
+ uint16_t filelen;
+ uint16_t offset;
+};
+
+
+
+//Returns 0 on succses.
+//Returns size of file if non-empty
+//If positive, populates mfi.
+//Returns -1 if can't find file or reached end of file list.
+int8_t MFSOpenFile( const char * fname, struct MFSFileInfo * mfi );
+void MFSStartReadFile( struct MFSFileInfo * f );
+uint8_t MFSRead8();
+void MFSDoneRead();
+
+
+
+#endif
+
+
View
@@ -0,0 +1,137 @@
+#include <stdio.h>
+#include <dirent.h>
+#include <sys/stat.h>
+#include <string.h>
+
+//Format:
+//
+// mfsfat[] = { 'f', 'i', 'l', 'e', '1', '.', 't', 'x', 't', 0, [reserved: 0],
+// [reserved: 0], [start high], [start low], [length high], [length low], 'f', 'i', 'l', ...
+//
+// mfsdata[] = {...}
+//
+//
+//Restrictions: FAT cannot exceed 65536 bytes.
+// FS Data Size cannot exceed 65536 bytes.
+
+unsigned char mfsfat[65536];
+unsigned char mfsdata[65536];
+
+unsigned short fatpointer = 0;
+unsigned short datapointer = 0;
+
+int main( int argc, char ** argv )
+{
+ int i;
+ DIR *d;
+ struct dirent *dir;
+
+ if( argc != 3 )
+ {
+ fprintf( stderr, "Error: [tool] [directory to pack] [output packed .c file]\n" );
+ return -1;
+ }
+
+ d = opendir( argv[1] );
+
+ if (!d)
+ {
+ fprintf( stderr, "Error: cannot open folder for packing.\n" );
+ return -2;
+ }
+
+ while ((dir = readdir(d)) != NULL)
+ {
+ if( dir->d_type & DT_REG )
+ {
+ char thisfile[1024];
+ struct stat buf;
+ int dlen = strlen( dir->d_name );
+ int sprret = snprintf( thisfile, 1023, "%s/%s", argv[1], dir->d_name );
+
+ if( sprret > 1023 || sprret < 1 )
+ {
+ fprintf( stderr, "Error processing \"%s\" (snprintf)\n", dir->d_name );
+ continue;
+ }
+
+ int statret = stat( thisfile, &buf );
+
+ if( statret )
+ {
+ fprintf( stderr, "Error processing \"%s\" (stat)\n", dir->d_name );
+ continue;
+ }
+
+
+ memcpy( &mfsfat[fatpointer], dir->d_name, dlen );
+ fatpointer+= dlen;
+ mfsfat[fatpointer++] = 0;
+
+ mfsfat[fatpointer++] = 0; //Reserved
+ mfsfat[fatpointer++] = 0;
+
+ mfsfat[fatpointer++] = datapointer >> 8;
+ mfsfat[fatpointer++] = datapointer & 0xFF;
+
+ if( buf.st_size + datapointer > 65535 )
+ {
+ fprintf( stderr, "Error: no space left.\n" );
+ return -1;
+ }
+
+ mfsfat[fatpointer++] = (buf.st_size) >> 8;
+ mfsfat[fatpointer++] = (buf.st_size) & 0xFF;
+
+ if( buf.st_size )
+ {
+ FILE * f = fopen( thisfile, "rb" );
+ if( !f )
+ {
+ fprintf( stderr, "Error: cannot open \"%s\" for reading.\n", dir->d_name );
+ return -9;
+ }
+ fread( &mfsdata[datapointer], 1, buf.st_size, f );
+ fclose( f );
+ datapointer += buf.st_size;
+ }
+ }
+ }
+
+ closedir(d);
+
+ mfsfat[fatpointer++] = 0;
+
+ FILE * f = fopen( argv[2], "w" );
+
+ if( !f || ferror( f ) )
+ {
+ fprintf( stderr, "Error: cannot open \"%s\" for writing.\n", argv[2] );
+ }
+
+ fprintf( f, "#include <avr/pgmspace.h>\n\n" );
+ fprintf( f, "const unsigned char PROGMEM mfsfat[] = {\n\t" );
+ for( i = 0; i < fatpointer; i++ )
+ {
+ if( i && (!(i%16)) )
+ {
+ fprintf( f, "\n\t" );
+ }
+ fprintf( f, "0x%02x, ", mfsfat[i] );
+ }
+ fprintf( f, "\n};\n\nconst unsigned char PROGMEM mfsdata[] = {\n\t" );
+ for( i = 0; i < datapointer; i++ )
+ {
+ if( i && (!(i%16)) )
+ {
+ fprintf( f, "\n\t" );
+ }
+ fprintf( f, "0x%02x, ", mfsdata[i] );
+ }
+ fprintf( f, "\n};\n\n" );
+ fclose( f );
+
+ return 0;
+}
+
+
View
@@ -0,0 +1,30 @@
+
+<HTML>
+<HEAD>
+<LINK href="theme.css" rel="stylesheet" type="text/css">
+</HEAD>
+<BODY ondragover="console.log( event );" ondrop="console.log( event );">
+This is a test page for my AVR.
+<BR>
+
+<DIV class=dwindow draggable=true ondragstart="console.log( event ); event.dataTransfer.setData('Text', this.id);">
+<TABLE>
+<TR>
+<TH>Title</TH>
+</TR>
+<TR>
+<TD>Body</TD>
+</TR>
+</TABLE>
+</DIV>
+
+
+<img id="drag1" src="img_logo.gif" draggable="true"
+ondragstart="console.log(event)" width="336" height="69">
+
+<div id="div1" ondrop="drop(event)"
+ondragover="console.log(event); return true">Hello</div>
+
+
+</BODY>
+</HTML>
View
@@ -0,0 +1,16 @@
+/* http://colorschemedesigner.com/#0V21Qw0w0w0w0 */
+
+div.dwindow { position:absolute; top:0; right:0; width:200px;
+}
+
+[draggable=true] {
+ -khtml-user-drag: element;
+ -webkit-user-drag: element;
+}
+
+table { box-shadow: 1px 1px 1px #000; background-color: #05296E }
+th { background-color: #6B8FD4; color:#000000 }
+td { background-color: #ffA700; color:#000000 }
+body { background-color: #1144AA; color:#ffffff }
+
+

0 comments on commit ce01867

Please sign in to comment.