Skip to content

Commit

Permalink
Merge commit '61266652bd61ae0918f1df5bd6c54a4a23b5e349'
Browse files Browse the repository at this point in the history
  • Loading branch information
pbosetti committed Jun 9, 2021
2 parents 834d91d + 6126665 commit 0910d40
Show file tree
Hide file tree
Showing 8 changed files with 2,251 additions and 68 deletions.
147 changes: 80 additions & 67 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,26 @@ project(Mightex1304 VERSION ${GIT_VERSION_TAG} LANGUAGES C)
# set(CMAKE_BUILD_TYPE Release)
message(STATUS "CMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}")

# Libusb
set(LIBUSB_VERSION 1.0.24)
if (CMAKE_BUILD_TYPE STREQUAL "Release")
set(LIBUSB_NO_DEBUG --disable-log)
if(NOT WIN32)
# Libusb
set(LIBUSB_VERSION 1.0.24)
if (CMAKE_BUILD_TYPE STREQUAL "Release")
set(LIBUSB_NO_DEBUG --disable-log)
endif()
include(ExternalProject)
ExternalProject_Add(libusb_prj
URL https://github.com/libusb/libusb/releases/download/v${LIBUSB_VERSION}/libusb-${LIBUSB_VERSION}.tar.bz2
PREFIX ${CMAKE_SOURCE_DIR}/vendor
BUILD_IN_SOURCE TRUE
CONFIGURE_COMMAND ./configure --prefix=${CMAKE_SOURCE_DIR}/vendor --enable-udev=no --with-pic -enable-shared=no ${LIBUSB_NO_DEBUG}
BUILD_COMMAND make
INSTALL_COMMAND make install
)
ExternalProject_Get_Property(libusb_prj INSTALL_DIR)
message(STATUS "Installing libusb in ${INSTALL_DIR}")
add_library(libusb STATIC IMPORTED)
set_property(TARGET libusb PROPERTY IMPORTED_LOCATION ${INSTALL_DIR}/lib/libusb-1.0.a)
endif()
include(ExternalProject)
ExternalProject_Add(libusb_prj
URL https://github.com/libusb/libusb/releases/download/v${LIBUSB_VERSION}/libusb-${LIBUSB_VERSION}.tar.bz2
PREFIX ${CMAKE_SOURCE_DIR}/vendor
BUILD_IN_SOURCE TRUE
CONFIGURE_COMMAND ./configure --prefix=${CMAKE_SOURCE_DIR}/vendor --enable-udev=no --with-pic -enable-shared=no ${LIBUSB_NO_DEBUG}
BUILD_COMMAND make
INSTALL_COMMAND make install
)
ExternalProject_Get_Property(libusb_prj INSTALL_DIR)
message(STATUS "Installing libusb in ${INSTALL_DIR}")
add_library(libusb STATIC IMPORTED)
set_property(TARGET libusb PROPERTY IMPORTED_LOCATION ${INSTALL_DIR}/lib/libusb-1.0.a)

# _____ _____
# / ____| / ____|
Expand All @@ -57,54 +59,62 @@ set_property(TARGET libusb PROPERTY IMPORTED_LOCATION ${INSTALL_DIR}/lib/libusb-
# | |
# |_|
# Find out target platform
execute_process(COMMAND ${CMAKE_C_COMPILER} -dumpmachine OUTPUT_VARIABLE TARGET_PLATFORM) #put into TARGET_PLATFORM result of $ gcc -dumpmachine
string(REPLACE "\n" "" TARGET_PLATFORM ${TARGET_PLATFORM})
string(REGEX MATCH mipsel-openwrt-linux-musl IS_MIPSEL ${TARGET_PLATFORM})
string(REGEX MATCH armv7-unknown-linux-gnueabi IS_ARM7 ${TARGET_PLATFORM})
string(REGEX MATCH arm-linux-gnueabihf IS_ARM6 ${TARGET_PLATFORM})
string(REGEX MATCH arm-cortexa8_neon-linux-gnueabihf IS_ARM7A ${TARGET_PLATFORM})
string(REGEX MATCH aarch64-unknown-linux-gnueabi IS_ARM64 ${TARGET_PLATFORM})
if (IS_ARM6 OR IS_ARM7 OR IS_ARM7A OR IS_ARM64)
set(IS_ARM TRUE)
endif()
if (NOT WIN32)
execute_process(COMMAND ${CMAKE_C_COMPILER} -dumpmachine OUTPUT_VARIABLE TARGET_PLATFORM) #put into TARGET_PLATFORM result of $ gcc -dumpmachine
string(REPLACE "\n" "" TARGET_PLATFORM ${TARGET_PLATFORM})
string(REGEX MATCH mipsel-openwrt-linux-musl IS_MIPSEL ${TARGET_PLATFORM})
string(REGEX MATCH armv7-unknown-linux-gnueabi IS_ARM7 ${TARGET_PLATFORM})
string(REGEX MATCH arm-linux-gnueabihf IS_ARM6 ${TARGET_PLATFORM})
string(REGEX MATCH arm-cortexa8_neon-linux-gnueabihf IS_ARM7A ${TARGET_PLATFORM})
string(REGEX MATCH aarch64-unknown-linux-gnueabi IS_ARM64 ${TARGET_PLATFORM})
if (IS_ARM6 OR IS_ARM7 OR IS_ARM7A OR IS_ARM64)
set(IS_ARM TRUE)
endif()

# SETUP PATHS AND VARIABLES
if(IS_MIPSEL)
message(STATUS "Cross-building for MIPSEL (cross_root: ${cross_root})")
set(LUA_LIB lua)
add_definitions(-D_OPENWRT_LINUX=1)
include_directories(${cross_root}/include)
link_directories(${cross_root}/lib)
elseif(IS_ARM)
message(STATUS "Cross-building for ARM (cross_root: ${cross_root})")
set(LINUX TRUE)
set(LUA_LIB lua)
include_directories(${cross_root}/include ${cross_root}/include/libusb-1.0)
link_directories(${cross_root}/lib)
add_definitions(-D__ARM7__)
elseif(UNIX AND NOT APPLE)
message(STATUS "Building on Linux")
set(LINUX TRUE)
set(NATIVE TRUE)
set(LUA_LIB lua5.3)
include_directories(/usr/include/${LUA_LIB} /usr/local/include ${CMAKE_SOURCE_DIR}/vendor/include)
link_directories(/usr/lib/x86_64-linux-gnu ${CMAKE_SOURCE_DIR}/vendor/lib)
elseif(APPLE)
message(STATUS "Building on Apple OS X")
set(LUA_LIB lua)
set(NATIVE TRUE)
include_directories(/usr/local/include /usr/local/include/lua ${CMAKE_SOURCE_DIR}/vendor/include)
link_directories(${CMAKE_CURRENT_LIST_DIR}/vendor/lib)
set(FRAMEWORKS "-framework CoreFoundation -framework IOKit")
endif()
# SETUP PATHS AND VARIABLES
if(IS_MIPSEL)
message(STATUS "Cross-building for MIPSEL (cross_root: ${cross_root})")
set(LUA_LIB lua)
add_definitions(-D_OPENWRT_LINUX=1)
include_directories(${cross_root}/include)
link_directories(${cross_root}/lib)
elseif(IS_ARM)
message(STATUS "Cross-building for ARM (cross_root: ${cross_root})")
set(LINUX TRUE)
set(LUA_LIB lua)
include_directories(${cross_root}/include ${cross_root}/include/libusb-1.0)
link_directories(${cross_root}/lib)
add_definitions(-D__ARM7__)
elseif(UNIX AND NOT APPLE)
message(STATUS "Building on Linux")
set(LINUX TRUE)
set(NATIVE TRUE)
set(LUA_LIB lua5.3)
include_directories(/usr/include/${LUA_LIB} /usr/local/include ${CMAKE_SOURCE_DIR}/vendor/include)
link_directories(/usr/lib/x86_64-linux-gnu ${CMAKE_SOURCE_DIR}/vendor/lib)
elseif(APPLE)
message(STATUS "Building on Apple OS X")
set(LUA_LIB lua)
set(NATIVE TRUE)
include_directories(/usr/local/include /usr/local/include/lua ${CMAKE_SOURCE_DIR}/vendor/include)
link_directories(${CMAKE_CURRENT_LIST_DIR}/vendor/lib)
set(FRAMEWORKS "-framework CoreFoundation -framework IOKit")
endif()

# COMPILE OPTIONS
add_compile_options(-std=gnu11 -fPIC -D_GNU_SOURCE)
if(CMAKE_BUILD_TYPE MATCHES "Debug")
message(STATUS "Debug mode, enabling all warnings")
add_compile_options(-Wall -Wno-comment)
# COMPILE OPTIONS
add_compile_options(-std=gnu11 -fPIC -D_GNU_SOURCE)
if(CMAKE_BUILD_TYPE MATCHES "Debug")
message(STATUS "Debug mode, enabling all warnings")
add_compile_options(-Wall -Wno-comment)
endif()
set(LIBUSB_NAME libusb)
else() #WIN32
include_directories(${CMAKE_SOURCE_DIR}/win/include)
link_directories(${CMAKE_SOURCE_DIR}/win/lib)
set(LIBUSB_NAME libusb-1.0.lib)
set(CMAKE_PLATFORM "Windows")
set(NATIVE TRUE)
endif()

# _____ _ _ _____ _ _ _
# | __ \ (_) | | | __ \ | | (_) |
# | |__) | __ ___ _ ___ ___| |_ | | | | ___| |_ __ _ _| |___
Expand All @@ -127,10 +137,10 @@ configure_file(

# TARGETS
add_library(mightex_static STATIC ${LIB_SOURCES})
target_link_libraries(mightex_static libusb ${FRAMEWORKS})
target_link_libraries(mightex_static ${LIBUSB_NAME} ${FRAMEWORKS})

add_library(mightex_shared SHARED ${LIB_SOURCES})
target_link_libraries(mightex_shared libusb ${FRAMEWORKS})
target_link_libraries(mightex_shared ${LIBUSB_NAME} ${FRAMEWORKS})
set_target_properties(mightex_shared PROPERTIES PREFIX "lib" LIBRARY_OUTPUT_NAME mightex SUFFIX ".so")
set_target_properties(mightex_shared PROPERTIES PUBLIC_HEADER "${HEADERS}")

Expand All @@ -140,15 +150,18 @@ target_link_libraries(test mightex_static)

add_executable(listusb ${SOURCE_DIR}/main/listusb.c)
target_link_options(listusb BEFORE PUBLIC -pthread)
target_link_libraries(listusb libusb ${FRAMEWORKS})
target_link_libraries(listusb ${LIBUSB_NAME} ${FRAMEWORKS})

add_dependencies(mightex_static libusb_prj)
if (NOT WIN32)
add_dependencies(mightex_static libusb_prj)
endif()

list(APPEND TARGETS_LIST
test listusb
mightex_static mightex_shared
)


# _____ _ _ _
# |_ _| | | | | |
# | | _ __ ___| |_ __ _| | |
Expand Down Expand Up @@ -212,7 +225,7 @@ include(CPack)
# |_____/ \___/ \___|_|\_\___|_| |_| |_|_|\___|
#
# if not cross-compiling, create Dockerfile for the target platform
if(NATIVE)
if(NATIVE AND NOT WIN32)
option(ENABLE_MRUBY "Enable build of mruby in container" OFF)
if(NOT ENABLE_MRUBY)
message(STATUS "mruby is NOT compiled in the container")
Expand Down
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ At the moment, Linux and OS X. Given that `libusb` is also available on Windows,

## Build

### Linux and OS X

Cmake style:

```sh
Expand All @@ -30,3 +32,20 @@ and you will find the compiled files under `products_host` and Doxygen documenta

The project is ready for an easy cross-compilation. See README-xcomp.md for details.

### Windows

On Windows, things are a tad more complicated. For starters you need to install:

1. Visual Studio 2019 (Community edition is fine), with Windows C++ SDK.
2. Git
3. CMake
4. **Most importantly** do not use the driver provided by Mightex, it won't work. Instead, you have to use the WinUSB driver: the easiest way to install it is by using the [Zadig tool](https://zadig.akeo.ie/).

Then you shall be able to do:

```sh
cmake -Bbuild -S. -DBUILD_DOC=no -DCMAKE_BUILD_TYPE=Release
cmake --build build -j4 -t install
```

and you'll find all the goodies in `products_host`
2 changes: 2 additions & 0 deletions src/defines.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@

#ifndef DEFINES_H
#define DEFINES_H
#ifndef _WIN32
#include <sys/param.h>
#endif
#ifdef __linux
#include <inttypes.h>
#endif
Expand Down
9 changes: 9 additions & 0 deletions src/main/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@

#include <stdio.h>
#include <stdlib.h>
#ifdef _WIN32
#include <stdint.h>
#else
#include <unistd.h>
#endif // _WIN32

#include "../mightex1304.h"

int main(void) {
Expand All @@ -23,7 +28,11 @@ int main(void) {
}

while ((n = mightex_get_buffer_count(m)) <= 0) {
#ifdef _WIN32
Sleep(10);
#else
usleep(10000);
#endif
}
fprintf(stderr, "Frame count: %d\n", n);
int i;
Expand Down
23 changes: 22 additions & 1 deletion src/mightex1304.c
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
#include "mightex1304.h"
#ifdef _WIN32
#pragma comment(lib, "Ws2_32.lib")
#include <winsock.h>
#else
#include <arpa/inet.h>
#endif
#include <assert.h>
#include <libusb-1.0/libusb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef _WIN32
#include <stdint.h>
#else
#include <unistd.h>
#endif // _WIN32

#define USB_IDVENDOR 0x04B4
#define USB_IDPRODUCT 0x0328
Expand All @@ -26,7 +35,11 @@
#define STRING_LENGTH 14

typedef union {
#ifdef _WIN32
struct di {
#else
struct __attribute__((__packed__)) di {
#endif
BYTE rc;
BYTE len;
BYTE config_revision;
Expand All @@ -38,7 +51,11 @@ typedef union {
} device_info_t;

typedef union {
#ifdef _WIN32
struct ver {
#else
struct __attribute__((__packed__)) ver {
#endif
BYTE rc;
BYTE len;
BYTE major, minor, rev;
Expand All @@ -47,7 +64,11 @@ typedef union {
} device_version_t;

typedef union {
struct __attribute__((__packed__)) frame {
#ifdef _WIN32
struct frame {
#else
struct __attribute__((__packed__)) frame {
#endif
uint16_t _dummy1[16];
uint16_t light_shield[13];
uint16_t _reserved[3];
Expand Down
6 changes: 6 additions & 0 deletions src/mightex1304.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
*
*/
#include <stdlib.h>
#ifdef _WIN32
#include <stdint.h>
#include <Windows.h>
#else
#include <unistd.h>
#endif // _WIN32
#include "defines.h"

/**
Expand Down
Loading

0 comments on commit 0910d40

Please sign in to comment.