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

Feature/benchmarks #32

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@
.externalNativeBuild
.tags*
.cxx/
linux/build/
33 changes: 33 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "GDB Debug JpegBenchmark",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/build/jpegbenchmark",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "CMakeBuild",
"logging": {
"engineLogging": true
},
"targetArchitecture": "x86_64",
"launchCompleteCommand": "exec-run"
}
]
}
67 changes: 67 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
{
"files.associations": {
"cstdint": "cpp",
"ostream": "cpp",
"*.tcc": "cpp",
"cstdlib": "cpp",
"mutex": "cpp",
"format": "cpp",
"array": "cpp",
"string_view": "cpp",
"initializer_list": "cpp",
"span": "cpp",
"utility": "cpp",
"atomic": "cpp",
"bit": "cpp",
"cctype": "cpp",
"charconv": "cpp",
"chrono": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"compare": "cpp",
"concepts": "cpp",
"condition_variable": "cpp",
"cstdarg": "cpp",
"cstddef": "cpp",
"cstdio": "cpp",
"ctime": "cpp",
"cwchar": "cpp",
"cwctype": "cpp",
"deque": "cpp",
"list": "cpp",
"map": "cpp",
"string": "cpp",
"unordered_map": "cpp",
"vector": "cpp",
"exception": "cpp",
"algorithm": "cpp",
"functional": "cpp",
"iterator": "cpp",
"memory": "cpp",
"memory_resource": "cpp",
"numeric": "cpp",
"optional": "cpp",
"random": "cpp",
"ratio": "cpp",
"system_error": "cpp",
"tuple": "cpp",
"type_traits": "cpp",
"iomanip": "cpp",
"iosfwd": "cpp",
"iostream": "cpp",
"istream": "cpp",
"limits": "cpp",
"new": "cpp",
"numbers": "cpp",
"semaphore": "cpp",
"sstream": "cpp",
"stdexcept": "cpp",
"stop_token": "cpp",
"streambuf": "cpp",
"thread": "cpp",
"cinttypes": "cpp",
"typeinfo": "cpp",
"variant": "cpp"
},
"cmake.sourceDirectory": "${workspaceFolder}/linux/"
}
38 changes: 38 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"tasks": [
{
"label": "CMakePrepare",
"type": "shell",
"command": "cmake",
"args": [
"-B",
"${workspaceFolder}/build",
"-S",
"${workspaceFolder}/linux/",
"-DCMAKE_BUILD_TYPE=Debug"
],
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": ["$gcc"],
"detail": "Generated task by Debugger."
},
{
"label": "CMakeBuild",
"type": "shell",
"command": "cmake",
"args": [
"--build",
"${workspaceFolder}/build"
],
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": ["$gcc"],
"detail": "Generated task by Debugger."
}
],
"version": "2.0.0"
}
73 changes: 73 additions & 0 deletions AndroidUSBCamera.code-workspace
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
{
"folders": [
{
"path": "."
}
],
"settings": {
"files.associations": {
"cstdint": "cpp",
"ostream": "cpp",
"*.tcc": "cpp",
"cstdlib": "cpp",
"mutex": "cpp",
"format": "cpp",
"array": "cpp",
"string_view": "cpp",
"initializer_list": "cpp",
"span": "cpp",
"utility": "cpp",
"atomic": "cpp",
"bit": "cpp",
"cctype": "cpp",
"charconv": "cpp",
"chrono": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"compare": "cpp",
"concepts": "cpp",
"condition_variable": "cpp",
"cstdarg": "cpp",
"cstddef": "cpp",
"cstdio": "cpp",
"ctime": "cpp",
"cwchar": "cpp",
"cwctype": "cpp",
"deque": "cpp",
"list": "cpp",
"map": "cpp",
"string": "cpp",
"unordered_map": "cpp",
"vector": "cpp",
"exception": "cpp",
"algorithm": "cpp",
"functional": "cpp",
"iterator": "cpp",
"memory": "cpp",
"memory_resource": "cpp",
"numeric": "cpp",
"optional": "cpp",
"random": "cpp",
"ratio": "cpp",
"system_error": "cpp",
"tuple": "cpp",
"type_traits": "cpp",
"iomanip": "cpp",
"iosfwd": "cpp",
"iostream": "cpp",
"istream": "cpp",
"limits": "cpp",
"new": "cpp",
"numbers": "cpp",
"semaphore": "cpp",
"sstream": "cpp",
"stdexcept": "cpp",
"stop_token": "cpp",
"streambuf": "cpp",
"thread": "cpp",
"cinttypes": "cpp",
"typeinfo": "cpp",
"variant": "cpp"
}
}
}
18 changes: 14 additions & 4 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-feature android:name="android.hardware.camera"/>
<uses-feature android:name="android.hardware.camera.autofocus"/>
<uses-feature android:name="android.hardware.camera.autofocus"
android:required="false" />

<application
android:name="com.jiangdg.demo.DemoApplication"
Expand All @@ -18,16 +17,27 @@
android:theme="@style/Theme.ClinkCameraDemo"
android:requestLegacyExternalStorage="true"
android:supportsRtl="true">

<meta-data
android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
android:resource="@xml/usb_device_filter"/>

<activity
android:name="com.vsh.activity.DevicesActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity android:name="com.vsh.activity.TestUsbBulkActivity"
android:exported="true">
<intent-filter>
<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"/>
</intent-filter>
</activity>

<activity
android:name="com.jiangdg.demo.MainActivity"
android:theme="@style/Theme.ClinkCameraDemo.NoActionBar" >
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/assets/jpeg_samples/sample1_1440.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/assets/jpeg_samples/sample1_2160.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/assets/jpeg_samples/sample2_0360.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/assets/jpeg_samples/sample2_2160.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/assets/jpeg_samples/sample3_2160.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 7 additions & 2 deletions app/src/main/java/com/vsh/activity/DevicesActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ import android.hardware.usb.UsbManager
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.toArgb
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import com.example.cupcake.ui.theme.AusbcTheme
import com.jiangdg.demo.MainActivity
import com.vsh.screens.DeviceListScreen
import com.vsh.screens.AusbcApp
import com.vsh.screens.DeviceListViewModel
import com.vsh.screens.DeviceListViewModelFactory
import kotlinx.coroutines.launch
Expand All @@ -35,6 +37,7 @@ class DevicesActivity : ComponentActivity() {
lateinit var viewModel: DeviceListViewModel

override fun onCreate(savedInstanceState: Bundle?) {
enableEdgeToEdge()
super.onCreate(savedInstanceState)
getWindow().getDecorView().setBackgroundColor(Color.White.toArgb())
viewModel = ViewModelProvider(
Expand All @@ -43,7 +46,9 @@ class DevicesActivity : ComponentActivity() {
)
).get(DeviceListViewModel::class.java)
setContent {
DeviceListScreen.ScreenContent(viewModel = viewModel)
AusbcTheme {
AusbcApp(viewModel = viewModel)
}
}
}

Expand Down
92 changes: 92 additions & 0 deletions app/src/main/java/com/vsh/activity/TestUsbBulkActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package com.vsh.activity

import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.hardware.usb.UsbDevice
import android.hardware.usb.UsbDeviceConnection
import android.hardware.usb.UsbEndpoint
import android.hardware.usb.UsbManager
import android.os.Bundle
import androidx.activity.ComponentActivity
import java.nio.charset.Charset

class UsbHelper(private val context: Context) {
private val usbManager = context.getSystemService(Context.USB_SERVICE) as UsbManager
private val ACTION_USB_PERMISSION = "com.example.USB_PERMISSION"

fun findAndConnectDevice(): Pair<UsbDeviceConnection, UsbDevice>? {
val deviceList = usbManager.deviceList
val device = deviceList.values.find { it.vendorId == 0x0000 && it.productId == 0x0001 }

if (device == null) {
println("Device not found")
return null
}

val permissionIntent = PendingIntent.getBroadcast(
context, 0, Intent(ACTION_USB_PERMISSION), PendingIntent.FLAG_IMMUTABLE
)

usbManager.requestPermission(device, permissionIntent)

if (!usbManager.hasPermission(device)) {
println("No permission to access USB device")
return null
}

val usbDeviceConnection = usbManager.openDevice(device) ?: return null
val interfaceUsb = device.getInterface(0)

val endpointOut = interfaceUsb.getEndpoint(0) // OUT
val endpointIn = interfaceUsb.getEndpoint(1) // IN

usbDeviceConnection.claimInterface(interfaceUsb, true)

println("Device connected: ${device.deviceName}")
return Pair(usbDeviceConnection, device)
}

fun sendData(connection: UsbDeviceConnection, endpointOut: UsbEndpoint, data: String) {
val bytes = data.toByteArray(Charset.defaultCharset())
val result = connection.bulkTransfer(endpointOut, bytes, bytes.size, 1000)
if (result > 0) {
println("Sent: $data")
} else {
println("Failed to send data")
}
}

fun receiveData(connection: UsbDeviceConnection, endpointIn: UsbEndpoint, bufferSize: Int = 64): String {
val buffer = ByteArray(bufferSize)
val received = connection.bulkTransfer(endpointIn, buffer, buffer.size, 1000)
return if (received > 0) {
String(buffer, 0, received, Charset.defaultCharset())
} else {
"No data received"
}
}
}

class TestUsbBulkActivity: ComponentActivity() {
private lateinit var usbHelper: UsbHelper

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

usbHelper = UsbHelper(this)

val pair = usbHelper.findAndConnectDevice()
if (pair != null) {
val device = pair.second
val connection = pair.first
val interfaceUsb = device.getInterface(0)
val endpointOut = interfaceUsb.getEndpoint(0)
val endpointIn = interfaceUsb.getEndpoint(1)

usbHelper.sendData(connection, endpointOut, "Hello USB!")
val response = usbHelper.receiveData(connection, endpointIn)
println("Received: $response")
}
}
}
Loading