mirror of
				https://github.com/eclipse/upm.git
				synced 2025-10-31 07:04:14 +03:00 
			
		
		
		
	4digitdisplay: added new 4digitdisplay module
Signed-off-by: Yevgeniy Kiveisha <yevgeniy.kiveisha@intel.com>
This commit is contained in:
		
							
								
								
									
										42
									
								
								examples/4digitdisplay.cxx
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										42
									
								
								examples/4digitdisplay.cxx
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,42 @@ | ||||
| /* | ||||
|  * Author: Yevgeniy Kiveisha <yevgeniy.kiveisha@intel.com> | ||||
|  * Copyright (c) 2014 Intel Corporation. | ||||
|  * | ||||
|  * Permission is hereby granted, free of charge, to any person obtaining | ||||
|  * a copy of this software and associated documentation files (the | ||||
|  * "Software"), to deal in the Software without restriction, including | ||||
|  * without limitation the rights to use, copy, modify, merge, publish, | ||||
|  * distribute, sublicense, and/or sell copies of the Software, and to | ||||
|  * permit persons to whom the Software is furnished to do so, subject to | ||||
|  * the following conditions: | ||||
|  * | ||||
|  * The above copyright notice and this permission notice shall be | ||||
|  * included in all copies or substantial portions of the Software. | ||||
|  * | ||||
|  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||||
|  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||||
|  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||||
|  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | ||||
|  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||||
|  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||||
|  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||||
|  */ | ||||
|  | ||||
| #include <unistd.h> | ||||
| #include <iostream> | ||||
| #include "tm1637.h" | ||||
|  | ||||
| int | ||||
| main(int argc, char **argv) | ||||
| { | ||||
| 	uint8_t data[] = { 0xaa, 0xff, 0xff, 0xff }; | ||||
|  | ||||
|     upm::TM1637 *display = new upm::TM1637(8, 9); // di - 8, dcki - 9 | ||||
| 	display->write ("1981"); | ||||
| 	 | ||||
| 	std::cout << "exiting application" << std::endl; | ||||
|  | ||||
| 	delete display; | ||||
|  | ||||
|     return 0; | ||||
| } | ||||
| @@ -5,12 +5,14 @@ add_executable (lcm-lcd lcm-lcd.cxx) | ||||
| add_executable (rgb-lcd rgb-lcd.cxx) | ||||
| add_executable (buzzer-sound buzzer-sound.cxx) | ||||
| add_executable (led-bar led-bar.cxx) | ||||
| add_executable (seg-lcd 4digitdisplay.cxx) | ||||
|  | ||||
| include_directories (${PROJECT_SOURCE_DIR}/src/hmc5883l) | ||||
| include_directories (${PROJECT_SOURCE_DIR}/src/grove) | ||||
| include_directories (${PROJECT_SOURCE_DIR}/src/lcd) | ||||
| include_directories (${PROJECT_SOURCE_DIR}/src/buzzer) | ||||
| include_directories (${PROJECT_SOURCE_DIR}/src/ledbar) | ||||
| include_directories (${PROJECT_SOURCE_DIR}/src/4digitdisplay) | ||||
|  | ||||
| target_link_libraries (compass hmc5883l ${CMAKE_THREAD_LIBS_INIT}) | ||||
| target_link_libraries (groveled grove ${CMAKE_THREAD_LIBS_INIT}) | ||||
| @@ -19,3 +21,4 @@ target_link_libraries (lcm-lcd i2clcd ${CMAKE_THREAD_LIBS_INIT}) | ||||
| target_link_libraries (rgb-lcd i2clcd ${CMAKE_THREAD_LIBS_INIT}) | ||||
| target_link_libraries (buzzer-sound buzzer ${CMAKE_THREAD_LIBS_INIT}) | ||||
| target_link_libraries (led-bar ledbar ${CMAKE_THREAD_LIBS_INIT}) | ||||
| target_link_libraries (seg-lcd 4digitdisplay ${CMAKE_THREAD_LIBS_INIT}) | ||||
|   | ||||
							
								
								
									
										4
									
								
								src/4digitdisplay/CMakeLists.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										4
									
								
								src/4digitdisplay/CMakeLists.txt
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,4 @@ | ||||
| set (libname "4digitdisplay") | ||||
| add_library (4digitdisplay SHARED tm1637.cxx) | ||||
| include_directories (${MAA_INCLUDE_DIR}) | ||||
| target_link_libraries (4digitdisplay ${MAA_LIBRARIES}) | ||||
							
								
								
									
										7
									
								
								src/4digitdisplay/jsupm_tm1637.i
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								src/4digitdisplay/jsupm_tm1637.i
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,7 @@ | ||||
| %module jsupm_tm1637 | ||||
|  | ||||
| %{ | ||||
|     #include "tm1637.h" | ||||
| %} | ||||
|  | ||||
| %include "tm1637.h" | ||||
							
								
								
									
										8
									
								
								src/4digitdisplay/pyupm_tm1637.i
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								src/4digitdisplay/pyupm_tm1637.i
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,8 @@ | ||||
| %module pyupm_tm1637 | ||||
|  | ||||
| %feature("autodoc", "3"); | ||||
|  | ||||
| %include "tm1637.h" | ||||
| %{ | ||||
|     #include "tm1637.h" | ||||
| %} | ||||
							
								
								
									
										190
									
								
								src/4digitdisplay/tm1637.cxx
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										190
									
								
								src/4digitdisplay/tm1637.cxx
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,190 @@ | ||||
| /* | ||||
|  * Author: Yevgeniy Kiveisha <yevgeniy.kiveisha@intel.com> | ||||
|  * Copyright (c) 2014 Intel Corporation. | ||||
|  * | ||||
|  * Permission is hereby granted, free of charge, to any person obtaining | ||||
|  * a copy of this software and associated documentation files (the | ||||
|  * "Software"), to deal in the Software without restriction, including | ||||
|  * without limitation the rights to use, copy, modify, merge, publish, | ||||
|  * distribute, sublicense, and/or sell copies of the Software, and to | ||||
|  * permit persons to whom the Software is furnished to do so, subject to | ||||
|  * the following conditions: | ||||
|  * | ||||
|  * The above copyright notice and this permission notice shall be | ||||
|  * included in all copies or substantial portions of the Software. | ||||
|  * | ||||
|  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||||
|  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||||
|  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||||
|  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | ||||
|  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||||
|  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||||
|  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||||
|  */ | ||||
|  | ||||
| #include <iostream> | ||||
| #include <unistd.h> | ||||
|  | ||||
| #include "tm1637.h" | ||||
|  | ||||
| using namespace upm; | ||||
|  | ||||
| const uint8_t digitToSegment[] = { | ||||
|  	// XGFEDCBA | ||||
| 	0b00111111,    // 0 | ||||
| 	0b00000110,    // 1 | ||||
| 	0b01011011,    // 2 | ||||
| 	0b01001111,    // 3 | ||||
| 	0b01100110,    // 4 | ||||
| 	0b01101101,    // 5 | ||||
| 	0b01111101,    // 6 | ||||
| 	0b00000111,    // 7 | ||||
| 	0b01111111,    // 8 | ||||
| 	0b01101111,    // 9 | ||||
| 	0b01110111,    // A | ||||
| 	0b01111100,    // B | ||||
| 	0b00111001,    // C | ||||
| 	0b01000111,    // D | ||||
| 	0b01111001,    // E | ||||
| 	0b01110001     // F | ||||
| }; | ||||
|  | ||||
| TM1637::TM1637 (uint8_t di, uint8_t dcki) { | ||||
| 	maa_result_t error = MAA_SUCCESS; | ||||
| 	maa_init(); | ||||
|  | ||||
| 	// init clock context | ||||
| 	m_clkPinCtx = maa_gpio_init(dcki); | ||||
| 	if (m_clkPinCtx == NULL) { | ||||
|         fprintf(stderr, "Are you sure that pin%d you requested is valid on your platform?", dcki); | ||||
| 		exit(1); | ||||
| 	} | ||||
| 	// init data context | ||||
| 	m_dataPinCtx = maa_gpio_init(di); | ||||
| 	if (m_dataPinCtx == NULL) { | ||||
|         fprintf(stderr, "Are you sure that pin%d you requested is valid on your platform?", di); | ||||
| 		exit(1); | ||||
| 	} | ||||
| 	 | ||||
| 	// set direction (out) | ||||
| 	error = maa_gpio_dir(m_clkPinCtx, MAA_GPIO_IN); | ||||
|     if (error != MAA_SUCCESS) { | ||||
|         maa_result_print(error); | ||||
|     } | ||||
|  | ||||
| 	// set direction (out) | ||||
| 	error = maa_gpio_dir(m_dataPinCtx, MAA_GPIO_IN); | ||||
|     if (error != MAA_SUCCESS) { | ||||
|         maa_result_print(error); | ||||
|     } | ||||
|  | ||||
| 	error = maa_gpio_write (m_dataPinCtx, LOW); | ||||
| 	error = maa_gpio_write (m_clkPinCtx, LOW); | ||||
| } | ||||
|  | ||||
| TM1637::~TM1637() { | ||||
| 	maa_result_t error = MAA_SUCCESS; | ||||
| 	error = maa_gpio_close (m_dataPinCtx); | ||||
|     if (error != MAA_SUCCESS) { | ||||
|         maa_result_print(error); | ||||
|     } | ||||
| 	error = maa_gpio_close (m_clkPinCtx); | ||||
|     if (error != MAA_SUCCESS) { | ||||
|         maa_result_print(error); | ||||
|     } | ||||
| } | ||||
|  | ||||
| maa_result_t  | ||||
| TM1637::setBrightness (uint8_t level) { | ||||
| 	m_brightness = level; | ||||
| } | ||||
|  | ||||
| maa_result_t  | ||||
| TM1637::setSegments (const uint8_t segments[], uint8_t length, uint8_t pos) { | ||||
| 	start(); | ||||
| 	writeByte(TM1637_I2C_COMM1); | ||||
| 	stop(); | ||||
|  | ||||
| 	start(); | ||||
| 	writeByte(TM1637_I2C_COMM2 + (pos & 0x03)); | ||||
| 	for (uint8_t idx = 0; idx < length; idx++) { | ||||
| 		writeByte(segments[idx]); | ||||
| 	} | ||||
| 	stop(); | ||||
|  | ||||
| 	start(); | ||||
| 	writeByte(TM1637_I2C_COMM3 + (m_brightness & 0x0f)); | ||||
| 	stop(); | ||||
| } | ||||
|  | ||||
| maa_result_t  | ||||
| TM1637::write (std::string msg) { | ||||
| 	char leter = '\0'; | ||||
| 	uint8_t data[] = { 0x0, 0x0, 0x0, 0x0 }; | ||||
| 	for (uint8_t idx = 0; idx < msg.length(); idx++) { | ||||
| 		leter = msg[idx]; | ||||
| 		if (idx < 4) { | ||||
| 			data[idx] = digitToSegment[strtol(&leter, NULL, 16)]; | ||||
| 		} | ||||
| 	} | ||||
| 	setBrightness (0x0f); | ||||
| 	setSegments(data); | ||||
| } | ||||
|  | ||||
| maa_result_t  | ||||
| TM1637::pinMode (maa_gpio_context ctx, gpio_dir_t mode) { | ||||
| 	maa_result_t error = MAA_SUCCESS; | ||||
| 	error = maa_gpio_dir(ctx, mode); | ||||
|     if (error != MAA_SUCCESS) { | ||||
|         maa_result_print(error); | ||||
|     } | ||||
| } | ||||
|  | ||||
| maa_result_t  | ||||
| TM1637::start() { | ||||
| 	pinMode (m_dataPinCtx, MAA_GPIO_OUT); | ||||
| 	usleep(PULSE_LENGTH); | ||||
| } | ||||
|  | ||||
| maa_result_t  | ||||
| TM1637::stop() { | ||||
| 	pinMode (m_dataPinCtx, MAA_GPIO_OUT); | ||||
| 	usleep(PULSE_LENGTH); | ||||
| 	pinMode (m_clkPinCtx, MAA_GPIO_IN); | ||||
| 	usleep(PULSE_LENGTH); | ||||
| 	pinMode (m_dataPinCtx, MAA_GPIO_IN); | ||||
| 	usleep(PULSE_LENGTH); | ||||
| } | ||||
|  | ||||
| maa_result_t  | ||||
| TM1637::writeByte(uint8_t value) { | ||||
| 	for (uint8_t idx = 0; idx < 8; idx++) { | ||||
| 		pinMode(m_clkPinCtx, MAA_GPIO_OUT); | ||||
| 		usleep(PULSE_LENGTH); | ||||
| 		if (value & 0x01) { | ||||
| 	  		pinMode(m_dataPinCtx, MAA_GPIO_IN); | ||||
| 		} else { | ||||
| 	  		pinMode(m_dataPinCtx, MAA_GPIO_OUT); | ||||
| 		} | ||||
| 		usleep(PULSE_LENGTH); | ||||
|  | ||||
| 		pinMode(m_clkPinCtx, MAA_GPIO_IN); | ||||
| 		usleep(PULSE_LENGTH); | ||||
| 		value = value >> 1; | ||||
| 	} | ||||
|  | ||||
| 	pinMode(m_clkPinCtx, MAA_GPIO_OUT); | ||||
| 	pinMode(m_dataPinCtx, MAA_GPIO_IN); | ||||
| 	usleep(PULSE_LENGTH); | ||||
|  | ||||
| 	pinMode(m_clkPinCtx, MAA_GPIO_IN); | ||||
| 	usleep(PULSE_LENGTH); | ||||
|  | ||||
| 	uint8_t ack = maa_gpio_read (m_dataPinCtx); | ||||
| 	if (ack == 0) { | ||||
|     	pinMode(m_dataPinCtx, MAA_GPIO_OUT); | ||||
| 	} usleep(PULSE_LENGTH); | ||||
|  | ||||
| 	pinMode(m_clkPinCtx, MAA_GPIO_OUT); | ||||
| 	usleep(50); | ||||
| } | ||||
							
								
								
									
										82
									
								
								src/4digitdisplay/tm1637.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										82
									
								
								src/4digitdisplay/tm1637.h
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,82 @@ | ||||
| /* | ||||
|  * Author: Yevgeniy Kiveisha <yevgeniy.kiveisha@intel.com> | ||||
|  * Copyright (c) 2014 Intel Corporation. | ||||
|  * | ||||
|  * Permission is hereby granted, free of charge, to any person obtaining | ||||
|  * a copy of this software and associated documentation files (the | ||||
|  * "Software"), to deal in the Software without restriction, including | ||||
|  * without limitation the rights to use, copy, modify, merge, publish, | ||||
|  * distribute, sublicense, and/or sell copies of the Software, and to | ||||
|  * permit persons to whom the Software is furnished to do so, subject to | ||||
|  * the following conditions: | ||||
|  * | ||||
|  * The above copyright notice and this permission notice shall be | ||||
|  * included in all copies or substantial portions of the Software. | ||||
|  * | ||||
|  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||||
|  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||||
|  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||||
|  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | ||||
|  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||||
|  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||||
|  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||||
|  */ | ||||
| #pragma once | ||||
|  | ||||
| #include <string> | ||||
| #include <maa/aio.h> | ||||
| #include <maa/gpio.h> | ||||
|  | ||||
| // | ||||
| //      A | ||||
| //     --- | ||||
| //  F |   | B | ||||
| //     -G- | ||||
| //  E |   | C | ||||
| //     --- | ||||
| //      D | ||||
|  | ||||
| #define SEG_A   			0b00000001 | ||||
| #define SEG_B   			0b00000010 | ||||
| #define SEG_C   			0b00000100 | ||||
| #define SEG_D   			0b00001000 | ||||
| #define SEG_E   			0b00010000 | ||||
| #define SEG_F   			0b00100000 | ||||
| #define SEG_G   			0b01000000 | ||||
|  | ||||
| #define TM1637_I2C_COMM1    0x40 | ||||
| #define TM1637_I2C_COMM2    0xC0 | ||||
| #define TM1637_I2C_COMM3    0x80 | ||||
|  | ||||
| #define PULSE_LENGTH		50 | ||||
|  | ||||
| #define HIGH          		1 | ||||
| #define LOW	        		0 | ||||
|  | ||||
| namespace upm { | ||||
|  | ||||
| class TM1637 { | ||||
|     public: | ||||
| 		TM1637 (uint8_t di, uint8_t dcki); | ||||
| 		~TM1637 (); | ||||
| 		maa_result_t setBrightness (uint8_t level); | ||||
| 		maa_result_t setSegments (const uint8_t segments[], uint8_t length = 4, uint8_t pos = 0); | ||||
| 		maa_result_t write (std::string msg); | ||||
| 		 | ||||
| 		std::string name() | ||||
|         { | ||||
|             return m_name; | ||||
|         } | ||||
| 	private: | ||||
| 		maa_result_t start(); | ||||
| 		maa_result_t stop(); | ||||
| 		maa_result_t writeByte (uint8_t value); | ||||
| 		maa_result_t pinMode (maa_gpio_context ctx, gpio_dir_t mode); | ||||
|  | ||||
| 		std::string m_name; | ||||
| 		maa_gpio_context m_clkPinCtx; | ||||
| 		maa_gpio_context m_dataPinCtx; | ||||
| 		uint8_t m_brightness; | ||||
| }; | ||||
|  | ||||
| } | ||||
| @@ -3,3 +3,4 @@ add_subdirectory (grove) | ||||
| add_subdirectory (lcd) | ||||
| add_subdirectory (buzzer) | ||||
| add_subdirectory (ledbar) | ||||
| add_subdirectory (4digitdisplay) | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Yevgeniy Kiveisha
					Yevgeniy Kiveisha