peter brier / Mbed 2 deprecated laos_iotest_board_v1
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002  * main.cpp
00003  *
00004  * Copyright (c) 2011 Peter Brier
00005  *
00006  *   This file is part of the LaOS project (see: http://laoslaser.org)
00007  *
00008  *   LaOS is free software: you can redistribute it and/or modify
00009  *   it under the terms of the GNU General Public License as published by
00010  *   the Free Software Foundation, either version 3 of the License, or
00011  *   (at your option) any later version.
00012  *
00013  *   LaOS is distributed in the hope that it will be useful,
00014  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  *   GNU General Public License for more details.
00017  *
00018  *   You should have received a copy of the GNU General Public License
00019  *   along with LaOS.  If not, see <http://www.gnu.org/licenses/>.
00020  *
00021  * This program tests the inputs and outputs of the laoslaser v1 board
00022  * LED1 = xhome
00023  * LED2 = yhome
00024  * LED3 = zmin
00025  * LED4 = zmax
00026  *
00027  */
00028 #include "mbed.h"
00029 #include "SDFileSystem.h"
00030 
00031 // USB Serial
00032 Serial pc(USBTX, USBRX); // tx, rx
00033 
00034 // SD card
00035 SDFileSystem sd(p11, p12, p13, p14, "sd");
00036 
00037 // Analog in/out (cover sensor) + NC
00038 DigitalIn cover(p19);
00039 
00040 // I2C
00041 I2C i2c(p9, p10);        // sda, scl
00042 #define _I2C_ADDRESS 0x04
00043 #define _I2C_HOME 0xFE
00044 #define _I2C_CLS 0xFF
00045 #define _I2C_BAUD 10000
00046 
00047 // status leds
00048 DigitalOut led1(LED1);
00049 DigitalOut led2(LED2);
00050 DigitalOut led3(LED3);
00051 DigitalOut led4(LED4);
00052 
00053 // Inputs;
00054 DigitalIn xhome(p8);
00055 DigitalIn yhome(p17);
00056 DigitalIn zmin(p15);
00057 DigitalIn zmax(p16);
00058 
00059 
00060 // motors
00061 DigitalOut enable(p7);
00062 DigitalOut xdir(p23);
00063 DigitalOut xstep(p24);
00064 DigitalOut ydir(p25);
00065 DigitalOut ystep(p26);
00066 DigitalOut zdir(p27);
00067 DigitalOut zstep(p28);
00068 
00069 // laser
00070 DigitalOut o1(p22);
00071 DigitalOut o2(p21);
00072 DigitalOut o3(p6);
00073 DigitalOut o4(p5);
00074 
00075 void home()
00076 {
00077   int i=0;
00078   xdir = 0;
00079   ydir = 0;
00080   zdir = 0;
00081   led1 = 0;
00082   while ( 1 )
00083   {
00084     xstep = ystep = 0;
00085     wait(0.0001);
00086     xstep = xhome;
00087     ystep = yhome;
00088     wait(0.0001);
00089     
00090     led2 = !xhome;
00091     led3 = !yhome;
00092     led4 = ((i++) & 0x10000);
00093     if ( (!xhome) && (!yhome) ) return;
00094   }
00095 }
00096 
00097 #define TEST(c,x) case c: x = !x; printf("Test %c: " #x " is now %s \r\n", c, (x ? "ON" : "OFF")); break 
00098 
00099 /**
00100 *** main()
00101 **/
00102 int main() 
00103 {
00104   led1 = led2 = led3 = led4 = 1;
00105 
00106   i2c.frequency(_I2C_BAUD);
00107   pc.baud(115200);
00108 
00109   xhome.mode(PullUp);
00110   yhome.mode(PullUp);
00111   zmin.mode(PullUp);
00112   zmax.mode(PullUp); 
00113   
00114   printf("Peforming io test, press 'X' to exit\r\n");
00115   home();
00116   int i=0;
00117   int test=0;
00118   while( 1 )
00119   {
00120     i++;
00121     led1 = xhome;
00122     led2 = yhome;
00123     led3 = zmin;
00124     led4 = zmax;
00125     if ( pc.readable() )
00126       test = pc.getc();
00127     else 
00128       test = 0;
00129     switch( test )
00130     {
00131       TEST('e',enable);
00132       TEST('X',xdir);
00133       TEST('x',xstep);
00134       TEST('Y',ydir);
00135       TEST('y',ystep);
00136       TEST('Z',zdir);
00137       TEST('z',zstep);
00138       TEST('1',o1);
00139       TEST('2',o2);
00140       TEST('3',o3);
00141       TEST('4',o4);
00142       case 's':
00143         char buf[32];
00144         memset(buf,0,sizeof(buf));
00145         printf("Testing IO board SD card: Write...\r\n");
00146         FILE *test1 = fopen("/sd/text.txt", "wb");
00147         fwrite("bla bla\n",8, 1, test1); 
00148         fclose(test1);
00149         printf("Testing IO board SD card: Read...\r\n");
00150         test1 = fopen("/sd/text.txt", "rb");
00151         fread(&buf,8, 1, test1); 
00152         printf("Result: '%s'\r\n", buf);
00153         fclose(test1);
00154         break;
00155       case 'i': 
00156         printf("I2C test:\r\n");
00157         char s[32], key;
00158         int result;
00159         sprintf(s,"Hello world!");
00160         i2c.write(_I2C_ADDRESS, s, strlen(s));
00161         wait(0.01);
00162         result = i2c.read(_I2C_ADDRESS,&key, 1);
00163         printf("Result=%d, key=%d\r\n", result, key);
00164         break;
00165       case 'c':
00166        printf("Cover switch: %s\r\n", (cover ? "OPEN" : "CLOSED"));
00167        break;
00168     }
00169     if ( test ) 
00170     {
00171       printf("xhome=%d, yhome=%d, zmin=%d, zmax=%d\n\r", (int)xhome, (int)yhome, (int)zmin, (int)zmax);
00172     }
00173   }
00174 }