Tedd OKANO / Mbed 2 deprecated AnalogClock_StepperMotor_NTP

Dependencies:   mbed NetEthApiLPC1768 NetServicesLPC1768

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /** Clock demo with stepper motor + NTP
00002  *
00003  *  Simple mbed analog clock demo with stepper motor.
00004  *  Time information will be get from NTP
00005  *  This code based on
00006  *  http://mbed.org/users/nucho/programs/SimpleLCDClock/5zi4y
00007  *  http://mbed.org/users/okano/notebook/nxp_pcf2127a-demo-code/
00008  *
00009  *  2010-Nov-20  @tedd_okano
00010  *  Released under the MIT License: http://mbed.org/license/mit
00011  */
00012 
00013 #include "mbed.h"
00014 #include "EthernetNetIf.h"
00015 #include "NtpClient.h"
00016 #include "TextLCD.h"
00017 #include "StepperMotor.h"
00018 
00019 
00020 EthernetNetIf   eth;
00021 NtpClient       ntp;
00022 TextLCD         lcd( p14, p15, p16, p17, p18, p19 ); // rs, rw, d0, d1, d2, d3
00023 Ticker          updater;
00024 StepperMotor    m_s( p21, p22, p23, p30 );
00025 StepperMotor    m_m( p24, p25, p26, p30 );
00026 StepperMotor    m_h( p27, p28, p29, p30 );
00027 AnalogIn        ain( p20 );
00028 
00029 int     tz_index            = 4;
00030 int     update_stop_lcd     = false;
00031 int     update_stop_hands   = false;
00032 int     resync              = false;
00033 
00034 #define RESYNC_INTERVAL     1  //  minutes
00035 
00036 typedef    struct _tz    {
00037     char    *name;
00038     float   offset;
00039 }
00040 tz;
00041 
00042 //  http://wwp.greenwichmeantime.com/info/timezone.htm
00043 
00044 tz  timezone[ 8 ]  = {
00045     { "London",          0 },
00046     { "Amsterdam",       1 },
00047     { "India",           5.5 },
00048     { "Singapore",       8 },
00049     { "Tokyo",           9 },
00050     { "Hawaii",        -10 },
00051     { "San Francisco",  -8 },
00052     { "New York",       -5 },
00053 };
00054 
00055 void clock_update( void );
00056 void show_clock( int h, int m, int s );
00057 int access_NTP( void );
00058 
00059 int main() {
00060     m_h.go_angle( 180 );
00061     m_m.go_angle( 180 );
00062     m_s.go_angle( 180 );
00063 
00064     lcd.cls();
00065     lcd.printf( "Ethernet setting" );
00066 
00067     EthernetErr ethErr = eth.setup();
00068 
00069     if (ethErr) {
00070         lcd.printf("Error %d in setup.\n", ethErr);
00071         return -1;
00072     }
00073 
00074     if ( access_NTP() ) {
00075         m_h.go_angle( 0 );
00076         m_m.go_angle( 0 );
00077         m_s.go_angle( 0 );
00078 
00079         return -1;
00080     }
00081 
00082     while ( m_h.distance() | m_m.distance() | m_s.distance() )
00083         wait( 0.1 );
00084 
00085     m_h.set_home_position();
00086     m_m.set_home_position();
00087     m_s.set_home_position();
00088 
00089     lcd.cls();
00090 
00091     updater.attach(&clock_update, 1.0);
00092 
00093     int     tz_i    = 0;
00094     int     tz_i_p  = -1;
00095     int     unstable = 50;
00096 
00097     while (1) {
00098 
00099         if ( resync ) {
00100             update_stop_hands   = true;
00101 
00102             m_h.go_angle( 0 );
00103             m_m.go_angle( 0 );
00104             m_s.go_angle( 0 );
00105 
00106             if ( access_NTP() ) {
00107                 return -1;
00108             }
00109 
00110             update_stop_hands   = false;
00111             resync              = false;
00112             tz_i_p              = -1;
00113         }
00114         tz_i    = (int)( ain * 7.99 );
00115 
00116         if ( tz_i != tz_i_p ) {
00117             update_stop_lcd = true;
00118             lcd.locate( 0, 1 );
00119             lcd.printf( "                " );
00120             lcd.locate( 0, 1 );
00121             lcd.printf( "%s", timezone[ tz_i ].name );
00122             update_stop_lcd = false;
00123 
00124             tz_i_p      = tz_i;
00125             unstable    = 50;
00126         } else {
00127             unstable    = unstable ? unstable-- : 0;
00128             tz_index    = unstable ? tz_i : tz_index;
00129         }
00130         wait( 0.1 );
00131     }
00132 }
00133 
00134 void clock_update( void ) {
00135     struct tm   dt, *dtp;
00136     time_t      t;
00137     char        s[ 30 ];
00138     dtp = &dt;
00139 
00140     t       = time( NULL ) + (int)(timezone[ tz_index ].offset * 3600.0);
00141     dtp     = localtime( &t );
00142 
00143     if ( !(dtp->tm_min % RESYNC_INTERVAL) && !(dtp->tm_sec) )
00144         resync  = true;
00145 
00146     if ( !update_stop_hands )
00147         show_clock( dtp->tm_hour, dtp->tm_min, dtp->tm_sec );
00148 
00149     if ( update_stop_lcd )
00150         return;
00151 
00152     strftime( s, 20, "%H:%M:%S", dtp );
00153     lcd.locate( 0, 0 );
00154     lcd.printf( "%s", s );
00155 
00156 #if 0
00157     strftime( s, 20, "%Y/%b/%d(%a)", dtp );
00158     lcd.locate( 0, 1 );
00159     lcd.printf( "%s", s );
00160 #endif
00161 }
00162 
00163 void show_clock( int h, int m, int s ) {
00164     printf( "%02d:%02d:%02d\r\n",h,m,s );
00165 
00166     h   = h % 12;
00167     m_s.go_angle( -((float)s * 360.0) / 60.0 );
00168     m_m.go_angle( -((float)m * 360.0) / 60.0 );
00169     m_h.go_angle( -(((float)h + ((float)m / 60.0)) * 360.0) / 12.0 );
00170 }
00171 
00172 int access_NTP( void ) {
00173     lcd.cls();
00174     lcd.printf( "accessing NTP.. " );
00175 
00176     lcd.locate( 0, 0 );
00177     lcd.printf("Setup OK");
00178 
00179     lcd.printf("Timer setting...\n");
00180     Host server(IpAddr(), 123, "ntp.jst.mfeed.ad.jp");
00181     ntp.setTime(server);
00182 
00183     lcd.cls();
00184 
00185     return 0;
00186 }
00187