From: kouichi Date: Wed, 21 Aug 2013 13:53:13 +0000 (+0900) Subject: add kanjya's files X-Git-Url: http://git.osdn.net/view?p=tondenhei%2Fet2013.git;a=commitdiff_plain;h=88460d9e510daaacd7435c4ac223612d53494443 add kanjya's files --- diff --git a/ETBalanceRunner.cpp b/ETBalanceRunner.cpp new file mode 100644 index 0000000..1f73778 --- /dev/null +++ b/ETBalanceRunner.cpp @@ -0,0 +1,31 @@ +#include "ETBalanceRunner.h" +#include "Motor.h" +#include "GyroSensor.h" +#include "Nxt.h" + +extern "C" { +#include "balancer.h" +} + +namespace ecrobot{ + +ETBalanceRunner::ETBalanceRunner(Motor& motorL, Motor& motorR, GyroSensor& gyro, Nxt& nxt) + : m_motorL(motorL), m_motorR(motorR), m_gyro(gyro), m_nxt(nxt), m_gyrooffset(610) +{ +} +ETBalanceRunner::~ETBalanceRunner(void) +{ +} +void ETBalanceRunner::Run(int forward, int turn) +{ + S8 r,l; + balance_control(forward,turn,m_gyro.getAnglerVelocity(),m_gyrooffset,m_motorL.getCount(),m_motorR.getCount(),m_nxt.getBattMv(),&l,&r); + m_motorL.setPWM(l); + m_motorR.setPWM(r); +} +void ETBalanceRunner::SetGyroOffset(int offset) +{ + m_gyrooffset = offset; +} + +} diff --git a/ETBalanceRunner.h b/ETBalanceRunner.h new file mode 100644 index 0000000..bb77bfd --- /dev/null +++ b/ETBalanceRunner.h @@ -0,0 +1,25 @@ +#pragma once +#include "Runner.h" + +namespace ecrobot{ + +class Motor; +class GyroSensor; +class Nxt; + +class ETBalanceRunner : public Runner +{ + Motor& m_motorL; + Motor& m_motorR; + GyroSensor& m_gyro; + Nxt& m_nxt; + int m_gyrooffset; +public: + ETBalanceRunner(Motor& motorL, Motor& motorR, GyroSensor& gyro, Nxt& nxt); + virtual ~ETBalanceRunner(void); + // + virtual void Run(int forward, int turn); + void SetGyroOffset(int offset); +}; + +} diff --git a/ETLinePos.cpp b/ETLinePos.cpp new file mode 100644 index 0000000..6a94c5f --- /dev/null +++ b/ETLinePos.cpp @@ -0,0 +1,27 @@ +#include "ETLinePos.h" +#include "LightSensor.h" + +namespace ecrobot{ + +ETLinePos::ETLinePos(LightSensor& light) + : m_light(light), m_black(0), m_white(1000) +{ +} +ETLinePos::~ETLinePos(void) +{ +} +int ETLinePos::GetLinePos() +{ + static const int range = 100; // -range <= result <= range ‚ɐ³‹K‰»‚µ‚Ü‚· + return (m_light.getBrightness() - (m_white + m_black) / 2) * range / (m_white - m_black); +} +void ETLinePos::SetBlack(int value) +{ + m_black = value; +} +void ETLinePos::SetWhite(int value) +{ + m_white = value; +} + +} diff --git a/ETLinePos.h b/ETLinePos.h new file mode 100644 index 0000000..e37bbb9 --- /dev/null +++ b/ETLinePos.h @@ -0,0 +1,22 @@ +#pragma once +#include "LinePos.h" + +namespace ecrobot{ + +class LightSensor; + +class ETLinePos : public LinePos +{ + LightSensor& m_light; + int m_black; + int m_white; +public: + ETLinePos(LightSensor& light); + virtual ~ETLinePos(void); + // + virtual int GetLinePos(); + void SetBlack(int value); + void SetWhite(int value); +}; + +} diff --git a/ETLineTracer.cpp b/ETLineTracer.cpp new file mode 100644 index 0000000..2de3f67 --- /dev/null +++ b/ETLineTracer.cpp @@ -0,0 +1,19 @@ +#include "ETLineTracer.h" + +ETLineTracer::ETLineTracer(LinePos& linepos, Runner& runner) + : LineTracer(linepos,runner) +{ +} +ETLineTracer::~ETLineTracer(void) +{ +} +void ETLineTracer::CalcOutput(int speed, int linepos, int& forward, int& turn) +{ + // ‚Æ‚è‚ ‚¦‚¸‚ÌŽÀ‘• + forward = speed * 3 / 10; + if(linepos > 0){ + turn = 50; + }else{ + turn = -50; + } +} diff --git a/ETLineTracer.h b/ETLineTracer.h new file mode 100644 index 0000000..66eb7c9 --- /dev/null +++ b/ETLineTracer.h @@ -0,0 +1,11 @@ +#pragma once +#include "LineTracer.h" +class ETLineTracer : public LineTracer +{ +public: + ETLineTracer(LinePos& linepos, Runner& runner); + virtual ~ETLineTracer(void); + // +private: + virtual void CalcOutput(int speed, int linepos, int& forward, int& turn); +}; diff --git a/LinePos.cpp b/LinePos.cpp new file mode 100644 index 0000000..0a6871d --- /dev/null +++ b/LinePos.cpp @@ -0,0 +1,8 @@ +#include "LinePos.h" + +LinePos::LinePos(void) +{ +} +LinePos::~LinePos(void) +{ +} diff --git a/LinePos.h b/LinePos.h new file mode 100644 index 0000000..8fac1bc --- /dev/null +++ b/LinePos.h @@ -0,0 +1,9 @@ +#pragma once +class LinePos +{ +public: + LinePos(void); + virtual ~LinePos(void); + // + virtual int GetLinePos() = 0; +}; diff --git a/LineTracer.cpp b/LineTracer.cpp new file mode 100644 index 0000000..08ed0a7 --- /dev/null +++ b/LineTracer.cpp @@ -0,0 +1,18 @@ +#include "LineTracer.h" +#include "LinePos.h" +#include "Runner.h" + +LineTracer::LineTracer(LinePos& linepos, Runner& runner) + : m_linepos(linepos), m_runner(runner) +{ +} +LineTracer::~LineTracer(void) +{ +} +void LineTracer::RunTrace(int speed) +{ + int forward = 0; + int turn = 0; + CalcOutput(speed,m_linepos.GetLinePos(),forward,turn); + m_runner.Run(forward,turn); +} diff --git a/LineTracer.h b/LineTracer.h new file mode 100644 index 0000000..1d29366 --- /dev/null +++ b/LineTracer.h @@ -0,0 +1,18 @@ +#pragma once + +class LinePos; +class Runner; + +class LineTracer +{ + LinePos& m_linepos; + Runner& m_runner; +public: + LineTracer(LinePos& linepos, Runner& runner); + virtual ~LineTracer(void); + // + void RunTrace(int speed); +private: + virtual void CalcOutput(int speed, int linepos, int& forward, int& turn) = 0; +}; + diff --git a/Runner.cpp b/Runner.cpp new file mode 100644 index 0000000..8552aa7 --- /dev/null +++ b/Runner.cpp @@ -0,0 +1,8 @@ +#include "Runner.h" + +Runner::Runner(void) +{ +} +Runner::~Runner(void) +{ +} diff --git a/Runner.h b/Runner.h new file mode 100644 index 0000000..f019790 --- /dev/null +++ b/Runner.h @@ -0,0 +1,9 @@ +#pragma once +class Runner +{ +public: + Runner(void); + virtual ~Runner(void); + // + virtual void Run(int forward,int turn) = 0; +}; diff --git a/Tonden.sdf b/Tonden.sdf new file mode 100644 index 0000000..22f1fbf Binary files /dev/null and b/Tonden.sdf differ diff --git a/Tonden.sln b/Tonden.sln new file mode 100644 index 0000000..4feffea --- /dev/null +++ b/Tonden.sln @@ -0,0 +1,20 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Express 2012 for Windows Desktop +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Tonden", "Tonden.vcxproj", "{F7231682-384E-4B78-A729-5CAED4A512F1}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Win32 = Debug|Win32 + Release|Win32 = Release|Win32 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {F7231682-384E-4B78-A729-5CAED4A512F1}.Debug|Win32.ActiveCfg = Debug|Win32 + {F7231682-384E-4B78-A729-5CAED4A512F1}.Debug|Win32.Build.0 = Debug|Win32 + {F7231682-384E-4B78-A729-5CAED4A512F1}.Release|Win32.ActiveCfg = Release|Win32 + {F7231682-384E-4B78-A729-5CAED4A512F1}.Release|Win32.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/Tonden.v11.suo b/Tonden.v11.suo new file mode 100644 index 0000000..a267696 Binary files /dev/null and b/Tonden.v11.suo differ diff --git a/Tonden.vcxproj b/Tonden.vcxproj new file mode 100644 index 0000000..9ae257a --- /dev/null +++ b/Tonden.vcxproj @@ -0,0 +1,85 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + + {F7231682-384E-4B78-A729-5CAED4A512F1} + MakeFileProj + + + + Makefile + true + v110 + + + Makefile + false + v110 + + + + + + + + + + + + + make all + make clean + make all + WIN32;_DEBUG;$(NMakePreprocessorDefinitions) + C:\cygwin\nxtOSEK\lejos_nxj\src\nxtvm\platform\nxt;C:\cygwin\nxtOSEK\toppers_osek\kernel;C:\cygwin\nxtOSEK\toppers_osek\include;C:\cygwin\nxtOSEK\ecrobot\nxtway_gs_balancer;C:\cygwin\nxtOSEK\ecrobot\c;C:\cygwin\nxtOSEK\ecrobot\c++\device;$(IncludePath) + + + make all + rxeflash.sh + make clean + make all + WIN32;NDEBUG;$(NMakePreprocessorDefinitions) + C:\cygwin\nxtOSEK\lejos_nxj\src\nxtvm\platform\nxt;C:\cygwin\nxtOSEK\toppers_osek\kernel;C:\cygwin\nxtOSEK\toppers_osek\include;C:\cygwin\nxtOSEK\ecrobot\nxtway_gs_balancer;C:\cygwin\nxtOSEK\ecrobot\c;C:\cygwin\nxtOSEK\ecrobot\c++\device;$(IncludePath) + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Tonden.vcxproj.filters b/Tonden.vcxproj.filters new file mode 100644 index 0000000..4dcb4d7 --- /dev/null +++ b/Tonden.vcxproj.filters @@ -0,0 +1,76 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hpp;hxx;hm;inl;inc;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + + + + + + + + ソース ファイル + + + ソース ファイル + + + ソース ファイル + + + ソース ファイル + + + ソース ファイル + + + ソース ファイル + + + ソース ファイル + + + ソース ファイル + + + ソース ファイル + + + + + ヘッダー ファイル + + + ヘッダー ファイル + + + ヘッダー ファイル + + + ヘッダー ファイル + + + ヘッダー ファイル + + + ヘッダー ファイル + + + ヘッダー ファイル + + + \ No newline at end of file diff --git a/Tonden.vcxproj.user b/Tonden.vcxproj.user new file mode 100644 index 0000000..7cbb321 --- /dev/null +++ b/Tonden.vcxproj.user @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/balancer_param.c b/balancer_param.c new file mode 100644 index 0000000..21adc67 --- /dev/null +++ b/balancer_param.c @@ -0,0 +1,34 @@ +/** + ******************************************************************************* + ** ƒtƒ@ƒCƒ‹–¼ : balancer_param.c + ** + ** ŠT—v : “|—§UŽq§Œäƒpƒ‰ƒ[ƒ^ + ** + ** ’‹L : “|—§UŽq§Œäƒpƒ‰ƒ[ƒ^‚͐§Œä“Á«‚É‘å‚«‚ȉe‹¿‚ð—^‚¦‚Ü‚·B + ** + ******************************************************************************* + **/ + +/*============================================================================ + * ƒf[ƒ^’è‹` + *===========================================================================*/ +float A_D = 0.8F; /* ƒ[ƒpƒXƒtƒBƒ‹ƒ^ŒW”(¶‰EŽÔ—Ö‚Ì•½‹Ï‰ñ“]Šp“x—p) */ +float A_R = 0.996F; /* ƒ[ƒpƒXƒtƒBƒ‹ƒ^ŒW”(¶‰EŽÔ—Ö‚Ì–Ú•W•½‹Ï‰ñ“]Šp“x—p) */ + +/* ó‘ÔƒtƒB[ƒhƒoƒbƒNŒW” + * K_F[0]: ŽÔ—Ö‰ñ“]Šp“xŒW” + * K_F[1]: ŽÔ‘ÌŒXŽÎŠp“xŒW” + * K_F[2]: ŽÔ—Ö‰ñ“]Šp‘¬“xŒW” + * K_F[3]: ŽÔ‘ÌŒXŽÎŠp‘¬“xŒW” + */ +float K_F[4] = {-0.870303F, -31.9978F, -1.1566F, -2.78873F}; +float K_I = -0.44721F; /* ƒT[ƒ{§Œä—pÏ•ªƒtƒB[ƒhƒoƒbƒNŒW” */ + +//float K_PHIDOT = 25.0F; /* ŽÔ‘Ì–Ú•Wù‰ñŠp‘¬“xŒW” */ +float K_PHIDOT = 25.0F*2.5F; /* ŽÔ‘Ì–Ú•Wù‰ñŠp‘¬“xŒW” */ +float K_THETADOT = 7.5F; /* ƒ‚[ƒ^–Ú•W‰ñ“]Šp‘¬“xŒW” */ + +const float BATTERY_GAIN = 0.001089F; /* PWMo—ÍŽZo—pƒoƒbƒeƒŠ“dˆ³•â³ŒW” */ +const float BATTERY_OFFSET = 0.625F; /* PWMo—ÍŽZo—pƒoƒbƒeƒŠ“dˆ³•â³ƒIƒtƒZƒbƒg */ + +/******************************** END OF FILE ********************************/ diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..a13d182 --- /dev/null +++ b/main.cpp @@ -0,0 +1,90 @@ +/* main.cpp for TOPPERS/ATK(OSEK) */ + +// ECRobot++ API +#include "Lcd.h" +#include "BlueTooth.h" +#include "Clock.h" +#include "GyroSensor.h" +#include "LightSensor.h" +#include "Motor.h" +#include "Nxt.h" +#include "SonarSensor.h" +#include "Speaker.h" +#include "TouchSensor.h" + +#include "tsprintf.h" +#include "ETLineTracer.h" +#include "ETLinePos.h" +#include "ETBalanceRunner.h" + +using namespace ecrobot; + +extern "C" +{ +#include "kernel.h" +#include "kernel_id.h" +#include "ecrobot_interface.h" + +DeclareCounter(SysTimerCnt);//Alarm—˜—pŽž•K—v + +// global object instances +Nxt nxt; +Lcd lcd; +Speaker speaker; +Bluetooth bt; +Clock clock; +GyroSensor gyro(PORT_1); +SonarSensor sonar(PORT_2); +LightSensor light(PORT_3); +TouchSensor touch(PORT_4); +Motor motorT(PORT_A); +Motor motorR(PORT_B); +Motor motorL(PORT_C); + +ETBalanceRunner runner(motorL,motorR,gyro,nxt); +ETLinePos linepos(light); +ETLineTracer tracer(linepos,runner); + +// nxtOSEK hook to be invoked from an ISR in category 2 +void user_1ms_isr_type2(void) +{ + // //Sleep—˜—pŽž•K—v + SleeperMonitor(); + // //Alarm—˜—pŽž•K—v + StatusType ercd; + ercd = SignalCounter(SysTimerCnt); + if (ercd != E_OK) { + ShutdownOS(ercd); + } +} + +TASK(TaskMain) +{ + lcd.clear(); + lcd.putf("s", "Hello World"); + lcd.disp(); + + TerminateTask(); +} + +TASK(Task4ms) +{ + static int cnt = 0; + char str[16+1]; + lcd.clear(); + tsprintf(str,"cnt = %d",cnt++); // tsprintf Žg—p—á + lcd.putf("s",str); + lcd.disp(); + + tracer.RunTrace(100); + + TerminateTask(); +} + +int __cxa_pure_virtual(){ + // ƒˆ‰¼‘zŠÖ”‚ªƒI[ƒo[ƒ‰ƒCƒh‚µ‚È‚¢‚ŌĂяo‚³‚ꂽŽž‚̃Gƒ‰[ƒnƒ“ƒhƒ‰‚ð‹óŽÀ‘• + // ‚±‚ê‚ð“ü‚ê‚È‚¢‚ÆramŽg—p—Ê‚ª‚Æ‚Ä‚à‘‚¦‚éEEE + return 0; +} + +} // extern "C" diff --git a/main.oil b/main.oil new file mode 100644 index 0000000..3af2646 --- /dev/null +++ b/main.oil @@ -0,0 +1,66 @@ +#include "implementation.oil" + +CPU ATMEL_AT91SAM7S256 +{ + OS LEJOS_OSEK + { + STATUS = EXTENDED; + STARTUPHOOK = FALSE; + ERRORHOOK = FALSE; + SHUTDOWNHOOK = FALSE; + PRETASKHOOK = FALSE; + POSTTASKHOOK = FALSE; + USEGETSERVICEID = FALSE; + USEPARAMETERACCESS = FALSE; + USERESSCHEDULER = FALSE; + }; + + /* Definition of application mode */ + APPMODE appmode1{}; + + /* Definition of Events */ + //SleepŽg—pŽžŽg‚¤ƒCƒxƒ“ƒg‚̃eƒ“ƒvƒŒ[ƒg + EVENT EventSleepI2C{MASK = AUTO;}; + EVENT EventSleep{MASK = AUTO;}; + + /* Definition of TaskMain */ + TASK TaskMain + { + AUTOSTART = TRUE + { + APPMODE = appmode1; + }; + PRIORITY = 1; /* lowest priority */ + ACTIVATION = 1; + SCHEDULE = FULL; + STACKSIZE = 512; + }; + + /* Definition of OSEK Alarm Counter */ + COUNTER SysTimerCnt + { + MINCYCLE = 1; + MAXALLOWEDVALUE = 10000; + TICKSPERBASE = 1; /* One tick is equal to 1msec */ + }; + + TASK Task4ms + { + AUTOSTART = FALSE; + PRIORITY = 1; + ACTIVATION = 1; + SCHEDULE = FULL; + STACKSIZE = 512; + }; + ALARM Alarm4ms + { + COUNTER = SysTimerCnt; + ACTION = ACTIVATETASK{TASK = Task4ms;}; + AUTOSTART = TRUE { + ALARMTIME = 1;//Å‰‚Ìtick (ms) –¾Ž¦‚Ì‚½‚ß0‹ÖŽ~ + CYCLETIME = 4;//ŽüŠútick(ms) + APPMODE = appmode1; + }; + }; + +}; diff --git a/readme.txt b/readme.txt new file mode 100644 index 0000000..f4a38af --- /dev/null +++ b/readme.txt @@ -0,0 +1,20 @@ +======================================================================== + メイクファイル プロジェクト: Tonden プロジェクトの概要 +======================================================================== + +この Tonden プロジェクトは、AppWizard により作成されました。 + +このファイルには、Tonden プロジェクトを構成する各ファイルの内容の概略が記述されています。 + + +Tonden.vcxproj + これは、アプリケーション ウィザードを使用して生成された VC++ プロジェクトのメイン プロジェクト ファイルです。ファイルを生成した Visual C++ のバージョンに関する情報と、アプリケーション ウィザードで選択されたプラットフォーム、構成、およびプロジェクト機能に関する情報が含まれています。 + +Tonden.vcxproj.filters + これは、アプリケーション ウィザードで生成された VC++ プロジェクトのフィルター ファイルです。このファイルには、プロジェクト内のファイルとフィルターとの間の関連付けに関する情報が含まれています。この関連付けは、特定のノードで同様の拡張子を持つファイルのグループ化を示すために IDE で使用されます (たとえば、".cpp" ファイルは "ソース ファイル" フィルターに関連付けられています)。 + +このプロジェクトは、ウィザードで入力したコマンドを呼び出すことにより、Visual Studio からビルド/クリーン/再ビルドできます。ビルド コマンドには、nmake などの任意のツールを使用できます。 + +このプロジェクトにはファイルが含まれないため、ソリューション エクスプローラーには何も表示されません。 + +///////////////////////////////////////////////////////////////////////////// diff --git a/tsprintf.c b/tsprintf.c new file mode 100644 index 0000000..80a2bfa --- /dev/null +++ b/tsprintf.c @@ -0,0 +1,251 @@ +/* + Tiny sprintf module + for Embedded microcontrollers + File Version : 1.0 + Yasuhiro ISHII 2005 + + yƒo[ƒWƒ‡ƒ“ƒAƒbƒv—š—ðz + 0.1 : ‚Æ‚è‚ ‚¦‚¸‚È‚ñ‚©“®‚¢‚½”Å + 0.2 : decimal‚Ì0•\Ž¦‘Ήž 20050313 + 0.3 : hexa decimal‚Ì0•\Ž¦‘Ήž 20050313 + 0.4 : ƒ\[ƒX’†‚̕ςȃR[ƒh(^M)‚ðíœ‚µ‚½ 20050503 + 0.5 : tsprintfŠÖ”‚̕ϐ” size‚ð‰Šú‰»‚·‚é‚悤‚É‚µ‚½ 20050503 + 0.6 : %d‚Ì•‰”‘Ήž,%x‚Ìunsignedˆ—‰» 20050522 + 0.7 : %d,%x‚ÌŒ…”Žw’è(%[n]d)/0•âŠ®Žw’è(%0[n]d)‘Ήž 20050522 + 0.8 : va_list‚Å“n‚·vtsprintf‚ðì¬‚µAvsprintf‚ðvtsprintf‚̐eŠÖ”‚É‚µ‚½ 20050522 + 0.9 : hex‚ŁA’l‚ª0‚ÌŽž‚ÉŒ…‚ª1‚É‚È‚Á‚Ä‚µ‚Ü‚¤ƒoƒOC³ 20050526 + 1.0 : dec‚ŁA’l‚ª0‚ÌŽž‚ÉŒ…‚ª1‚É‚È‚Á‚Ä‚µ‚Ü‚¤ƒoƒOC³ 20050629 + + printf‚̏‘Ž®Ý’è‚ðŠÈˆÕ“I‚È‚à‚Ì‚É‚µ‚ÄŽÀ‘•‚µ‚Ä‚ ‚é‚Ì‚ÅŽg—pŽž‚É‚Í + à–¾‘‚ðŠm”F‚µ‚ĉº‚³‚¢B + +*/ + +#include + +int tsprintf(char* ,char* , ...); +int vtsprintf(char* buff,char* fmt,va_list arg); + +static int tsprintf_string(char* ,char* ); +static int tsprintf_char(int ,char* ); +static int tsprintf_decimal(signed long,char* ,int ,int ); +static int tsprintf_hexadecimal(unsigned long ,char* ,int ,int ,int ); + +/* + Tiny sprintfŠÖ” +*/ +int tsprintf(char* buff,char* fmt, ...){ + va_list arg; + int len; + + len = 0; + va_start(arg, fmt); + + len = vtsprintf(buff,fmt,arg); + + va_end(arg); + return len; +} + +int vtsprintf(char* buff,char* fmt,va_list arg){ + int len; + int size; + int zeroflag,width; + + size = 0; + len = 0; + + while(*fmt){ + if(*fmt=='%'){ /* % ‚ÉŠÖ‚·‚鏈— */ + zeroflag = width = 0; + fmt++; + + if (*fmt == '0'){ + fmt++; + zeroflag = 1; + } + if ((*fmt >= '0') && (*fmt <= '9')){ + width = *(fmt++) - '0'; + } + + /* printf ("zerof = %d,width = %d\n",zeroflag,width); */ + + switch(*fmt){ + case 'd': /* 10i” */ + size = tsprintf_decimal(va_arg(arg,signed long),buff,zeroflag,width); + break; + case 'x': /* 16i” 0-f */ + size = tsprintf_hexadecimal(va_arg(arg,unsigned long),buff,0,zeroflag,width); + break; + case 'X': /* 16i” 0-F */ + size = tsprintf_hexadecimal(va_arg(arg,unsigned long),buff,1,zeroflag,width); + break; + case 'c': /* ƒLƒƒƒ‰ƒNƒ^[ */ + size = tsprintf_char(va_arg(arg,int),buff); + break; + case 's': /* ASCIIZ•¶Žš—ñ */ + size = tsprintf_string(va_arg(arg,char*),buff); + break; + default: /* ƒRƒ“ƒgƒ[ƒ‹ƒR[ƒhˆÈŠO‚Ì•¶Žš */ + /* %%(%‚ɑΉž)‚Í‚±‚±‚őΉž‚³‚ê‚é */ + len++; + *(buff++) = *fmt; + break; + } + len += size; + buff += size; + fmt++; + } else { + *(buff++) = *(fmt++); + len++; + } + } + + *buff = '\0'; /* I’[‚ð“ü‚ê‚é */ + + va_end(arg); + return (len); +} + + + + +/* + ”’l => 10i•¶Žš—ñ•ÏŠ· +*/ +static int tsprintf_decimal(signed long val,char* buff,int zf,int wd){ + int i; + char tmp[10]; + char* ptmp = tmp + 9; + int len = 0; + int minus = 0; + + if (!val){ /* Žw’è’l‚ª0‚̏ꍇ */ + *(ptmp--) = '0'; + len++; + } else { + /* ƒ}ƒCƒiƒX‚Ì’l‚̏ꍇ‚É‚Í2‚̕␔‚ðŽæ‚é */ + if (val < 0){ + val = ~val; + val++; + minus = 1; + } + while (val){ + /* ƒoƒbƒtƒ@ƒAƒ“ƒ_[ƒtƒ[‘΍ô */ + if (len >= 8){ + break; + } + + *ptmp = (val % 10) + '0'; + val /= 10; + ptmp--; + len++; + } + + } + + /* •„†AŒ…‡‚킹‚ÉŠÖ‚·‚鏈— */ + if (zf){ + if (minus){ + wd--; + } + while (len < wd){ + *(ptmp--) = '0'; + len++; + } + if (minus){ + *(ptmp--) = '-'; + len++; + } + } else { + if (minus){ + *(ptmp--) = '-'; + len++; + } + while (len < wd){ + *(ptmp--) = ' '; + len++; + } + } + + /* ¶¬•¶Žš—ñ‚̃oƒbƒtƒ@ƒRƒs[ */ + for (i=0;i 16i•¶Žš—ñ•ÏŠ· +*/ +static int tsprintf_hexadecimal(unsigned long val,char* buff, + int capital,int zf,int wd){ + int i; + char tmp[10]; + char* ptmp = tmp + 9; + int len = 0; + char str_a; + + /* A`F‚ð‘啶Žš‚É‚·‚é‚©¬•¶Žš‚É‚·‚é‚©Ø‚è‘Ö‚¦‚é */ + if (capital){ + str_a = 'A'; + } else { + str_a = 'a'; + } + + if (!val){ /* Žw’è’l‚ª0‚̏ꍇ */ + *(ptmp--) = '0'; + len++; + } else { + while (val){ + /* ƒoƒbƒtƒ@ƒAƒ“ƒ_[ƒtƒ[‘΍ô */ + if (len >= 8){ + break; + } + + *ptmp = (val % 16); + if (*ptmp > 9){ + *ptmp += str_a - 10; + } else { + *ptmp += '0'; + } + + val >>= 4; /* 16‚ÅŠ„‚é */ + ptmp--; + len++; + } + } + while (len < wd){ + *(ptmp--) = zf ? '0' : ' '; + len++; + } + + for (i=0;i 1•¶ŽšƒLƒƒƒ‰ƒNƒ^•ÏŠ· +*/ +static int tsprintf_char(int ch,char* buff){ + *buff = (char)ch; + return(1); +} + +/* + ”’l => ASCIIZ•¶Žš—ñ•ÏŠ· +*/ +static int tsprintf_string(char* str,char* buff){ + int count = 0; + while(*str){ + *(buff++) = *str; + str++; + count++; + } + return(count); +} + diff --git a/tsprintf.h b/tsprintf.h new file mode 100644 index 0000000..20b3fb2 --- /dev/null +++ b/tsprintf.h @@ -0,0 +1,24 @@ +/* + Tiny sprintf module + for Embedded microcontrollers + + (Ver 1.0) +*/ + +#ifndef _TSPRINTF_H_ +#define _TSPRINTF_H_ + +#include + +#ifdef __cplusplus +extern "C"{ +#endif /* __cplusplus */ + +extern int tsprintf(char* ,char* , ...); +extern int vtsprintf(char* buff,char* fmt,va_list arg); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* _TSPRINTF_H_ */