Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
en:tinkering_kit_light_level_meter_with_driver [2015/12/02 17:02] brian.kim [Introduction] |
en:tinkering_kit_light_level_meter_with_driver [2017/04/01 08:29] odroid [Python example] |
||
---|---|---|---|
Line 23: | Line 23: | ||
[[http://www.hardkernel.com/main/products/prdt_info.php?g_code=G141637532784|Where to buy]] | [[http://www.hardkernel.com/main/products/prdt_info.php?g_code=G141637532784|Where to buy]] | ||
- | ====== Schematic ====== | + | ====== DIY light level meter project ====== |
+ | ==== Linux ==== | ||
+ | 1. Configuration tinkering kit such as below schematic.\\ | ||
+ | Light Level Meter schematic | ||
{{:en:tinkeringkitdriverleds.png?800|}} | {{:en:tinkeringkitdriverleds.png?800|}} | ||
+ | |||
+ | 2. Get the wiringPi library compatible **ODROID** | ||
+ | <code> | ||
+ | # git clone https://github.com/hardkernel/wiringPi | ||
+ | </code> | ||
+ | |||
+ | 3. Build the library | ||
+ | <code> | ||
+ | # cd wiringPi | ||
+ | # ./build | ||
+ | </code> | ||
+ | |||
+ | 4. Compile and run the example source code. | ||
+ | |||
+ | <file c example-led.c> | ||
+ | //------------------------------------------------------------------------------------------------------------ | ||
+ | // | ||
+ | // ODROID-C GPIO(LED) / ADC Test Application. | ||
+ | // | ||
+ | // Defined port number is wiringPi port number. | ||
+ | // | ||
+ | // Compile : gcc -o <create excute file name> <source file name> -lwiringPi -lwiringPiDev -lpthread | ||
+ | // Run : sudo ./<created excute file name> | ||
+ | // | ||
+ | //------------------------------------------------------------------------------------------------------------ | ||
+ | #include <stdio.h> | ||
+ | #include <stdlib.h> | ||
+ | #include <stdint.h> | ||
+ | |||
+ | #include <unistd.h> | ||
+ | #include <string.h> | ||
+ | #include <time.h> | ||
+ | |||
+ | #include <wiringPi.h> | ||
+ | #include <wiringPiI2C.h> | ||
+ | #include <wiringSerial.h> | ||
+ | #include <lcd.h> | ||
+ | |||
+ | //------------------------------------------------------------------------------------------------------------ | ||
+ | // | ||
+ | // Global handle Define | ||
+ | // | ||
+ | //------------------------------------------------------------------------------------------------------------ | ||
+ | |||
+ | #define DATA_UPDATE_PERIOD 100 // 100ms | ||
+ | |||
+ | //------------------------------------------------------------------------------------------------------------ | ||
+ | // | ||
+ | // ADC: | ||
+ | // | ||
+ | //------------------------------------------------------------------------------------------------------------ | ||
+ | #define PORT_ADC 0 // ADC.AIN0 | ||
+ | #define MAX_ADC_VALUE 4095 | ||
+ | |||
+ | //------------------------------------------------------------------------------------------------------------ | ||
+ | // | ||
+ | // LED: | ||
+ | // | ||
+ | //------------------------------------------------------------------------------------------------------------ | ||
+ | const int ledPorts[] = { | ||
+ | 7, | ||
+ | 0, | ||
+ | 2, | ||
+ | 3, | ||
+ | 12, | ||
+ | 13, | ||
+ | 14, | ||
+ | 21, | ||
+ | 22, | ||
+ | 23, | ||
+ | }; | ||
+ | |||
+ | #define MAX_LED_CNT (sizeof(ledPorts) / sizeof(ledPorts[0])) | ||
+ | #define ADC_UNIT (MAX_ADC_VALUE / MAX_LED_CNT) | ||
+ | |||
+ | //------------------------------------------------------------------------------------------------------------ | ||
+ | //------------------------------------------------------------------------------------------------------------ | ||
+ | // | ||
+ | // system init | ||
+ | // | ||
+ | //------------------------------------------------------------------------------------------------------------ | ||
+ | int system_init(void) | ||
+ | { | ||
+ | int i; | ||
+ | |||
+ | // GPIO Init(LED Port ALL Output) | ||
+ | for(i = 0; i < MAX_LED_CNT; i++) pinMode (ledPorts[i], OUTPUT); | ||
+ | |||
+ | return 0; | ||
+ | } | ||
+ | |||
+ | //------------------------------------------------------------------------------------------------------------ | ||
+ | // | ||
+ | // board data update | ||
+ | // | ||
+ | //------------------------------------------------------------------------------------------------------------ | ||
+ | void boardDataUpdate(void) | ||
+ | { | ||
+ | int i, adcValue, ledPos; | ||
+ | |||
+ | // adc value read | ||
+ | if((adcValue = analogRead (PORT_ADC))) { | ||
+ | ledPos = adcValue / ADC_UNIT; | ||
+ | } | ||
+ | else | ||
+ | ledPos = 0; | ||
+ | |||
+ | // LED Control | ||
+ | for(i = 0; i < MAX_LED_CNT; i++) digitalWrite (ledPorts[i], 0); // LED All Clear | ||
+ | for(i = 0; i < ledPos; i++) digitalWrite (ledPorts[i], 1); // LED On | ||
+ | } | ||
+ | |||
+ | //------------------------------------------------------------------------------------------------------------ | ||
+ | // | ||
+ | // Start Program | ||
+ | // | ||
+ | //------------------------------------------------------------------------------------------------------------ | ||
+ | int main (int argc, char *argv[]) | ||
+ | { | ||
+ | static int timer = 0 ; | ||
+ | |||
+ | wiringPiSetup (); | ||
+ | |||
+ | if (system_init() < 0) | ||
+ | { | ||
+ | fprintf (stderr, "%s: System Init failed\n", __func__); return -1; | ||
+ | } | ||
+ | |||
+ | for(;;) { | ||
+ | usleep(100000); | ||
+ | if (millis () < timer) continue ; | ||
+ | |||
+ | timer = millis () + DATA_UPDATE_PERIOD; | ||
+ | |||
+ | // All Data update | ||
+ | boardDataUpdate(); | ||
+ | } | ||
+ | |||
+ | return 0 ; | ||
+ | } | ||
+ | </file> | ||
+ | |||
+ | ====== Python example ====== | ||
+ | * [[https://github.com/hardkernel/WiringPi2-Python|WiringPi2-Python repository for ODROID]] | ||
+ | 0. Requrements install | ||
+ | <code> | ||
+ | # sudo apt update | ||
+ | # sudo apt install python-dev python-setuptools swig3.0 | ||
+ | </code> | ||
+ | |||
+ | 1. Get/setup WiringPi 2 for Python repository | ||
+ | <code> | ||
+ | # git clone https://github.com/hardkernel/WiringPi2-Python.git | ||
+ | # cd WiringPi2-Python | ||
+ | # git submodule init | ||
+ | # git submodule update | ||
+ | </code> | ||
+ | |||
+ | 2. Build & install | ||
+ | <code> | ||
+ | # ./build.sh | ||
+ | </code> | ||
+ | Or | ||
+ | <code> | ||
+ | # swig3.0 -python -threads wiringpi.i | ||
+ | # sudo python setup.py build install | ||
+ | </code> | ||
+ | |||
+ | |||
+ | **[[http://odroid.com/dokuwiki/doku.php?id=en:xu3_hardware_gpio#gpio_map_for_wiringpi_library_con10_2_x_15|WiringPi Pin Map]]** | ||
+ | |||
+ | |||
+ | 3. Run the example source code | ||
+ | <file python example-led.py> | ||
+ | #!/usr/bin/python | ||
+ | import wiringpi2 as wpi | ||
+ | import time | ||
+ | |||
+ | leds = [7, 0, 2, 3, 12, 13, 14, 21, 22, 23] | ||
+ | wpi.wiringPiSetup() | ||
+ | |||
+ | # GPOI pin setup | ||
+ | for x in leds: | ||
+ | wpi.pinMode(x, 1) | ||
+ | wpi.digitalWrite(x, 0) | ||
+ | |||
+ | adc_unit = 4095 / len(leds) | ||
+ | while True: | ||
+ | time.sleep(0.05) | ||
+ | adcValue = wpi.analogRead(0) | ||
+ | ledPos = adcValue / adc_unit | ||
+ | for x in leds: | ||
+ | wpi.digitalWrite(x, 0) | ||
+ | |||
+ | for x in xrange(ledPos): | ||
+ | wpi.digitalWrite(leds[x], 1) | ||
+ | </file> | ||
+ |