SSブログ

「オープンソースハードウェアセミナーVol1」レポート Arduino WEBサーバー 静的コンテンツ用バイナリー to Cソース変換プログラムのソース [ATmarquino Arduino]

ether_shield_018.png下手なプログラムで申し訳ない。
でも、これ見ただけでは何の事か判らないですね、、、。


例えばこんな写真を
chobi_01.jpg


通すと一番下のコードを出力します。ちなみに写真のデータサイズは12Kbyte程度でした。

作成したプログラムはコマンドラインで動かします。GUIのプログラムは苦手で。

※もうこんな時間なので、この静的コンテンツを使ったスケッチの方は次回公開します。
/* ------------------------------------------------------------------------ */
/* バイナリファイルをCソースファイルに変換するプログラム					*/
/*                                                                          */
/*                                  Copyright (C) 2004- by hamayan			*/
/*                                  hamayan.contact あと 爺mail.com         */
/* ------------------------------------------------------------------------ */
#include <stdio.h>
#include <string.h>
#include <time.h>

/* ------------------------------------------------------------------------ */
/* メインね!																*/
/* ------------------------------------------------------------------------ */
int main( int argc, char *argv[] )
{
	char	str[ 32 ],header_file[ 32 ];
	int		i,c;
	long	length;
	FILE	*in,*out;
	time_t	timer;
	struct	tm	*tblock;

	if( argc != 3 )
	{
		fprintf( stderr, "Argument Missmatch.\r\n" );
		return EOF;
	}

	if( (in = fopen( argv[1], "rb" )) == NULL )
	{
		fprintf( stderr, "Image File Open Error.\r\n" );
		return EOF;
	}
	if( (out = fopen( argv[2], "wb" )) == NULL )
	{
		fprintf( stderr, "Output File Open Error.\r\n" );
		fclose( in );
		return EOF;
	}

	/*ヘッダー部の出力*/
	fprintf( out, "/* ------------------------------------------------------------------------ */\r\n" );
	fprintf( out, "/* Binary data file convert to C source file program.                       */\r\n" );
	fprintf( out, "/*                                             designed by hamayan          */\r\n" );
	fprintf( out, "/*                                             Copyright(C) hamayan         */\r\n" );
	fprintf( out, "/*                                             since 2004 -                 */\r\n" );
	fprintf( out, "/*                                        hamayan.contact あと 爺mail.com   */\r\n" );
	fprintf( out, "/* ------------------------------------------------------------------------ */\r\n" );

	/*ヘッダーファイルの取り込み*/
	fprintf( out, "#include  <avr/pgmspace.h>\r\n" );
	fprintf( out, "#include  \"mime.h\"\r\n\r\n" );

	/*変換後のデータ表現*/
	fprintf( out, "static const unsigned char PROGMEM file[] =\r\n" );
	fprintf( out, "{\r\n" );
	do
	{
		fprintf( out, "\t" );
		for( i = 0; i < 16; i++ )
		{
			if( (c = fgetc( in )) == EOF ) break;
			fprintf( out, "0x%02X,", c );
		}
		fprintf( out, "\r\n" );
	} while( c != EOF );
	fprintf( out, "};\r\n\r\n" );

	/*プロパティシートを生成する。*/
	strcpy( str, argv[1] );
	*( strchr( str, '.' ) ) = '_';
	fprintf( out, "const FILE_PROPERTIES %s =\r\n", str );
	fprintf( out, "{\r\n" );

		/*ファイル名*/
		fprintf( out, "\t\"%s\",\t/*ファイル名*/\r\n",
		  argv[1] );

		/*タイムスタンプ*/
		timer = time(NULL);
		tblock = localtime(&timer);
		fprintf( out, "\t\"%4u/%2u/%2u %2u:%2u:%2u\",\t/*タイムスタンプ*/\r\n",
		  tblock->tm_year + 1900,
		  tblock->tm_mon + 1,
		  tblock->tm_mday,
		  tblock->tm_hour,
		  tblock->tm_min,
		  tblock->tm_sec );

		/*Mime type*/
		fprintf( out, "\t%s,\t/*Mime type*/\r\n",
		 (strcmp( "htm", strchr( argv[1], '.' ) + 1) == 0) ? "AddType_TXT" : 
		 (strcmp( "HTM", strchr( argv[1], '.' ) + 1) == 0) ? "AddType_TXT" : 
		 (strcmp( "html", strchr( argv[1], '.' ) + 1) == 0) ? "AddType_TXT" : 
		 (strcmp( "HTML", strchr( argv[1], '.' ) + 1) == 0) ? "AddType_TXT" : 
		 (strcmp( "txt", strchr( argv[1], '.' ) + 1) == 0) ? "AddType_TXT" : 
		 (strcmp( "TXT", strchr( argv[1], '.' ) + 1) == 0) ? "AddType_TXT" : 
		 (strcmp( "log", strchr( argv[1], '.' ) + 1) == 0) ? "AddType_TXT" : 
		 (strcmp( "LOG", strchr( argv[1], '.' ) + 1) == 0) ? "AddType_TXT" : 
		 (strcmp( "jpg", strchr( argv[1], '.' ) + 1) == 0) ? "AddType_JPEG" : 
		 (strcmp( "JPG", strchr( argv[1], '.' ) + 1) == 0) ? "AddType_JPEG" : 
		 (strcmp( "jpeg", strchr( argv[1], '.' ) + 1) == 0) ? "AddType_JPEG" : 
		 (strcmp( "JPEG", strchr( argv[1], '.' ) + 1) == 0) ? "AddType_JPEG" : 
		 (strcmp( "gif", strchr( argv[1], '.' ) + 1) == 0) ? "AddType_GIF" : 
		 (strcmp( "GIF", strchr( argv[1], '.' ) + 1) == 0) ? "AddType_GIF" : 
		 (strcmp( "png", strchr( argv[1], '.' ) + 1) == 0) ? "AddType_PNG" : 
		 (strcmp( "PNG", strchr( argv[1], '.' ) + 1) == 0) ? "AddType_PNG" : 
		 (strcmp( "bmp", strchr( argv[1], '.' ) + 1) == 0) ? "AddType_BMP" : 
		 (strcmp( "BMP", strchr( argv[1], '.' ) + 1) == 0) ? "AddType_BMP" : ""
		);

		/*ファイルの開始アドレス*/
		fprintf( out, "\tfile,\t/*ファイルの開始アドレス*/\r\n" );

		/*著者*/
		fprintf( out, "\t\"\",\t/*著者*/\r\n" );

		/*認証用*/
		fprintf( out, "\t(void *)0,\t/*認証用*/\r\n" );

		/*ファイルののサイズ*/
		fseek( in, 0L, SEEK_END );
		length = ftell( in );
		rewind( in );
		fprintf( out, "\t%ldU\t/*ファイルのサイズ*/\r\n", length );
	fprintf( out, "};\r\n\r\n" );

	*(strchr( argv[1], '.' )) = '\0';

	/*フッター部の出力*/
	fprintf( out, "/* ------------------------------------------------------------------------ */\r\n" );
	fprintf( out, "/*                                             designed by hamayan          */\r\n" );
	fprintf( out, "/*                                             Copyright(C) hamayan         */\r\n" );
	fprintf( out, "/*                                             since 2004 -                 */\r\n" );
	fprintf( out, "/*                                        hamayan.contact あと 爺mail.com   */\r\n" );
	fprintf( out, "/* ------------------------------------------------------------------------ */\r\n" );

	fclose( in );
	fclose( out );

	/*ヘッダーファイルの出力*/
	strcpy( header_file, argv[2] );
	strcpy( strchr( header_file, '.' ), ".h" );


	if( (out = fopen( header_file, "wb" )) == NULL )
	{
		fprintf( stderr, "Output File Open Error.\r\n" );
		fclose( in );
		return EOF;
	}

	/*ヘッダー部の出力*/
	fprintf( out, "/* ------------------------------------------------------------------------ */\r\n" );
	fprintf( out, "/* Binary data file convert to header file program.                         */\r\n" );
	fprintf( out, "/*                                             designed by hamayan          */\r\n" );
	fprintf( out, "/*                                             Copyright(C) hamayan         */\r\n" );
	fprintf( out, "/*                                             since 2004 -                 */\r\n" );
	fprintf( out, "/*                                        hamayan.contact あと 爺mail.com   */\r\n" );
	fprintf( out, "/* ------------------------------------------------------------------------ */\r\n" );

	/*ヘッダーファイルの取り込み*/
	fprintf( out, "#include  <avr/pgmspace.h>\r\n" );
	fprintf( out, "#include  \"mime.h\"\r\n\r\n" );

	/**/
	fprintf( out, "extern const FILE_PROPERTIES %s;\r\n\r\n", str );

	/*フッター部の出力*/
	fprintf( out, "/* ------------------------------------------------------------------------ */\r\n" );
	fprintf( out, "/*                                             designed by hamayan          */\r\n" );
	fprintf( out, "/*                                             Copyright(C) hamayan         */\r\n" );
	fprintf( out, "/*                                             since 2004 -                 */\r\n" );
	fprintf( out, "/*                                        hamayan.contact あと 爺mail.com   */\r\n" );
	fprintf( out, "/* ------------------------------------------------------------------------ */\r\n" );

	fclose( out );
	return 0;
}

/* ------------------------------------------------------------------------ */
/*                                  Copyright (C) 2004- by hamayan			*/
/*                                  hamayan.contact あと 爺mail.com         */
/* ------------------------------------------------------------------------ */


ちょびちゃんのCソース化
/* ------------------------------------------------------------------------ */
/* Binary data file convert to C source file program.                       */
/*                                             designed by hamayan          */
/*                                             Copyright(C) hamayan         */
/*                                             since 2004 -                 */
/*                                        hamayan.contact あと 爺mail.com   */
/* ------------------------------------------------------------------------ */
#include  <avr/pgmspace.h>
#include  "mime.h"

static const unsigned char PROGMEM file[] =
{
	0xFF,0xD8,0xFF,0xE0,0x00,0x10,0x4A,0x46,0x49,0x46,0x00,0x01,0x01,0x01,0x00,0x48,

  長いので途中省略

	0xE0,0xFA,0xFA,0x83,0x45,0x4D,0xE3,0xD9,0x64,0xDF,0xB7,0x79,0xC1,0x19,0xA2,0xAD,
	0x23,0x29,0x3B,0x1F,0xFF,0xD9,
};

const FILE_PROPERTIES chobi_01_jpg =
{
	"chobi_01.jpg",	/*ファイル名*/
	"2009/ 6/19  2:10:59",	/*タイムスタンプ*/
	AddType_JPEG,	/*Mime type*/
	file,	/*ファイルの開始アドレス*/
	"",	/*著者*/
	(void *)0,	/*認証用*/
	12358U	/*ファイルのサイズ*/
};

/* ------------------------------------------------------------------------ */
/*                                             designed by hamayan          */
/*                                             Copyright(C) hamayan         */
/*                                             since 2004 -                 */
/*                                        hamayan.contact あと 爺mail.com   */
/* ------------------------------------------------------------------------ */

ちょびちゃんのヘッダーファイル
/* ------------------------------------------------------------------------ */
/* Binary data file convert to header file program.                         */
/*                                             designed by hamayan          */
/*                                             Copyright(C) hamayan         */
/*                                             since 2004 -                 */
/*                                        hamayan.contact あと 爺mail.com   */
/* ------------------------------------------------------------------------ */
#include  <avr/pgmspace.h>
#include  "mime.h"

extern const FILE_PROPERTIES chobi_01_jpg;

/* ------------------------------------------------------------------------ */
/*                                             designed by hamayan          */
/*                                             Copyright(C) hamayan         */
/*                                             since 2004 -                 */
/*                                        hamayan.contact あと 爺mail.com   */
/* ------------------------------------------------------------------------ */

Arduinoモニタープログラム参加中
電子部品・半導体の通販サイト - チップワンストップ




Arduinoをはじめよう

Arduinoをはじめよう

  • 作者: Massimo Banzi
  • 出版社/メーカー: オライリージャパン
  • 発売日: 2009/03/27
  • メディア: 単行本(ソフトカバー)



Making Things Talk -Arduinoで作る「会話」するモノたち

Making Things Talk -Arduinoで作る「会話」するモノたち

  • 作者: Tom Igoe
  • 出版社/メーカー: オライリージャパン
  • 発売日: 2008/11/17
  • メディア: 大型本



nice!(0)  コメント(0)  トラックバック(0) 

nice! 0

コメント 0

コメントを書く

お名前:[必須]
URL:
コメント:
画像認証:
下の画像に表示されている文字を入力してください。

※ブログオーナーが承認したコメントのみ表示されます。

トラックバック 0

この広告は前回の更新から一定期間経過したブログに表示されています。更新すると自動で解除されます。