Wiselib
|
00001 /*************************************************************************** 00002 ** This file is part of the generic algorithm library Wiselib. ** 00003 ** Copyright (C) 2008,2009 by the Wisebed (www.wisebed.eu) project. ** 00004 ** ** 00005 ** The Wiselib is free software: you can redistribute it and/or modify ** 00006 ** it under the terms of the GNU Lesser General Public License as ** 00007 ** published by the Free Software Foundation, either version 3 of the ** 00008 ** License, or (at your option) any later version. ** 00009 ** ** 00010 ** The Wiselib is distributed in the hope that it will be useful, ** 00011 ** but WITHOUT ANY WARRANTY; without even the implied warranty of ** 00012 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ** 00013 ** GNU Lesser General Public License for more details. ** 00014 ** ** 00015 ** You should have received a copy of the GNU Lesser General Public ** 00016 ** License along with the Wiselib. ** 00017 ** If not, see <http://www.gnu.org/licenses/>. ** 00018 ***************************************************************************/ 00019 #ifndef CONNECTOR_ISENSE_SERIAL_COM_BUFFERUART0_H 00020 #define CONNECTOR_ISENSE_SERIAL_COM_BUFFERUART0_H 00021 00022 #include "external_interface/isense/isense_types.h" 00023 #include "util/delegates/delegate.hpp" 00024 #include "util/base_classes/uart_base.h" 00025 #include <isense/os.h> 00026 #include <isense/uart.h> 00027 #include <isense/task.h> 00028 00029 namespace wiselib 00030 { 00031 00042 template<typename OsModel_P, 00043 int UART = 0> 00044 class iSenseSerialComBufferUartModel 00045 : public isense::Uint8DataHandler, 00046 public isense::Task, 00047 public UartBase<OsModel_P, uint8, uint8> 00048 { 00049 public: 00050 typedef OsModel_P OsModel; 00051 00052 typedef iSenseSerialComBufferUartModel<OsModel, UART> self_t; 00053 typedef self_t* self_pointer_t; 00054 00055 typedef uint8 block_data_t; 00056 typedef uint8 size_t; 00057 #ifdef ISENSE_ENABLE_UART_16BIT 00058 typedef uint16 uart_packet_length_t; 00059 #else 00060 typedef uint8 uart_packet_length_t; 00061 #endif 00062 // -------------------------------------------------------------------- 00063 enum ErrorCodes 00064 { 00065 SUCCESS = OsModel::SUCCESS, 00066 ERR_UNSPEC = OsModel::ERR_UNSPEC 00067 }; 00068 // -------------------------------------------------------------------- 00069 iSenseSerialComBufferUartModel( isense::Os& os ) 00070 : os_ ( os ), 00071 data_ ( 0 ), 00072 wait_for_delivery_( false ), 00073 errors_ ( 0 ) 00074 {} 00075 // ----------------------------------------------------------------------- 00076 int enable_serial_comm() 00077 { 00078 os().uart(UART).enable(); 00079 os().uart(UART).set_uint8_handler( this ); 00080 os().uart(UART).enable_interrupt( true ); 00081 00082 return SUCCESS; 00083 } 00084 // ----------------------------------------------------------------------- 00085 int disable_serial_comm() 00086 { 00087 os().uart(UART).disable(); 00088 return SUCCESS; 00089 } 00090 // ----------------------------------------------------------------------- 00091 int write( size_t len, block_data_t *buf ) 00092 { 00093 //os().debug( "got %s", buf ); 00094 os().uart(UART).write_buffer( (char*)buf, len ); 00095 00096 return SUCCESS; 00097 } 00098 // ----------------------------------------------------------------- 00099 void handle_uint8_data( uint8 data ) 00100 { 00101 //os().debug( "handle %d", data ); 00102 if ( wait_for_delivery_ ) 00103 { 00104 errors_++; 00105 return; 00106 } 00107 00108 // FIXME: Write in small buffer, then send as much of buffer as 00109 // available in execute()? 00110 data_ = data; 00111 00112 wait_for_delivery_ = true; 00113 os().add_task( this, 0 ); 00114 } 00115 // ----------------------------------------------------------------- 00116 virtual void execute( void* userdata ) 00117 { 00118 self_t::notify_receivers( 1, &data_ ); 00119 wait_for_delivery_ = false; 00120 } 00121 00122 private: 00123 isense::Os& os() 00124 { return os_; } 00125 // -------------------------------------------------------------------- 00126 isense::Os& os_; 00127 00128 uint8 data_; 00129 volatile bool wait_for_delivery_; 00130 volatile int errors_; 00131 }; 00132 } 00133 00134 #endif