summaryrefslogtreecommitdiff
path: root/arduino/OpenPCR/program.h
blob: ba861d4417a676480cf4bf5d68f393084d774f32 (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
 *  program.h - OpenPCR control software.
 *  Copyright (C) 2010-2011 Josh Perfetto. 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 _PROGRAM_H_
#define _PROGRAM_H_

class Step;

////////////////////////////////////////////////////////////////////
// Class ProgramComponent
class ProgramComponent {
public:
  enum TType {
    EStep = 0,
    ECycle
  };
  
  virtual void Reset() = 0;
  virtual TType GetType() = 0;
  
  // iteration
  virtual void BeginIteration() = 0;
  virtual Step* GetNextStep() = 0;
};

////////////////////////////////////////////////////////////////////
// Class Step
class Step: public ProgramComponent {
public:  
  // accessors
  char* GetName() { return iName; }
  int GetDuration() { return iDuration; }
  float GetTemp() { return iTemp; }
  virtual TType GetType() { return EStep; }
  boolean IsFinal() { return iDuration == 0; }

  // mutators
  void SetDuration(int duration) { iDuration = duration; }
  void SetTemp(float temp) { iTemp = temp; }
  void SetName(const char* szName);
  
  virtual void Reset();
  
  // iteration
  virtual void BeginIteration();
  virtual Step* GetNextStep();

private:
  int iDuration; //in seconds
  float iTemp; // C
  boolean iStepReturned;
  char iName[STEP_NAME_LENGTH];
};

////////////////////////////////////////////////////////////////////
// Class Cycle
class Cycle: public ProgramComponent {
public:
  // accessors
  virtual TType GetType() { return ECycle; }
  int GetCurrentCycle() { return iCurrentCycle + 1; } //add 1 because cycles start at 0
  int GetNumCycles() { return iNumCycles; }
  int GetNumComponents() { return iNumComponents; }
  ProgramComponent* GetComponent(int index);
  
  // mutators
  void SetNumCycles(int numCycles) { iNumCycles = numCycles; }
  PcrStatus AddComponent(ProgramComponent* pComponent); //takes ownership
  virtual void Reset();
  
  // iteration
  virtual void BeginIteration();
  virtual Step* GetNextStep();
  
private:
  void RestartCycle();

private:
  ProgramComponent* iComponents[MAX_CYCLE_ITEMS];
  int iNumComponents;
  int iNumCycles;
  
  int iCurrentCycle;
  int iCurrentComponent; // -1 means no component
};

////////////////////////////////////////////////////////////////////
// Class ProgramComponentPool
template <class T, int N>
class ProgramComponentPool {
public:
  ProgramComponentPool() { iAllocatedComponents = 0; }

  T* AllocateComponent() { 
    if (iAllocatedComponents == N)
      return NULL;
      
    T* pComponent = &iComponents[iAllocatedComponents++];
    pComponent->Reset();
    return pComponent;
  }
  
  void ResetPool() { iAllocatedComponents = 0; }
  
private:
  int iAllocatedComponents;
  T iComponents[N];
};

////////////////////////////////////////////////////////////////////
// Struct SCommand
struct SCommand {
  char name[21];
  uint16_t commandId;
  enum TCommandType {
    ENone = 0,
    EStart,
    EStop,
    EConfig
  } command;
  int lidTemp;
  uint8_t contrast;
  Cycle* pProgram;
};

////////////////////////////////////////////////////////////////////
// Class CommandParser
class CommandParser {
public:
  static void ParseCommand(SCommand& command, char* pCommandBuf);

private:
  static void AddComponent(SCommand* pCommand, char key, char* szValue);
  static Cycle* ParseProgram(char* pBuffer);
  static ProgramComponent* ParseCycle(char* pBuffer);
  static Step* ParseStep(char* pBuffer);
};

////////////////////////////////////////////////////////////////////
// Class ProgramStore
class ProgramStore {
public:
  //reading
  static uint8_t RetrieveContrast();
  static boolean RetrieveProgram(SCommand& command, char* pBuffer);

  //writing
  static void StoreContrast(uint8_t contrast);
  static void StoreProgram(const char* szProgram);
};
  

#endif