Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
main.cpp
00001 /* 00002 * This program is free software; you can redistribute it and/or modify 00003 * it under the terms of the GNU General Public License as published by 00004 * the Free Software Foundation; either version 2 of the License, or 00005 * (at your option) any later version. 00006 * 00007 * This program is distributed in the hope that it will be useful, 00008 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00009 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00010 * GNU General Public License for more details. 00011 * 00012 * You should have received a copy of the GNU General Public License 00013 * along with this program; if not, write to the Free Software 00014 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 00015 * MA 02110-1301, USA. 00016 * 00017 */ 00018 #include "mbed.h" 00019 #include "TransLayer.h" 00020 #include "MbedConsole.h" 00021 #include "MbedPerAccess.h" 00022 00023 /** 00024 * \brief The main entry point of our application. 00025 * \author Navin Bhaskar 00026 */ 00027 00028 Serial pc(USBTX, USBRX); 00029 /** 00030 * \fn number(char* buff) 00031 * \brief This function converts a string into a number. 00032 * \param[in] buff buffer containg string representation of a number (should be decimal representation). 00033 * \retrun converted number 00034 */ 00035 00036 int number(char *buff) 00037 { 00038 int i = strlen(buff); 00039 int j,temp=0; 00040 00041 for(j=0; j<i; j++) { 00042 if(buff[j] >= '0' && buff[j] <= '9') { 00043 temp = temp*10; 00044 temp = temp + buff[j] - '0'; 00045 00046 } 00047 } 00048 00049 return temp; 00050 } 00051 /** 00052 * \fn pin_control(char* buff, int len) 00053 * \brief sets or resets a pin 00054 */ 00055 void pin_control(Console * cons, PerAccess * per, char* buff, int len) 00056 { 00057 uint temp = number(buff); 00058 uint pinno, pinst; 00059 uint status; 00060 00061 if( len < 3) { 00062 cons->printErr(ERR_INVALID_ARG); 00063 } else { 00064 pinst = temp%10; // LSB is pin state 00065 pinno = temp/10; // rest of it is pin number 00066 status = per->digitalOut(pinno, pinst); 00067 cons->printErr(status); 00068 } 00069 } 00070 00071 /** 00072 * \fn analog_out(char* buff, int len) 00073 * \brief Outputs an anolog voltage on a given PWM channel 00074 */ 00075 00076 void analog_out(Console * cons, PerAccess * per, char* buff, int len) 00077 { 00078 int temp = number(buff); 00079 int pinno, pinval; 00080 uint status; 00081 if( len < 3) { 00082 cons->printErr(ERR_INVALID_ARG); 00083 return ; 00084 } 00085 00086 pinno = temp&0xff; // LSB is pin value 00087 pinval = temp>>8; // MSB is pin no 00088 00089 status = per->analogOut(pinno, pinval); 00090 cons->printErr(status); 00091 } 00092 00093 /** 00094 * \fn analog_in(char* buff, int len) 00095 * \brief This function reads an analog volatge on a given channel and prints 00096 * it over on the serial terminal 00097 */ 00098 00099 void analog_in(Console * cons, PerAccess * per, char* buff, int len) 00100 { 00101 uint adc_val; 00102 uint ch=number(buff); 00103 uint status; 00104 status = per->analogIn(ch, &adc_val); 00105 if (status == ERR_SUCCESS) { 00106 cons->printf("%d\n", adc_val); 00107 } 00108 cons->printErr(status); 00109 } 00110 00111 /** 00112 * \fn read_pin(char* buff, int len) 00113 * \brief This function reads digital logic level at a specified pin and prints 00114 * it over serial port prints 1 if high else it prints 0 00115 */ 00116 00117 void read_pin(Console * cons, PerAccess * per, char* buff, int len) 00118 { 00119 uint read_val; 00120 uint pin=number(buff); 00121 uint status; 00122 00123 status = per->digitalIn(pin, &read_val); 00124 00125 if (status == ERR_SUCCESS) { 00126 cons->printf("%d\n", read_val); 00127 } 00128 cons->printErr(status); 00129 } 00130 00131 00132 int main(void) 00133 { 00134 TransLayer comm_packet; 00135 MbedConsole cons; 00136 MbedPerAccess per; 00137 00138 Console *transCons; 00139 PerAccess *transPer; 00140 00141 transCons = &cons; 00142 transPer = &per; 00143 00144 00145 comm_packet.AddService(pin_control, 'P'); 00146 comm_packet.AddService(analog_out, 'A'); 00147 comm_packet.AddService(analog_in, 'I'); 00148 comm_packet.AddService(read_pin, 'R'); 00149 comm_packet.MainLoop(transCons, transPer); 00150 00151 while(1); 00152 }
Generated on Thu Aug 4 2022 08:49:39 by
1.7.2