Darien Figueroa / Mbed 2 deprecated repo3

Dependencies:   mbed MAX14720 MAX30205 USBDevice

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers DataLogPipeline.cs Source File

DataLogPipeline.cs

00001 /*******************************************************************************
00002 * Copyright (C) 2016 Maxim Integrated Products, Inc., All rights Reserved.
00003 * 
00004 * This software is protected by copyright laws of the United States and
00005 * of foreign countries. This material may also be protected by patent laws
00006 * and technology transfer regulations of the United States and of foreign
00007 * countries. This software is furnished under a license agreement and/or a
00008 * nondisclosure agreement and may only be used or reproduced in accordance
00009 * with the terms of those agreements. Dissemination of this information to
00010 * any party or parties not specified in the license agreement and/or
00011 * nondisclosure agreement is expressly prohibited.
00012 *
00013 * The above copyright notice and this permission notice shall be included
00014 * in all copies or substantial portions of the Software.
00015 *
00016 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00017 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00018 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00019 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
00020 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
00021 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
00022 * OTHER DEALINGS IN THE SOFTWARE.
00023 *
00024 * Except as contained in this notice, the name of Maxim Integrated
00025 * Products, Inc. shall not be used except as stated in the Maxim Integrated
00026 * Products, Inc. Branding Policy.
00027 *
00028 * The mere transfer of this software does not imply any licenses
00029 * of trade secrets, proprietary technology, copyrights, patents,
00030 * trademarks, maskwork rights, or any other form of intellectual
00031 * property whatsoever. Maxim Integrated Products, Inc. retains all
00032 * ownership rights.
00033 *******************************************************************************
00034 */
00035 
00036 using System;
00037 using System.Collections.Generic;
00038 using System.Linq;
00039 using System.Text;
00040 using RPCSupport.Pipelines;
00041 using System.Globalization;
00042 
00043 namespace RPCSupport
00044 {
00045     public class DataLogPipeline : Pipeline
00046     {
00047         int page;
00048         bool isConnected;
00049         bool endOfLog = false;
00050         Pipeline proxyPipeline;
00051 
00052         const int  DATALOG_PARAMETER_PAGE = 0x0;
00053         const int  DATALOG_RESUME_TABLE_PAGE = 0x10;
00054         const int  DATALOG_DATA_PAGE = 0x12;
00055         class DataLogPacket
00056         {
00057             public int index;
00058             public char[] buffer;
00059             public int LengthLeft()
00060             {
00061                 return buffer.Length - index;
00062             }
00063             public char FetchChar()
00064             {
00065                 char ch = (char)buffer[index++];
00066                 return ch;
00067             }
00068         }
00069         Queue<DataLogPacket> queue = new Queue<DataLogPacket>();
00070         public void SetProxyPipeline(Pipeline pipeline)
00071         {
00072             page = DATALOG_DATA_PAGE;
00073             isConnected = true;
00074             proxyPipeline = pipeline;
00075             endOfLog = false;
00076         }
00077 
00078         public override string RawRpcCall(string request, bool reply)
00079         {
00080             throw new NotImplementedException();
00081         }
00082 
00083         public override void SendSingleByte(char ch)
00084         {
00085             throw new NotImplementedException();
00086         }
00087 
00088         public override void Discard()
00089         {
00090             throw new NotImplementedException();
00091         }
00092 
00093         public override string RawRpcCallBinary(string request, bool reply, out bool allOnes)
00094         {
00095             throw new NotImplementedException();
00096         }
00097 
00098         public override bool IsConnected()
00099         {
00100             return true;
00101         }
00102 
00103         public override string ReadString()
00104         {
00105             throw new NotImplementedException();
00106         }
00107 
00108         public override int Read(char[] buffer, int offset, int count)
00109         {
00110             int numberRead = 0;
00111             char[] currentBuffer = ReadDataLog(buffer, offset, count, out numberRead);
00112             //String returnStr = "";
00113             //bool allOnes;
00114             //returnStr = proxyPipeline.CallBinary("S25FS512", "ReadPagesBinary", out allOnes, new String[] { page.ToString("X2"), page.ToString("X2") });
00115             return numberRead;
00116         }
00117 
00118         private char[] ReadDataLog(char[] buffer, int offset, int count, out int numberRead)
00119         {
00120             // if queue empty then read from datalog
00121             if (queue.Count == 0)
00122             {
00123                 QueuePage();
00124             }
00125              // get the log packet at the top of the queue
00126             DataLogPacket currentPacket = queue.Peek();
00127            // check if we need to get another page queued if more buffer count is needed
00128             bool extraQueued = false;
00129             int currentPacketIndex = currentPacket.index;
00130             int currentPacketLengthLeft = currentPacket.LengthLeft();
00131             if (currentPacket.LengthLeft() < count)
00132             {
00133                 QueuePage();
00134                 extraQueued = true;
00135             }
00136             // get data from the queue
00137             for (int i = 0; i < count; i++)
00138             {
00139                 buffer[i] = currentPacket.FetchChar();
00140                 // if there is no data left in this packet then get the next packet
00141                 if (currentPacket.LengthLeft() == 0 && i != (count - 1))
00142                 {
00143                     if (queue.Count != 2)
00144                     {
00145                     }
00146                     queue.Dequeue();
00147                     currentPacket = queue.Peek();
00148                 }
00149             }
00150             // if this is the last buffer then return count of 0 to end the stream
00151             //if (IsAllFF(currentPacket.buffer) == true) count = 0;
00152             // if the DataLogPacket is exhasted then dequeue it, we are finished with it
00153             if (currentPacket.LengthLeft() == 0)
00154             {
00155                 queue.Dequeue();
00156             }
00157             if (endOfLog == true)
00158             {
00159                 count = 0;
00160             }
00161             numberRead = count;
00162             return buffer;
00163         }
00164 
00165         private void QueuePage()
00166         {
00167             byte[] buf = ReadPageFromFlash(page);
00168             if (page >= 0x18F)
00169             {
00170                 //endOfLog = true;
00171             }
00172             if (IsAllFF(buf) == true)
00173             {
00174                 endOfLog = true;
00175             }
00176             page++;
00177             char[] charArray = BinToCharArray(buf);
00178             DataLogPacket packet = new DataLogPacket();
00179             packet.buffer = charArray;
00180             packet.index = 0;
00181             queue.Enqueue(packet);
00182         }
00183 
00184         private char[] BinToCharArray(byte[] buf)
00185         {
00186             int val;
00187             int val1;
00188             int val2;
00189             int val3;
00190             int val4;
00191             StringBuilder sb = new StringBuilder();
00192             for (int i = 0; i < buf.Length / 4; i++) {
00193                 val1 = (int)buf[i*4 + 3] << 24;
00194                 val2 = (int)buf[i*4 + 2] << 16;
00195                 val3 = (int)buf[i*4 + 1] << 8;
00196                 val4 = (int)buf[i*4 + 0] << 0;
00197                 val = val1 + val2 + val3 + val4;
00198                 sb.Append(String.Format("{0:X} ", val));
00199             }
00200             char[] chars = sb.ToString().ToCharArray();
00201             return chars;
00202         }
00203 
00204         private byte[] ReadPageFromFlash(int pageToRead)
00205         {
00206             String returnStr = "";
00207             bool allOnes;
00208             returnStr = proxyPipeline.CallBinary("S25FS512", "ReadPagesBinary", out allOnes, new String[] { pageToRead.ToString("X2"), pageToRead.ToString("X2") });
00209             byte[] array = LineToArray(returnStr);
00210             return array;
00211         }
00212 
00213         private bool IsAllFF(byte[] buffer)
00214         {
00215             for (int i = 0; i < buffer.Length; i++)
00216             {
00217                 if (buffer[i] != 0xFF) return false;
00218             }
00219             return true;
00220         }
00221 
00222         private byte[] LineToArray(String hexStr)
00223         {
00224             byte val;
00225             List<byte> intList = new List<byte>();
00226             String[] strings = hexStr.Split(new char[] { ' ' });
00227             foreach (String str in strings)
00228             {
00229                 if (byte.TryParse(str, NumberStyles.HexNumber, CultureInfo.CurrentCulture, out val))
00230                 {
00231                     intList.Add(val);
00232                 }
00233                 else
00234                 {
00235                     throw new Exception(String.Format("Error In hex string \"{0}\" in \"{1}\"", str, hexStr));
00236                 }
00237             }
00238             return intList.ToArray();
00239         }
00240     }
00241 }