A project to implement a console using the Mbed using VGA for video output and a PS/2 keyboard for the input. The eventual goal is to also include tools for managing SD cards, and a semi-self-hosting programming environment.

Dependencies:   PS2_MbedConsole fastlib SDFileSystem vga640x480g_mbedconsole lightvm mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers textio.cpp Source File

textio.cpp

00001 /*
00002 <Copyright Header>
00003 Copyright (c) 2012 Jordan "Earlz" Earls  <http://lastyearswishes.com>
00004 All rights reserved.
00005 
00006 Redistribution and use in source and binary forms, with or without
00007 modification, are permitted provided that the following conditions
00008 are met:
00009 
00010 1. Redistributions of source code must retain the above copyright
00011    notice, this list of conditions and the following disclaimer.
00012 2. Redistributions in binary form must reproduce the above copyright
00013    notice, this list of conditions and the following disclaimer in the
00014    documentation and/or other materials provided with the distribution.
00015 3. The name of the author may not be used to endorse or promote products
00016    derived from this software without specific prior written permission.
00017    
00018 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
00019 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
00020 AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
00021 THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00022 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00023 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
00024 OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
00025 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
00026 OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
00027 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00028 
00029 This file is part of the MbedConsole project
00030 */
00031 
00032 #include "mbedconsole.h"
00033 #include "keyboard.h"
00034 
00035 #define FONTHEIGHT 16
00036 #define FONTWIDTH 8
00037 
00038 int console_x=0, console_y=(250/15);
00039 int console_color=WHITE; //text color
00040 
00041 
00042 void vsetcursor(int x, int y)
00043 {
00044     console_x=x;
00045     console_y=y;
00046 }
00047 
00048 void vrawputc(char c)
00049 {
00050     //fuck that shitv
00051     if(console_x>80)
00052     {
00053         return;
00054     }
00055     //shift left for fast multiply
00056     vga_putchar(console_x<<3, console_y<<4, c, console_color);
00057 }
00058 void vputc(char c)
00059 {
00060     //shift left for fast multiply
00061     if(console_x>=79)
00062     {
00063         console_x=0;
00064         console_y++;
00065     }
00066     if(console_y>=29)
00067     {
00068         console_y--;
00069         vga_scroll();
00070     }
00071     switch(c){
00072         case '\n':
00073         case '\r':
00074         console_y++;
00075         console_x=0;
00076         break;
00077         case '\b':
00078         vrawputc(' ');
00079         if(console_x>0)
00080         {
00081             console_x--;
00082         }
00083         vrawputc(' ');
00084         break;
00085         case '\t':
00086         for(int i=0;i<4;i++)
00087         {
00088             console_x++;
00089             vrawputc(' ');
00090         }
00091         default:
00092         vga_putchar(console_x<<3, console_y<<4, c, console_color);
00093         console_x++;
00094     }
00095 }
00096 
00097 void vputs(char *s){
00098     while(*s!=0){
00099         vputc(*s);
00100         s++;
00101     }
00102 }
00103 
00104 char vgetc()
00105 {
00106     char tmp=kbd_GetKey();
00107     vputc(tmp);
00108     return tmp;
00109 }
00110 int vgetsl(char *buf, int len)
00111 {
00112     int pos=0;
00113     while(1){
00114         buf[pos]=kbd_GetKey();
00115         if(buf[pos]=='\r'){
00116             buf[pos]='\n';
00117         }
00118         
00119         vputc(buf[pos]);
00120         if(buf[pos]=='\b'){
00121             buf[pos]=0;
00122             if(pos>0){
00123                 pos--;
00124                 buf[pos--]=0;
00125             }
00126         }
00127         if(pos>len-1){
00128             break;
00129         }
00130         if(buf[pos]=='\n'){
00131             buf[pos]=0;
00132             return 1;
00133         }
00134         pos++;
00135     }
00136     return 0;
00137 }
00138 
00139 
00140 
00141 int strlcmp(const char *s1,const char *s2,size_t count){
00142     int i=0;
00143     while((s1[i]!=0) && (s2[i]!=0)){
00144         if(s1[i]!=s2[i]){
00145             return -1;
00146         }
00147         if(i>=count){
00148             return -1;
00149         }
00150         i++;
00151         
00152     }
00153     if(s1[i]!=s2[i]){
00154         return -1;
00155     }
00156     return 0;
00157 }
00158