summaryrefslogtreecommitdiff
path: root/arduino/OpenPCR/serialcontrol.h
blob: 5e5b7fe5401015cb59508f0680341422840e8146 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
 *  serialcontrol.h - OpenPCR control software.
 *  Copyright (C) 2010-2011 Josh Perfetto and Xia Hong. All Rights Reserved.
 *
 *  OpenPCR control software is free software: you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License as published
 *  by the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  OpenPCR control software is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along with
 *  the OpenPCR control software.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef _SERIALCONTROL_H_
#define _SERIALCONTROL_H_

#include "thermocycler.h"

#define START_CODE    0xFF
#define ESCAPE_CODE   0xFE

class Display;
class ProgramComponent;
class Cycle;
class Step;
struct SCommand;

typedef enum {
    SEND_CMD       = 0x10,
    STATUS_REQ     = 0x40,
    STATUS_RESP    = 0x80
} PACKET_TYPE;

//packet header
struct PCPPacket {
  PCPPacket(PACKET_TYPE type)
  : startCode(START_CODE)
  , length(0)
  , eType(type)
  {}

  uint8_t startCode;
  uint16_t length;
  uint8_t eType; //lower 4 bits are used for seq
};

class SerialControl {
public:
  SerialControl(Display* pDisplay);
  ~SerialControl();
  
  void Process();
  byte* GetBuffer() { return buf; } //used for stored program parsing at start-up only if no serial command received
  boolean CommandReceived() { return iReceivedStatusRequest; }
  
private:
  void ReadPacket();
  void ProcessPacket(byte* data, int datasize);
  void SendStatus();

  char* AddParam(char* pBuffer, char key, int val, boolean init = false);  
  char* AddParam(char* pBuffer, char key, unsigned long val, boolean init = false);
  char* AddParam(char* pBuffer, char key, float val, int decimalDigits, boolean pad, boolean init = false);
  char* AddParam(char* pBuffer, char key, const char* szVal, boolean init = false);
  char* AddParam_P(char* pBuffer, char key, const char* szVal, boolean init = false);
  
  const char* GetProgramStateString_P(Thermocycler::ProgramState state);
  const char* GetThermalStateString_P(Thermocycler::ThermalState state);
  
private:
  byte buf[MAX_COMMAND_SIZE + 1]; //read or write buffer
  
  typedef enum{
    STATE_START,
    STATE_STARTCODE_FOUND,
    STATE_PACKETLEN_LOW,
    STATE_PACKETHEADER_DONE
  }PACKET_STATE;
  
  PACKET_STATE packetState;
  uint8_t lastPacketSeq, checksum;
  uint16_t packetLen, packetRealLen, iCommandId;
  boolean bEscapeCodeFound;
  boolean iReceivedStatusRequest;
  
  Display* ipDisplay;
};

#endif