blob: d79ef9c4f14fb71bbfe1ef87407de702c7ea8eda [file] [log] [blame]
jerrymf1579332013-02-07 01:56:28 +00001/*----------------------------------------------------------------------------*/
2/* Copyright (c) FIRST 2008. All Rights Reserved. */
3/* Open Source Software - may be modified and shared by FRC teams. The code */
4/* must be accompanied by the FIRST BSD license file in $(WIND_BASE)/WPILib. */
5/*----------------------------------------------------------------------------*/
6
7#include "IntCameraParameter.h"
8#include "pcre.h"
9#include <stdio.h>
10#include <string.h>
11
12/**
13 * Constructor for an integer camera parameter.
14 * @param setString The string to set a value in the HTTP request
15 * @param getString The string to retrieve a value in the HTTP request
16 */
17IntCameraParameter::IntCameraParameter(const char *setString, const char *getString, bool requiresRestart)
18{
19 m_changed = false;
20 m_value = 0;
21 m_setString = setString;
22 m_getString = getString;
23 m_requiresRestart = requiresRestart;
24}
25
26/**
27 * Get a value for a camera parameter.
28 * @returns The camera parameter cached valued.
29 */
30int IntCameraParameter::GetValue()
31{
32 return m_value;
33}
34
35/**
36 * Set a value for a camera parameter.
37 * Mark the value for change. The value will be updated in the parameter
38 * change loop.
39 */
40void IntCameraParameter::SetValue(int value)
41{
42 m_value = value;
43 m_changed = true;
44}
45
46/**
47 * Check if a parameter has changed and update.
48 * Check if a parameter has changed and send the update string if it
49 * has changed. This is called from the loop in the parameter task loop.
50 * @returns true if the camera needs to restart
51 */
52bool IntCameraParameter::CheckChanged(bool &changed, char *param)
53{
54 changed = m_changed;
55 if (m_changed)
56 {
57 sprintf(param, m_setString, m_value);
58 m_changed = false;
59 return m_requiresRestart;
60 }
61 return false;
62}
63
64/**
65 * Get a parameter value from the string.
66 * Get a parameter value from the camera status string. If it has been changed
67 * been changed by the program, then don't update it. Program values have
68 * precedence over those written in the camera.
69 */
70void IntCameraParameter::GetParamFromString(const char *string, int stringLength)
71{
72 char resultString[150];
73 if (SearchForParam(m_getString, string, stringLength, resultString) >= 0)
74 {
75 if (!m_changed) m_value = atoi(resultString);
76 }
77}
78
79/**
80 * @param pattern: the regular expression
81 * @param searchString the text to search
82 * @param searchStringLen the length of searchString
83 * @param result buffer to put resulting text into, must be pre-allocated
84 */
85int IntCameraParameter::SearchForParam(const char *pattern, const char *searchString, int searchStringLen, char *result)
86{
87 int vectorLen = 10;
88 int resultVector[vectorLen];
89 const char *error;
90 int erroffset;
91 pcre *compiledPattern = pcre_compile(
92 pattern, //"root.Image.I0.Appearance.Resolution=(.*)",
93 PCRE_CASELESS,
94 &error, // for error message
95 &erroffset, // for error offset
96 NULL); // use default character tables
97 int rc;
98 rc = pcre_exec(compiledPattern,
99 NULL,
100 searchString,
101 searchStringLen,
102 0,
103 0,
104 resultVector, //locations of submatches
105 vectorLen); //size of ovector
106 int length = resultVector[3] - resultVector[2];
107 memcpy(result, &searchString[resultVector[2]], length);
108 result[length] = '\0';
109 return rc;
110}
111