1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef EXTENSIONS_BROWSER_API_WEBCAM_PRIVATE_VISCA_WEBCAM_H_
6 #define EXTENSIONS_BROWSER_API_WEBCAM_PRIVATE_VISCA_WEBCAM_H_
10 #include "base/callback.h"
11 #include "base/memory/weak_ptr.h"
12 #include "extensions/browser/api/serial/serial_connection.h"
13 #include "extensions/browser/api/webcam_private/webcam.h"
14 #include "extensions/common/api/serial.h"
16 namespace extensions
{
18 class ViscaWebcam
: public Webcam
{
20 ViscaWebcam(const std::string
& path
, const std::string
& extension_id
);
21 ~ViscaWebcam() override
;
23 using OpenCompleteCallback
= base::Callback
<void(bool)>;
25 // Open and initialize the web camera. This is done by the following three
26 // steps (in order): 1. Open the serial port; 2. Request address; 3. Clear
27 // the command buffer. After these three steps completes, |open_callback| will
29 void Open(const OpenCompleteCallback
& open_callback
);
40 using CommandCompleteCallback
=
41 base::Callback
<void(bool, const std::vector
<char>&)>;
43 void OpenOnIOThread(const OpenCompleteCallback
& open_callback
);
44 // Callback function that will be called after the serial connection has been
45 // opened successfully.
46 void OnConnected(const OpenCompleteCallback
& open_callback
, bool success
);
47 // Callback function that will be called after the address has been set
49 void OnAddressSetCompleted(const OpenCompleteCallback
& open_callback
,
51 const std::vector
<char>& response
);
52 // Callback function that will be called after the command buffer has been
53 // cleared successfully.
54 void OnClearAllCompleted(const OpenCompleteCallback
& open_callback
,
56 const std::vector
<char>& response
);
58 // Send or queue a command and wait for the camera's response.
59 void Send(const std::vector
<char>& command
,
60 const CommandCompleteCallback
& callback
);
61 void OnSendCompleted(const CommandCompleteCallback
& callback
,
63 core_api::serial::SendError error
);
64 void ReceiveLoop(const CommandCompleteCallback
& callback
);
65 void OnReceiveCompleted(const CommandCompleteCallback
& callback
,
66 const std::vector
<char>& data
,
67 core_api::serial::ReceiveError error
);
68 // Callback function that will be called after the send and reply of a command
69 // are both completed. Update |value| according to |type| and |response| if
71 void OnCommandCompleted(CommandType type
,
74 const std::vector
<char>& response
);
77 void Reset(bool pan
, bool tilt
, bool zoom
) override
;
78 bool GetPan(int* value
) override
;
79 bool GetTilt(int* value
) override
;
80 bool GetZoom(int* value
) override
;
81 bool SetPan(int value
) override
;
82 bool SetTilt(int value
) override
;
83 bool SetZoom(int value
) override
;
84 bool SetPanDirection(PanDirection direction
) override
;
85 bool SetTiltDirection(TiltDirection direction
) override
;
87 const std::string path_
;
88 const std::string extension_id_
;
90 scoped_ptr
<SerialConnection
> serial_connection_
;
92 // Stores the response for the current command.
93 std::vector
<char> data_buffer_
;
94 // Queues commands till the current command completes.
95 std::deque
<std::pair
<std::vector
<char>, CommandCompleteCallback
>> commands_
;
97 // Visca webcam always get/set pan-tilt together. |pan| and |tilt| are used to
98 // store the current value of pan and tilt positions.
102 base::WeakPtrFactory
<ViscaWebcam
> weak_ptr_factory_
;
104 DISALLOW_COPY_AND_ASSIGN(ViscaWebcam
);
107 } // namespace extensions
109 #endif // EXTENSIONS_BROWSER_API_WEBCAM_PRIVATE_VISCA_WEBCAM_H_