Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add error handling for camera initialization. #2

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 30 additions & 7 deletions activity.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,18 +132,23 @@ def timer_cb(self, b: Gtk.ToggleButton):
#SECTION Camera operations
#==========================================================================
def start_camera_preview(self):
try:
#* Initialize Camera
self.picam2 = Picamera2()
if not hasattr(self, 'preview_config'):
self.preview_config = self.picam2.create_preview_configuration({
self.picam2 = Picamera2()
if not hasattr(self, 'preview_config'):
self.preview_config = self.picam2.create_preview_configuration({
'size': self._size,
'format': self._format},
transform=Transform(hflip=self._hflip, vflip=self._vflip))
self.picam2.configure(self.preview_config)
self.picam2.start()
self.picam2.configure(self.preview_config)
self.picam2.start()

# Update the preview continuously: 30ms
GLib.timeout_add(30, self.update_preview)
GLib.timeout_add(30, self.update_preview)

except Exception as e:
self.show_error_message("Failed to initialize the camera. Please check your connection.")
print(f"Error starting camera preview: {e}")

def update_config(self):
config = self.picam2.create_preview_configuration({
Expand All @@ -156,8 +161,26 @@ def update_config(self):
print('changed preview config')

def update_preview(self):
self.drawing_area.queue_draw()
if hasattr(self, 'picam2'):
try:
self.drawing_area.queue_draw()
except Exception as e:
self.show_error_message("Error updating preview.")
print(f"Error updating preview: {e}")
return True

def show_error_message(self, message):
# Create an error dialog
dialog = Gtk.MessageDialog(
parent=self,
flags=Gtk.DialogFlags.MODAL,
type=Gtk.MessageType.ERROR,
buttons=Gtk.ButtonsType.OK,
message_format=message
)
dialog.run()
dialog.destroy()
Comment on lines +172 to +182

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should use the sugar3.graphics.alert instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok thanks!



def calculate_stride_and_scale(self, width, height, widget):
stride = cairo.ImageSurface.format_stride_for_width(cairo.FORMAT_RGB24,
Expand Down