summaryrefslogtreecommitdiff
path: root/arduino/OpenPCR/program.cpp
blob: 4565a29253d4267dd44cd9851fb4a3756a53c6cb (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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*
 *  program.cpp - 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/>.
 */

#include "pcr_includes.h"
#include "program.h"

#include <EEPROM.h>

#include "display.h"

////////////////////////////////////////////////////////////////////
// Class Step
void Step::SetName(const char* szName) {
  strncpy(iName, szName, sizeof(iName));
  iName[sizeof(iName) - 1] = '\0';
}

void Step::Reset() {
  iStepReturned = false;
  iDuration = 0;
  iTemp = 0;
  iName[0] = '\0'; 
}

void Step::BeginIteration() {
  iStepReturned = false;
}

Step* Step::GetNextStep() {
  if (iStepReturned) {
    return NULL;
  } else {
    iStepReturned = true;
    return this;
  }
}

////////////////////////////////////////////////////////////////////
// Class Cycle
ProgramComponent* Cycle::GetComponent(int index) {
  return iComponents[index];
}

PcrStatus Cycle::AddComponent(ProgramComponent* pComponent) {
  if (iNumComponents >= MAX_CYCLE_ITEMS)
    return ETooManySteps;
  
  iComponents[iNumComponents++] = pComponent;
  return ESuccess;
}

void Cycle::Reset() {
  iNumComponents = 0;
  iNumCycles = 0;
  iCurrentCycle = 0;
  iCurrentComponent = 0;
}

// iteration
void Cycle::BeginIteration() {
  iCurrentCycle = 0;
  RestartCycle();
}

Step* Cycle::GetNextStep() {
  //check for next step of existing component
  Step* pNextStep = iComponents[iCurrentComponent]->GetNextStep();
  
  //advance to next component if current component done
  if (pNextStep == NULL && ++iCurrentComponent < iNumComponents)
    pNextStep = iComponents[iCurrentComponent]->GetNextStep(); //should never be NULL
  
  //advance to next cycle if current cycle done
  if (pNextStep == NULL && ++iCurrentCycle < iNumCycles) {
    RestartCycle();
  
    //return first component of that cycle
    pNextStep = iComponents[iCurrentComponent]->GetNextStep(); //should never be NULL
  }
      
  return pNextStep;
}

void Cycle::RestartCycle() {
  iCurrentComponent = 0;
  
  for (int i = 0; i < iNumComponents; i++)
    iComponents[i]->BeginIteration();
}

////////////////////////////////////////////////////////////////////
// Class CommandParser
void CommandParser::ParseCommand(SCommand& command, char* pCommandBuf) {
  char* pValue;
  memset(&command, NULL, sizeof(command));
  char buf[32];

  gpThermocycler->Stop(); //need to stop here to reset program pools
    
  char* pParam = strtok(pCommandBuf, "&");
  while (pParam) {  
    pValue = strchr(pParam, '=');
    *pValue++ = '\0';
    AddComponent(&command, pParam[0], pValue);
    pParam = strtok(NULL, "&");
  }
}

void CommandParser::AddComponent(SCommand* pCommand, char key, char* szValue) {
  switch(key) {
  case 'n':
    strncpy(pCommand->name, szValue, sizeof(pCommand->name) - 1);
    pCommand->name[sizeof(pCommand->name) - 1] = '\0';
    break;
  case 'c':
    if (strcmp(szValue, "start") == 0)
      pCommand->command = SCommand::EStart;
    else if (strcmp(szValue, "stop") == 0)
      pCommand->command = SCommand::EStop;
    else if (strcmp(szValue, "cfg") == 0)
      pCommand->command = SCommand::EConfig;
    break;
  case 'l':
    pCommand->lidTemp = atoi(szValue);
    break;
  case 'o':
    pCommand->contrast = atoi(szValue);
  case 'd':
    pCommand->commandId = atoi(szValue);
    break;
  case 'p':
    pCommand->pProgram = ParseProgram(szValue);
    break;
  }
}

Cycle* CommandParser::ParseProgram(char* pBuffer) {
  Cycle* pProgram = gpThermocycler->GetCyclePool().AllocateComponent();
  pProgram->SetNumCycles(1);
	
  char* pCycBuf = strtok(pBuffer, "()");
  while (pCycBuf != NULL) {
    pProgram->AddComponent(ParseCycle(pCycBuf));
    pCycBuf = strtok(NULL, "()");
  }
  
  return pProgram;
}

ProgramComponent* CommandParser::ParseCycle(char* pBuffer) {
  char countBuf[5];
	
  //find first step
  char* pStep = strchr(pBuffer, '[');
	
  //get cycle count
  int countLen = pStep - pBuffer;
  strncpy(countBuf, pBuffer, countLen);
  countBuf[countLen] = '\0';
  int cycCount = atoi(countBuf);
  
  Cycle* pCycle = gpThermocycler->GetCyclePool().AllocateComponent();
  pCycle->SetNumCycles(cycCount);
	
  //add steps
  while (pStep != NULL) {
    *pStep++ = '\0';
    char* pStepEnd = strchr(pStep, ']');
    *pStepEnd++ = '\0';

    Step* pNewStep = ParseStep(pStep);
    pCycle->AddComponent(pNewStep);
    pStep = strchr(pStepEnd, '[');
  }

  return pCycle;
}

Step* CommandParser::ParseStep(char* pBuffer) {
  char* pTemp = strchr(pBuffer, '|');
  *pTemp++ = '\0';
  char* pName = strchr(pTemp, '|');
  *pName++ = '\0';
  char* pEnd = strchr(pName, ']');
  *pEnd = '\0';
	
  int duration = atoi(pBuffer);
  float temp = atof(pTemp);

  Step* pStep = gpThermocycler->GetStepPool().AllocateComponent();
  
  pStep->SetName(pName);
  pStep->SetDuration(duration);
  pStep->SetTemp(temp);
  return pStep;
}


////////////////////////////////////////////////////////////////////
// Class ProgramStore
//
// Note: Byte 0 of EEPROM is used for contrast
//       Bytes 1 and onwards are used for stored program string
//
uint8_t ProgramStore::RetrieveContrast() {
  return EEPROM.read(0);
}

#define PROG_START_STR "&c=start"
const char PROG_START_STR_P[] PROGMEM = PROG_START_STR;
boolean ProgramStore::RetrieveProgram(SCommand& command, char* pBuffer) {
  for (int i = 0; i < MAX_COMMAND_SIZE; i++)
    pBuffer[i] = EEPROM.read(i + 1);
  
  if (strncmp_P(pBuffer, PROG_START_STR_P, strlen(PROG_START_STR)) == 0) {
    //previous program stored
    CommandParser::ParseCommand(command, pBuffer);   
    return true;
    
  } else {
    return false;
  }
}



void ProgramStore::StoreContrast(uint8_t contrast) {
  EEPROM.write(0, contrast);
}

void ProgramStore::StoreProgram(const char* szProgram) {
  for (int i = 0; i < MAX_COMMAND_SIZE; i++)
    EEPROM.write(i + 1, szProgram[i]);
}