diff --git a/Code/devterm_wiringpi_cpi/gpio/CPi_readall.c b/Code/devterm_wiringpi_cpi/gpio/CPi_readall.c index 30cadc2..86bc0b2 100755 --- a/Code/devterm_wiringpi_cpi/gpio/CPi_readall.c +++ b/Code/devterm_wiringpi_cpi/gpio/CPi_readall.c @@ -8,35 +8,35 @@ int bcmToGpioCPi[64] = { - 106, 107, // 0, 1 - 104, 10, // 2, 3 - 3, 9, // 4 5 - 4, 90, // 6, 7 - 92, 158, // 8, 9 - 156, 105, //10,11 - 146, 150, //12,13 - 81, 80, //14,15 + 58, 57, // 0, 1 + 167, 0, // 2, 3 + 1, 2, // 4 5 + 3, 4, // 6, 7 + 5, 6, // 8, 9 + 7, 8, //10,11 + 15, 54, //12,13 + 134, 135, //14,15 - 82, 83, //16,17 - 131, 132, //18,19 - 134, 135, //20,21 - 89, 88, //22,23 - 84, 85, //24,25 - 86, 87, //26,27 - 112, 113, //28,29 - 109, 157, //30,31 + 137, 136, //16,17 + 139, 138, //18,19 + 141, 140, //20,21 + 128, 129, //22,23 + 130, 131, //24,25 + 132, 133, //26,27 + 9, 201, //28,29 + 196, 199, //30,31 - 148, 147, //32,33 - 100, 101, //34,35 - 102, 103, //36,37 - 97, 98, //38,39 - 99, 96, //40,41 - 110, 111, //42,43 - 64, 65, //44,45 - -1, -1, //46,47 + 161, 160, //32,33 + 227, 198, //34,35 + 163, 166, //36,37 + 165, 164, //38,39 + 228, 224, //40,41 + 225, 226, //42,43 + 56, 55, //44,45 + -1, -1, //46,47 - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,// ... 63 -} + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,// ... 63 +}; #endif @@ -71,7 +71,7 @@ int bcmToGpioCPi[64] = 64, 65, //44,45 -1, -1, //46,47 - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,// ... 63 + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,// ... 63 }; #endif @@ -81,32 +81,60 @@ static char *alts [] = "IN", "OUT", "ALT2", "ALT3", "ALT4", "ALT5", "ALT6", "OFF" } ; +static char* get_pin_name(int pin) +{ + static char name[10]; + char c; + int b, d; + + b = pin/32; + d = pin % 32; + +#ifdef CONFIG_CLOCKWORKPI_A04 + + if(b < 6) + c = b + 'C'; + else + c = b - 6 + 'L'; + sprintf(name, "P%c%d", c, d); + +#elif defined(CONFIG_CLOCKWORKPI_A06) + + c = d/8 + 'A'; + sprintf(name, "%d%c%d", b, c, d % 8); + +#endif + return name; +} + void CPiReadAll(void) { int pin, pin2; int tmp = wiringPiDebug; wiringPiDebug = FALSE; - printf (" +-----+------+------+---+-----+------+------+---+\n"); - printf (" | BCM | GPIO | Mode | V | BCM | GPIO | Mode | V |\n"); - printf (" +-----+------+------+---+-----+------+------+---+\n"); + printf (" +-----+------+------+------+---+-----+------+------+------+---+\n"); + printf (" | BCM | GPIO | Name | Mode | V | BCM | GPIO | Name | Mode | V |\n"); + printf (" +-----+------+------+------+---+-----+------+------+------+---+\n"); for (pin = 0 ; pin < 23; pin ++) { printf (" | %3d", pin); printf (" | %4d", bcmToGpioCPi[pin]); + printf (" | %-4s", get_pin_name(bcmToGpioCPi[pin])); printf (" | %4s", alts [CPi_get_gpio_mode(bcmToGpioCPi[pin])]); printf (" | %d", CPi_digitalRead(bcmToGpioCPi[pin])) ; pin2 = pin + 23; printf (" | %3d", pin2); printf (" | %4d", bcmToGpioCPi[pin2]); + printf (" | %-4s", get_pin_name(bcmToGpioCPi[pin2])); printf (" | %4s", alts [CPi_get_gpio_mode(bcmToGpioCPi[pin2])]); printf (" | %d", CPi_digitalRead(bcmToGpioCPi[pin2])) ; printf (" |\n") ; } - printf (" +-----+------+------+---+-----+------+------+---+\n"); - printf (" | BCM | GPIO | Mode | V | BCM | GPIO | Mode | V |\n"); - printf (" +-----+------+------+---+-----+------+------+---+\n"); + printf (" +-----+------+------+------+---+-----+------+------+------+---+\n"); + printf (" | BCM | GPIO | Name | Mode | V | BCM | GPIO | Name | Mode | V |\n"); + printf (" +-----+------+------+------+---+-----+------+------+------+---+\n"); wiringPiDebug = tmp; } @@ -117,6 +145,38 @@ void CPiReadAllRaw(void) int tmp = wiringPiDebug; wiringPiDebug = FALSE; +#ifdef CONFIG_CLOCKWORKPI_A04 + + printf (" +------+------+------+---+------+------+------+---+------+------+------+---+------+------+------+---+------+------+------+---+------+------+------+---+\n"); + printf (" | GPIO | Name | Mode | V | GPIO | Name | Mode | V | GPIO | Name | Mode | V | GPIO | Name | Mode | V | GPIO | Name | Mode | V | GPIO | Name | Mode | V |\n"); + printf (" +------+------+------+---+------+------+------+---+------+------+------+---+------+------+------+---+------+------+------+---+------+------+------+---+\n"); + + for (pin = 0 ; pin < 27; pin++) { + pin2 = pin; + for(i = 0; i < 6; i++) { + if(CPi_get_gpio_mode(pin2) >= 0) { + printf (" | %4d", pin2) ; + printf (" | %-4s", get_pin_name(pin2)); + printf (" | %4s", alts [CPi_get_gpio_mode(pin2)]) ; + printf (" | %d", CPi_digitalRead(pin2)) ; + } else { + printf (" | ") ; + printf (" | ") ; + printf (" | ") ; + printf (" | ") ; + } + pin2 += 32; + if(i == 1) pin2 += 64; + } + printf (" |\n") ; + } + + printf (" +------+------+------+---+------+------+------+---+------+------+------+---+------+------+------+---+------+------+------+---+------+------+------+---+\n"); + printf (" | GPIO | Name | Mode | V | GPIO | Name | Mode | V | GPIO | Name | Mode | V | GPIO | Name | Mode | V | GPIO | Name | Mode | V | GPIO | Name | Mode | V |\n"); + printf (" +------+------+------+---+------+------+------+---+------+------+------+---+------+------+------+---+------+------+------+---+------+------+------+---+\n"); + +#elif defined(CONFIG_CLOCKWORKPI_A06) + printf (" +------+------+---+------+------+---+------+------+---+------+------+---+------+------+---+\n"); printf (" | GPIO | Mode | V | GPIO | Mode | V | GPIO | Mode | V | GPIO | Mode | V | GPIO | Mode | V |\n"); printf (" +------+------+---+------+------+---+------+------+---+------+------+---+------+------+---+\n"); @@ -136,6 +196,8 @@ void CPiReadAllRaw(void) printf (" | GPIO | Mode | V | GPIO | Mode | V | GPIO | Mode | V | GPIO | Mode | V | GPIO | Mode | V |\n"); printf (" +------+------+---+------+------+---+------+------+---+------+------+---+------+------+---+\n"); +#endif + wiringPiDebug = tmp; } diff --git a/Code/devterm_wiringpi_cpi/wiringPi/wiringCPi.c b/Code/devterm_wiringpi_cpi/wiringPi/wiringCPi.c index 9abef8d..fc1313e 100755 --- a/Code/devterm_wiringpi_cpi/wiringPi/wiringCPi.c +++ b/Code/devterm_wiringpi_cpi/wiringPi/wiringCPi.c @@ -30,22 +30,53 @@ static int wpimode = -1 ; #ifdef CONFIG_CLOCKWORKPI_A04 -int CPI_PIN_MASK[12][32] = //[BANK] [INDEX] +int bcmToGpioCPi[64] = { - {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,},//PA - {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,},//PB - {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,},//PC - {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,15,16,-1,18,-1,-1,21,22,23,24,25,26,-1,-1,-1,-1,-1,},//PD - {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,},//PE - {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,},//PF - {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,},//PG - {-1,-1,-1, 3, 4, 5, 6,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,},//PH - {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,},//PI - {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,},//PJ - {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,},//PK - {-1,-1, 2, 3,-1,-1,-1,-1, 8,-1,10,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,},//PL + 58, 57, // 0, 1 + 167, 0, // 2, 3 + 1, 2, // 4 5 + 3, 4, // 6, 7 + 5, 6, // 8, 9 + 7, 8, //10,11 + 15, 54, //12,13 + 134, 135, //14,15 + + 137, 136, //16,17 + 139, 138, //18,19 + 141, 140, //20,21 + 128, 129, //22,23 + 130, 131, //24,25 + 132, 133, //26,27 + 9, 201, //28,29 + 196, 199, //30,31 + + 161, 160, //32,33 + 227, 198, //34,35 + 163, 166, //36,37 + 165, 164, //38,39 + 228, 224, //40,41 + 225, 226, //42,43 + 56, 55, //44,45 + -1, -1, //46,47 + + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,// ... 63 }; +int CPI_PIN_MASK[8][32] = //[BANK] [INDEX] +{ + { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,},//PC 0 + { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1,},//PD 32 + {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,},//PE 64 + {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,},//PF 96 + { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,},//PG 128 + { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,},//PH 160 + { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,},//PL 192 + { 1, 1, 1, 1, 1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,},//PM 224 +}; + +volatile uint32_t *gpio_base; +volatile uint32_t *gpioL_base; + #endif #ifdef CONFIG_CLOCKWORKPI_A06 @@ -84,11 +115,11 @@ int bcmToGpioCPi[64] = int CPI_PIN_MASK[5][32] = //[BANK] [INDEX] { - { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,},//PA - { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,},//PB - { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,},//PC - { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,},//PD - { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,},//PD + { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,},//GPIO0 + { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,},//GPIO1 + { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,},//GPIO2 + { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,},//GPIO3 + { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,},//GPIO4 }; volatile uint32_t *cru_base; @@ -132,8 +163,17 @@ static unsigned int readR(unsigned int addr) return val; #elif (defined CONFIG_CLOCKWORKPI_A04) + uint32_t val = 0; + uint32_t mmap_base = (addr & ~MAP_MASK); + uint32_t mmap_seek = ((addr - mmap_base) >> 2); + + if (addr >= GPIOL_BASE) + val = *(gpioL_base + mmap_seek); + else + val = *(gpio_base + mmap_seek); return val; + #endif } @@ -165,6 +205,14 @@ static void writeR(unsigned int val, unsigned int addr) #elif (defined CONFIG_CLOCKWORKPI_A04) + unsigned int mmap_base = (addr & ~MAP_MASK); + unsigned int mmap_seek = ((addr - mmap_base) >> 2); + + if (addr >= GPIOL_BASE) + *(gpioL_base + mmap_seek) = val; + else + *(gpio_base + mmap_seek) = val; + #endif } @@ -175,6 +223,9 @@ int CPi_get_gpio_mode(int pin) unsigned int index = pin - (bank << 5); unsigned int phyaddr = 0; unsigned char mode = -1; + + if (CPI_PIN_MASK[bank][index] < 0) + return -1; #ifdef CONFIG_CLOCKWORKPI_A06 @@ -202,31 +253,29 @@ int CPi_get_gpio_mode(int pin) ddr_phyaddr = GPIO4_BASE + GPIO_SWPORTA_DDR_OFFSET; } - if (CPI_PIN_MASK[bank][index] != -1) { - regval = readR(grf_phyaddr); - mode = (regval >> (offset << 1)) & 0x3; - if(mode == 0){ - regval = readR(ddr_phyaddr); - return (regval >> index) & 1; - } - return mode + 1; + regval = readR(grf_phyaddr); + mode = (regval >> (offset << 1)) & 0x3; + + if(mode == 0){ + regval = readR(ddr_phyaddr); + return (regval >> index) & 1; } + return mode + 1; + #elif (defined CONFIG_CLOCKWORKPI_A04) int offset = ((index - ((index >> 3) << 3)) << 2); - if (bank == 11) { - phyaddr = GPIOL_BASE + ((index >> 3) << 2); + if (bank >= 6) { + phyaddr = GPIOL_BASE + (bank -6) * 0x24 + ((index >> 3) << 2); + } + else { + phyaddr = GPIO_BASE_MAP + (bank * 0x24) + ((index >> 3) << 2); } - else - phyaddr = GPIO_BASE_MAP + (bank * 36) + ((index >> 3) << 2); - /* Ignore unused gpio */ - if (CPI_PIN_MASK[bank][index] != -1) { - regval = readR(phyaddr); - mode = (regval >> offset) & 7; - } + regval = readR(phyaddr); + mode = (regval >> offset) & 7; return mode; @@ -243,21 +292,23 @@ int CPi_set_gpio_mode(int pin, int mode) unsigned int index = pin - (bank << 5); unsigned int phyaddr = 0; -#ifdef CONFIG_CLOCKWORKPI_A06 - int offset = ((index - ((index >> 3) << 3))); - unsigned int cru_phyaddr, grf_phyaddr, gpio_phyaddr; -#elif (defined CONFIG_CLOCKWORKPI_A04) +#ifdef CONFIG_CLOCKWORKPI_A04 + int offset = ((index - ((index >> 3) << 3)) << 2); - if (bank == 11) { - phyaddr = GPIOL_BASE + ((index >> 3) << 2); + if (bank >= 6) { + phyaddr = GPIOL_BASE + (bank -6) * 0x24 + ((index >> 3) << 2); } else { - phyaddr = GPIO_BASE_MAP + (bank * 36) + ((index >> 3) << 2); + phyaddr = GPIO_BASE_MAP + (bank * 0x24) + ((index >> 3) << 2); } + #endif #ifdef CONFIG_CLOCKWORKPI_A06 + int offset = ((index - ((index >> 3) << 3))); + unsigned int cru_phyaddr, grf_phyaddr, gpio_phyaddr; + if(bank == 0){ cru_phyaddr = PMUCRU_BASE + PMUCRU_CLKGATE_CON1_OFFSET; grf_phyaddr = PMUGRF_BASE + ((index >> 3) << 2); @@ -286,38 +337,37 @@ int CPi_set_gpio_mode(int pin, int mode) #endif - if (CPI_PIN_MASK[bank][index] != -1) { + if (CPI_PIN_MASK[bank][index] != -1) { #ifdef CONFIG_CLOCKWORKPI_A04 regval = readR(phyaddr); - if (wiringPiDebug) - printf("Before read reg val: 0x%x offset:%d\n",regval,offset); + if (wiringPiDebug) + printf("Before read reg val: 0x%x offset:%d\n",regval,offset); #endif - if (wiringPiDebug) - printf("Register[%#x]: %#x index:%d\n", phyaddr, regval, index); + if (wiringPiDebug) + printf("Register[%#x]: %#x index:%d\n", phyaddr, regval, index); - /* Set Input */ - if (INPUT == mode) { + if (INPUT == mode) { #ifdef CONFIG_CLOCKWORKPI_A06 - writeR(0xffff0180, cru_phyaddr); - regval = readR(grf_phyaddr); - regval |= 0x3 << ((offset << 1) | 0x10); - regval &= ~(0x3 << (offset << 1)); - writeR(regval, grf_phyaddr); - regval = readR(gpio_phyaddr); - regval &= ~(1 << index); - writeR(regval, gpio_phyaddr); - if (wiringPiDebug){ + writeR(0xffff0180, cru_phyaddr); + regval = readR(grf_phyaddr); + regval |= 0x3 << ((offset << 1) | 0x10); + regval &= ~(0x3 << (offset << 1)); + writeR(regval, grf_phyaddr); regval = readR(gpio_phyaddr); - printf("Input mode set over reg val: %#x\n",regval); - } + regval &= ~(1 << index); + writeR(regval, gpio_phyaddr); + if (wiringPiDebug){ + regval = readR(gpio_phyaddr); + printf("Input mode set over reg val: %#x\n",regval); + } #else - regval &= ~(7 << offset); - writeR(regval, phyaddr); - regval = readR(phyaddr); - if (wiringPiDebug) - printf("Input mode set over reg val: %#x\n",regval); + regval &= ~(7 << offset); + writeR(regval, phyaddr); + regval = readR(phyaddr); + if (wiringPiDebug) + printf("Input mode set over reg val: %#x\n",regval); #endif - } else if (OUTPUT == mode) { /* Set Output */ + } else if (OUTPUT == mode) { #ifdef CONFIG_CLOCKWORKPI_A06 writeR(0xffff0180, cru_phyaddr); regval = readR(grf_phyaddr); @@ -332,19 +382,19 @@ int CPi_set_gpio_mode(int pin, int mode) printf("Out mode get value: 0x%x\n",regval); } #else - regval &= ~(7 << offset); - regval |= (1 << offset); - if (wiringPiDebug) - printf("Out mode ready set val: 0x%x\n",regval); - writeR(regval, phyaddr); - regval = readR(phyaddr); - if (wiringPiDebug) - printf("Out mode get value: 0x%x\n",regval); + regval &= ~(7 << offset); + regval |= (1 << offset); + if (wiringPiDebug) + printf("Out mode ready set val: 0x%x\n",regval); + writeR(regval, phyaddr); + regval = readR(phyaddr); + if (wiringPiDebug) + printf("Out mode get value: 0x%x\n",regval); #endif - }else - printf("Unknow mode\n"); - } else - printf("unused pin\n"); + } else + printf("Unknow mode\n"); + } else + printf("unused pin\n"); } int CPi_set_gpio_alt(int pin, int mode) @@ -356,10 +406,10 @@ int CPi_set_gpio_alt(int pin, int mode) unsigned int phyaddr = 0; int offset = ((index - ((index >> 3) << 3)) << 2); - if (bank == 11) { + if (bank >= 6) { phyaddr = GPIOL_BASE + ((index >> 3) << 2); }else - phyaddr = GPIO_BASE_MAP + (bank * 36) + ((index >> 3) << 2); + phyaddr = GPIO_BASE_MAP + (bank * 0x24) + ((index >> 3) << 2); /* Ignore unused gpio */ if (CPI_PIN_MASK[bank][index] != -1) { @@ -382,56 +432,56 @@ int CPi_set_gpio_alt(int pin, int mode) */ void CPi_digitalWrite(int pin, int value) { - unsigned int bank = pin >> 5; - unsigned int index = pin - (bank << 5); - unsigned int phyaddr = 0; -#ifdef CONFIG_CLOCKWORKPI_A06 + unsigned int bank = pin >> 5; + unsigned int index = pin - (bank << 5); + unsigned int phyaddr = 0; unsigned int regval = 0; + +#ifdef CONFIG_CLOCKWORKPI_A04 + + if (bank >= 6) { + phyaddr = GPIOL_BASE + (bank -6) * 0x24 + 0x10; + } else { + phyaddr = GPIO_BASE_MAP + (bank * 0x24) + 0x10; + } + +#endif + +#ifdef CONFIG_CLOCKWORKPI_A06 + unsigned int cru_phyaddr = 0; -#else - unsigned int regval = 0; - - if (bank == 11) { - phyaddr = GPIOL_BASE + 0x10; + if(bank == 0){ + phyaddr = GPIO0_BASE + GPIO_SWPORTA_DR_OFFSET; + cru_phyaddr = PMUCRU_BASE + PMUCRU_CLKGATE_CON1_OFFSET; } - else - phyaddr = GPIO_BASE_MAP + (bank * 36) + 0x10; + else if(bank == 1){ + phyaddr = GPIO1_BASE + GPIO_SWPORTA_DR_OFFSET; + cru_phyaddr = PMUCRU_BASE + PMUCRU_CLKGATE_CON1_OFFSET; + } + else if(bank == 2){ + phyaddr = GPIO2_BASE + GPIO_SWPORTA_DR_OFFSET; + cru_phyaddr = CRU_BASE + CRU_CLKGATE_CON31_OFFSET; + } + else if(bank == 3){ + phyaddr = GPIO3_BASE + GPIO_SWPORTA_DR_OFFSET; + cru_phyaddr = CRU_BASE + CRU_CLKGATE_CON31_OFFSET; + } + else if(bank == 4){ + phyaddr = GPIO4_BASE + GPIO_SWPORTA_DR_OFFSET; + cru_phyaddr = CRU_BASE + CRU_CLKGATE_CON31_OFFSET; + } + #endif + if (CPI_PIN_MASK[bank][index] != -1) { + #ifdef CONFIG_CLOCKWORKPI_A06 - - if(bank == 0){ - phyaddr = GPIO0_BASE + GPIO_SWPORTA_DR_OFFSET; - cru_phyaddr = PMUCRU_BASE + PMUCRU_CLKGATE_CON1_OFFSET; - } - else if(bank == 1){ - phyaddr = GPIO1_BASE + GPIO_SWPORTA_DR_OFFSET; - cru_phyaddr = PMUCRU_BASE + PMUCRU_CLKGATE_CON1_OFFSET; - } - else if(bank == 2){ - phyaddr = GPIO2_BASE + GPIO_SWPORTA_DR_OFFSET; - cru_phyaddr = CRU_BASE + CRU_CLKGATE_CON31_OFFSET; - } - else if(bank == 3){ - phyaddr = GPIO3_BASE + GPIO_SWPORTA_DR_OFFSET; - cru_phyaddr = CRU_BASE + CRU_CLKGATE_CON31_OFFSET; - } - else if(bank == 4){ - phyaddr = GPIO4_BASE + GPIO_SWPORTA_DR_OFFSET; - cru_phyaddr = CRU_BASE + CRU_CLKGATE_CON31_OFFSET; - } - + writeR(0xffff0180, cru_phyaddr); #endif - if (CPI_PIN_MASK[bank][index] != -1) { - - #ifdef CONFIG_CLOCKWORKPI_A06 - writeR(0xffff0180, cru_phyaddr); - #endif - - if (wiringPiDebug) - printf("pin: %d, bank: %d, index: %d, phyaddr: 0x%x\n", pin, bank, index, phyaddr); + if (wiringPiDebug) + printf("pin: %d, bank: %d, index: %d, phyaddr: 0x%x\n", pin, bank, index, phyaddr); regval = readR(phyaddr); if (wiringPiDebug) @@ -450,9 +500,7 @@ void CPi_digitalWrite(int pin, int value) printf("HIGH val set over reg val: 0x%x\n", regval); } - } else - printf("unused pin\n"); - + } } /* @@ -463,19 +511,20 @@ int CPi_digitalRead(int pin) int bank = pin >> 5; int index = pin - (bank << 5); int val; + unsigned int phyaddr; + #ifdef CONFIG_CLOCKWORKPI_A04 - unsigned int phyaddr; - - if (bank == 11) { - phyaddr = GPIOL_BASE + 0x10; + if (bank >= 6) { + phyaddr = GPIOL_BASE + (bank -6) * 0x24 + 0x10; } else { - phyaddr = GPIO_BASE_MAP + (bank * 36) + 0x10; + phyaddr = GPIO_BASE_MAP + (bank * 0x24) + 0x10; } + #endif #ifdef CONFIG_CLOCKWORKPI_A06 - unsigned int phyaddr; + if(bank == 0) phyaddr = GPIO0_BASE + GPIO_EXT_PORTA_OFFSET; else if(bank == 1) @@ -486,6 +535,7 @@ int CPi_digitalRead(int pin) phyaddr = GPIO3_BASE + GPIO_EXT_PORTA_OFFSET; else if(bank == 4) phyaddr = GPIO4_BASE + GPIO_EXT_PORTA_OFFSET; + #endif if (CPI_PIN_MASK[bank][index] != -1) { @@ -493,7 +543,7 @@ int CPi_digitalRead(int pin) val = val >> index; val &= 1; if (wiringPiDebug) - printf("Read reg val: 0x%#x, bank:%d, index:%d\n", val, bank, index); + printf("Read reg val: 0x%#x, bank:%d, index:%d phyaddr: 0x%x\n", val, bank, index, phyaddr); return val; } return 0; @@ -504,9 +554,15 @@ int CPiSetup(int fd) { #ifdef CONFIG_CLOCKWORKPI_A04 - + gpio_base = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, GPIOA_BASE); + if ((int32_t)(unsigned long)gpio_base == -1) + return wiringPiFailure(WPI_ALMOST, "wiringPiSetup: mmap (GPIO) failed: %s\n", strerror(errno)); + gpioL_base = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, GPIOL_BASE); + if ((int32_t)(unsigned long)gpioL_base == -1) + return wiringPiFailure(WPI_ALMOST, "wiringPiSetup: mmap (GPIO) failed: %s\n", strerror(errno)); #elif defined(CONFIG_CLOCKWORKPI_A06) + gpio0_base = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO0_BASE); if ((int32_t)(unsigned long)gpio0_base == -1) return wiringPiFailure(WPI_ALMOST, "wiringPiSetup: mmap (GPIO0_BASE) failed: %s\n", strerror(errno)); @@ -535,6 +591,7 @@ int CPiSetup(int fd) pmugrf_base = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, PMUGRF_BASE); if ((int32_t)(unsigned long)pmugrf_base == -1) return wiringPiFailure(WPI_ALMOST, "wiringPiSetup: mmap (PMUGRF_BASE) failed: %s\n", strerror(errno)); + #endif wpimode = WPI_MODE_BCM; } @@ -620,12 +677,12 @@ void CPiBoardId (int *model, int *rev, int *mem, int *maker, int *warranty) #ifdef CONFIG_CLOCKWORKPI_A04 *model = CPI_MODEL_A04; *rev = PI_VERSION_1; - *mem = 3; + *mem = 3; *maker = 3; #elif defined(CONFIG_CLOCKWORKPI_A06) *model = CPI_MODEL_A06; *rev = PI_VERSION_1; - *mem = 4; + *mem = 4; *maker = 3; #endif } diff --git a/Code/devterm_wiringpi_cpi/wiringPi/wiringCPi.h b/Code/devterm_wiringpi_cpi/wiringPi/wiringCPi.h index a4786d3..9cde29c 100755 --- a/Code/devterm_wiringpi_cpi/wiringPi/wiringCPi.h +++ b/Code/devterm_wiringpi_cpi/wiringPi/wiringCPi.h @@ -2,37 +2,33 @@ #define _WIRING_CPI_H #ifdef CONFIG_CLOCKWORKPI_A04 -#define GPIOA_BASE (0x0300B000) -#define GPIO_NUM (0x40) -#define GPIO_BASE_MAP (0x0300B000) -#define MEM_INFO (1024) -#define GPIOL_BASE (0x07022000) -#define GPIOL_BASE_MAP (0x07022000) -#define MAP_SIZE_L (4096 * 1) -#define GPIO_PWM_OP (0x0300A000) +#define GPIOA_BASE 0x0300B000 +#define GPIO_BASE_MAP (GPIOA_BASE + 0x24 * 2) +#define GPIOL_BASE (0x07022000) +#define GPIO_PWM_OP (0x0300A000) +#define GPIO_NUM (256) #endif #ifdef CONFIG_CLOCKWORKPI_A06 -#define GPIO0_BASE 0xff720000 -#define GPIO1_BASE 0xff730000 -#define GPIO2_BASE 0xff780000 -#define GPIO3_BASE 0xff788000 -#define GPIO4_BASE 0xff790000 -#define GPIO_NUM (160) -#define GPIO_SWPORTA_DR_OFFSET 0x00 -#define GPIO_SWPORTA_DDR_OFFSET 0x04 -#define GPIO_EXT_PORTA_OFFSET 0x50 -#define PMUGRF_BASE 0xff320000 -#define GRF_BASE 0xff77e000 -#define CRU_BASE 0xff760000 -#define PMUCRU_BASE 0xff750000 -#define CRU_CLKGATE_CON31_OFFSET 0x037c //bit 3 4 5 -#define PMUCRU_CLKGATE_CON1_OFFSET 0x0104 +#define GPIO0_BASE 0xff720000 +#define GPIO1_BASE 0xff730000 +#define GPIO2_BASE 0xff780000 +#define GPIO3_BASE 0xff788000 +#define GPIO4_BASE 0xff790000 +#define GPIO_SWPORTA_DR_OFFSET 0x00 +#define GPIO_SWPORTA_DDR_OFFSET 0x04 +#define GPIO_EXT_PORTA_OFFSET 0x50 +#define PMUGRF_BASE 0xff320000 +#define GRF_BASE 0xff77e000 +#define CRU_BASE 0xff760000 +#define PMUCRU_BASE 0xff750000 +#define CRU_CLKGATE_CON31_OFFSET 0x037c +#define PMUCRU_CLKGATE_CON1_OFFSET 0x0104 +#define GPIO_NUM (160) #endif -#define MAP_SIZE_L (4*1024) -#define MAP_SIZE MAP_SIZE_L -#define MAP_MASK (MAP_SIZE - 1) +#define MAP_SIZE (4*1024) +#define MAP_MASK (MAP_SIZE - 1) extern int CPi_set_gpio_mode(int pin, int mode); extern int CPi_set_gpio_alt(int pin, int mode); diff --git a/Code/devterm_wiringpi_cpi/wiringPi/wiringPiSPI.c b/Code/devterm_wiringpi_cpi/wiringPi/wiringPiSPI.c index d21ebc4..7cb1241 100644 --- a/Code/devterm_wiringpi_cpi/wiringPi/wiringPiSPI.c +++ b/Code/devterm_wiringpi_cpi/wiringPi/wiringPiSPI.c @@ -109,7 +109,7 @@ int wiringPiSPISetupMode (int channel, int speed, int mode) // Channel can be anything - lets hope for the best // channel &= 1 ; // Channel is 0 or 1 -#ifdef CONFIG_CLOCKWORKPI +#ifdef CONFIG_CLOCKWORKPI_A06 snprintf (spiDev, 31, "/dev/spidev2.%d", channel) ; #else snprintf (spiDev, 31, "/dev/spidev0.%d", channel) ; diff --git a/Code/devterm_wiringpi_cpi/wiringPiD/drcNetCmd.h b/Code/devterm_wiringpi_cpi/wiringPiD/drcNetCmd.h index 23f7dc1..4300396 100644 --- a/Code/devterm_wiringpi_cpi/wiringPiD/drcNetCmd.h +++ b/Code/devterm_wiringpi_cpi/wiringPiD/drcNetCmd.h @@ -35,7 +35,7 @@ #define DRCN_ANALOG_READ 9 -struct drcNetComStruct +typedef struct drcNetComStruct { uint32_t pin ; uint32_t cmd ; diff --git a/Code/thermal_printer/Makefile b/Code/thermal_printer/Makefile index e1bd127..f437190 100644 --- a/Code/thermal_printer/Makefile +++ b/Code/thermal_printer/Makefile @@ -1,13 +1,13 @@ CC = gcc -CFLAGS = -g -Wall -LDFLAUS = -INCLUDES = -LIBS = -lwiringPi -lm -lcrypt -lpthread -lrt +CFLAGS = -O0 -g -Wall +LDFLAGS = +INCLUDES = -I /usr/include/freetype2 +LIBS = -lwiringPi -lm -lcrypt -lpthread -lfreetype -lrt MAIN = devterm_thermal_printer.elf -SRCS = printer.c devterm_thermal_printer.c utils.c +SRCS = printer.c devterm_thermal_printer.c utils.c ftype.c utf8-utils.c OBJS = $(SRCS:.c=.o) .PHONY: depend clean diff --git a/Code/thermal_printer/NotoSansCJK-Regular.ttf b/Code/thermal_printer/NotoSansCJK-Regular.ttf new file mode 100644 index 0000000..271526f Binary files /dev/null and b/Code/thermal_printer/NotoSansCJK-Regular.ttf differ diff --git a/Code/thermal_printer/config.h b/Code/thermal_printer/config.h index 8777c38..c336ee4 100644 --- a/Code/thermal_printer/config.h +++ b/Code/thermal_printer/config.h @@ -1,7 +1,11 @@ #ifndef CONFIG_H #define CONFIG_H -///raspberry pi CM3 +// clang-format off +#include +#include +// clang-format on +/// raspberry pi CM3 #define BCM_GPIO_28 28 #define BCM_GPIO_29 29 @@ -22,27 +26,28 @@ #define BCM_GPIO_44 44 #define BCM_GPIO_45 45 -//PA8-12 UART1 -//#define SPI1_NSS_PIN PA4 //SPI_1 Chip Select pin is PA4. //no use in DevTerm +// PA8-12 UART1 +//#define SPI1_NSS_PIN PA4 //SPI_1 Chip Select pin is PA4. //no use in +//DevTerm -#define VH_PIN BCM_GPIO_40 //ENABLE_VH required,PRT_EN +#define VH_PIN BCM_GPIO_40 // ENABLE_VH required,PRT_EN -#define LATCH_PIN BCM_GPIO_36 //18 -#define PEM_PIN BCM_GPIO_34 // 1 [PS,PAPER] +#define LATCH_PIN BCM_GPIO_36 // 18 +#define PEM_PIN BCM_GPIO_34 // 1 [PS,PAPER] /* -#define PEM_CTL_PIN BCM_GPIO_32 //3 VPS +#define PEM_CTL_PIN BCM_GPIO_32 //3 VPS #define ENABLE_PEM digitalWrite(PEM_CTL_PIN,HIGH) -#define DISABLE_PEM digitalWrite(PEM_CTL_PIN,LOW) +#define DISABLE_PEM digitalWrite(PEM_CTL_PIN,LOW) */ -//DevTerm no VPS IO -//#define PEM_CTL_PIN -#define ENABLE_PEM -#define DISABLE_PEM +// DevTerm no VPS IO +//#define PEM_CTL_PIN +#define ENABLE_PEM +#define DISABLE_PEM // https://www.raspberrypi.org/documentation/hardware/raspberrypi/spi/README.md -//enable SPI0 ALT0 in CM3 first -// in /boot/config.txt +// enable SPI0 ALT0 in CM3 first +// in /boot/config.txt // dtparam=spi=on // dtoverlay=spi-gpio35-39 // then we can see GPIO38 GPIO39 in ALT0 Mode by `gpio readall` @@ -50,74 +55,68 @@ #define MOSI_PIN BCM_GPIO_38 #define CLK_PIN BCM_GPIO_39 - /** - *@brief STB_NUMBER stand for STROBE NUMBER of lines, which means how many lines - * are going to be activated -**/ -#define STB_NUMBER 1 - -#define STB1_PIN BCM_GPIO_37//13 -#define STB2_PIN STB1_PIN -#define STB3_PIN STB1_PIN -#define STB4_PIN STB1_PIN -#define STB5_PIN STB1_PIN -#define STB6_PIN STB1_PIN + *@brief STB_NUMBER stand for STROBE NUMBER of lines, which means how many lines + * are going to be activated + **/ +#define STB_NUMBER 1 +#define STB1_PIN BCM_GPIO_37 // 13 +#define STB2_PIN STB1_PIN +#define STB3_PIN STB1_PIN +#define STB4_PIN STB1_PIN +#define STB5_PIN STB1_PIN +#define STB6_PIN STB1_PIN #define PH1_PIN BCM_GPIO_28 #define PH2_PIN BCM_GPIO_29 #define PH3_PIN BCM_GPIO_30 #define PH4_PIN BCM_GPIO_31 -///0 1 3 2 mine -#define PA_PIN PH1_PIN // +/// 0 1 3 2 mine +#define PA_PIN PH1_PIN // #define PNA_PIN PH2_PIN // -#define PB_PIN PH3_PIN // +#define PB_PIN PH3_PIN // #define PNB_PIN PH4_PIN // - - //#define ENABLE1_PIN PA13 //#define ENABLE2_PIN PA14 -#define THERMISTORPIN BCM_GPIO_35 //ADC,14 - +#define THERMISTORPIN BCM_GPIO_35 // ADC,14 #define MOTOR_ENABLE1 -#define MOTOR_ENABLE2 +#define MOTOR_ENABLE2 + +#define MOTOR_DISABLE1 +#define MOTOR_DISABLE2 -#define MOTOR_DISABLE1 -#define MOTOR_DISABLE2 - #define ENABLE_VH digitalWrite(VH_PIN, HIGH) #define DISABLE_VH digitalWrite(VH_PIN, LOW) #define LATCH_ENABLE digitalWrite(LATCH_PIN, LOW) #define LATCH_DISABLE digitalWrite(LATCH_PIN, HIGH) -#define ASK4PAPER digitalRead(PEM_PIN) +#define ASK4PAPER digitalRead(PEM_PIN) +#define ERROR_FEED_PITCH ((uint8_t)0x01) +#define IS_PAPER 0x00 +#define NO_PAPER 0x01 +#define HOT_PRINTER 0x02 -#define ERROR_FEED_PITCH ((uint8_t) 0x01) -#define IS_PAPER 0x00 -#define NO_PAPER 0x01 -#define HOT_PRINTER 0x02 - -#define FORWARD 0x01 -#define BACKWARD 0x00 +#define FORWARD 0x01 +#define BACKWARD 0x00 #define HOT 64 -#define BCoefficent 3950 -#define RthNominal 30000 -#define TempNominal 25 -#define ADCResolution 1024 -#define SeriesResistor 30000 -#define NumSamples 1 +#define BCoefficent 3950 +#define RthNominal 30000 +#define TempNominal 25 +#define ADCResolution 1024 +#define SeriesResistor 30000 +#define NumSamples 1 -#define KELVIN 1 -#define CELSIUS 0 +#define KELVIN 1 +#define CELSIUS 0 #define ADC_FILE_PAT "/tmp/devterm_adc" @@ -126,28 +125,26 @@ #define BAT_CAP "/sys/class/power_supply/axp20x-battery/capacity" #define BAT_THRESHOLD 14 // %14 battery = low power -#define int16 uint16_t -#define int8 uint8_t - -#define asciistart ((uint8_t)'A') -#define netxcharacter ((uint8_t)24) -#define Fontrows ((uint8_t)24) -#define FontColums ((uint8_t)16) - -#define nextcharactercolum ((uint8_t)Fontrows/8) // = 3 +#define int16 uint16_t +#define int8 uint8_t +#define asciistart ((uint8_t)'A') +#define netxcharacter ((uint8_t)24) +#define Fontrows ((uint8_t)24) +#define FontColums ((uint8_t)16) +#define nextcharactercolum ((uint8_t)Fontrows / 8) // = 3 #define ASCII_TAB '\t' // Horizontal tab -#define ASCII_LF '\n' // Line feed,10 -#define ASCII_FF '\f' // Form feed -#define ASCII_CR '\r' // Carriage return -#define ASCII_EOT 4 // End of Transmission -#define ASCII_DLE 16 // Data Link Escape -#define ASCII_DC2 18 // Device control 2 //0x12 -#define ASCII_ESC 27 // Escape //0x1b -#define ASCII_FS 28 // Field separator//0x1c -#define ASCII_GS 29 // Group separator //0x1d +#define ASCII_LF '\n' // Line feed,10 +#define ASCII_FF '\f' // Form feed +#define ASCII_CR '\r' // Carriage return +#define ASCII_EOT 4 // End of Transmission +#define ASCII_DLE 16 // Data Link Escape +#define ASCII_DC2 18 // Device control 2 //0x12 +#define ASCII_ESC 27 // Escape //0x1b +#define ASCII_FS 28 // Field separator//0x1c +#define ASCII_GS 29 // Group separator //0x1d #define PRINT_STATE 0 #define ESC_STATE 1 @@ -161,7 +158,6 @@ #define BITS8 8 - #define MAX_DOTS 384 #define PRINTER_BITS MAX_DOTS #define MAXPIXELS 48 @@ -172,75 +168,81 @@ #define MAXPIXELS 72 #endif +#define FONT_MODE_0 0 // Internal +#define FONT_MODE_1 1 // External -//extract bits -#define LAST(k,n) ((k) & ((1<<(n))-1)) -#define MID(k,m,n) LAST((k)>>(m),((n)-(m))) +// extract bits +#define LAST(k, n) ((k) & ((1 << (n)) - 1)) +#define MID(k, m, n) LAST((k) >> (m), ((n) - (m))) - -typedef struct _Margin{ +typedef struct _Margin { uint16_t width; uint8_t esgs; -}Margin; - +} Margin; typedef struct _FONT { - uint8_t width;//in bits + uint8_t width; // in bits uint8_t height; + uint8_t mode; // 0 internal pcf font array,1 external ttf font file const uint8_t *data; -}FONT; + char *file; +} FONT; + +typedef struct _ImageCache { -typedef struct _ImageCache{ - uint16_t idx; uint16_t num; uint16_t width; uint16_t height; - uint8_t need_print:1; - uint8_t revert_bits:1;//MSB OR LSB + uint8_t need_print : 1; + uint8_t revert_bits : 1; // MSB OR LSB uint8_t cache[IMAGE_MAX]; // 48x192bytes(384x192 pixels) ,max - -}ImageCache; -typedef struct _CONFIG -{ +} ImageCache; + +typedef struct _CONFIG { uint8_t state; uint8_t line_space; uint8_t align; - uint8_t reverse; //reverse print + uint8_t reverse; // reverse print uint8_t orient; uint8_t under_line; uint8_t feed_pitch; - uint8_t density:4; //0-f,300+density*46 HEAT_TIME + uint8_t density : 4; // 0-f,300+density*46 HEAT_TIME - uint16_t wordgap:10;//1023 max - uint8_t max_pts;// max pts in print_dots_8bit_split + uint16_t wordgap : 10; // 1023 max + uint8_t max_pts; // max pts in print_dots_8bit_split uint8_t lock; - + int16_t degree; // char rotate 0,90,180,270, -90,-180,-270 + Margin margin; - FONT*font; + FONT *font; ImageCache *img; - FILE*fp; + FILE *fp; - int (*printf)(struct _CONFIG*, char*, ...); + FT_Face face; + FT_Library ft; -}CONFIG; + int (*printf)(struct _CONFIG *, char *, ...); +} CONFIG; -typedef struct _SerialCache{ +typedef struct _SerialCache { uint8_t idx; - uint8_t data[77];//384/5 -}SerialCache; + uint8_t utf8idx; // 0-4 + // uint8_t data[77];//384/5, minium size font 5 pixel + uint32_t data[MAX_DOTS]; // uint32_t for utf8 characters +} SerialCache; -typedef struct _TimeRec{ +typedef struct _TimeRec { unsigned int time; uint8_t last_status; uint8_t check; - -}TimeRec; -void PrintDots8bit(uint8_t *Array, uint8_t characters,uint8_t feed_num); +} TimeRec; + +void PrintDots8bit(uint8_t *Array, uint8_t characters, uint8_t feed_num); uint8_t invert_bit(uint8_t a); diff --git a/Code/thermal_printer/devterm_thermal_printer.c b/Code/thermal_printer/devterm_thermal_printer.c index ebd9cbd..d6c36fc 100644 --- a/Code/thermal_printer/devterm_thermal_printer.c +++ b/Code/thermal_printer/devterm_thermal_printer.c @@ -1,15 +1,15 @@ // for ltp02-245 -// 203 dpi, 384dots in 48mm(1.88976inch) every dots == 0.125mm, 1byte==8dots==1mm -// make clean && make && ./*.elf +// 203 dpi, 384dots in 48mm(1.88976inch) every dots == 0.125mm, +// 1byte==8dots==1mm make clean && make && ./*.elf //#include +#include +#include +#include #include #include -#include #include -#include #include #include -#include #include @@ -19,15 +19,15 @@ #include "pcf_6x12-ISO8859-1_6x12.h" #include "pcf_7x14-ISO8859-1_7x14.h" - #include "ttf_Px437_PS2thin1_8x16.h" #include "ttf_Px437_PS2thin2_8x16.h" - - #include "config.h" -#include "utils.h" #include "printer.h" +#include "utils.h" + +#include "ftype.h" +#include SerialCache ser_cache; @@ -43,76 +43,107 @@ ImageCache img_cache; FONT current_font; +FT_Face face; + +FT_Library ft; + CONFIG g_config; TimeRec battery_chk_tm; -int printf_out(CONFIG*cfg, char *format, ...) { - va_list args; - int rv; +int printf_out(CONFIG *cfg, char *format, ...) { + va_list args; + int rv; - va_start(args, format); - if(cfg->fp != NULL) - rv = vfprintf(cfg->fp, format, args); - else { - rv = -1; - } - va_end(args); + va_start(args, format); + if (cfg->fp != NULL) + rv = vfprintf(cfg->fp, format, args); + else { + rv = -1; + } + va_end(args); - return rv; + return rv; } -//void printer_set_font(CONFIG*cfg,uint8_t fnbits); -//void parse_serial_stream(CONFIG*cfg,uint8_t input_ch); +// void printer_set_font(CONFIG*cfg,uint8_t fnbits); +// void parse_serial_stream(CONFIG*cfg,uint8_t input_ch); -void reset_cmd(){ +void reset_cmd() { cmd_idx = 0; - ser_cache.idx=0; + ser_cache.idx = 0; g_config.state = PRINT_STATE; } -void init_printer(){ - - memset(cmd,0,10); +void init_printer() { + char *error = NULL; + memset(cmd, 0, 10); newline = 0; cmd_idx = 0; - img_cache.idx=0; - img_cache.num=0; - img_cache.width=0; - - img_cache.need_print=0; + img_cache.idx = 0; + img_cache.num = 0; + img_cache.width = 0; + + img_cache.need_print = 0; img_cache.revert_bits = 0; -/* - current_font.width=5; current_font.height=7; current_font.data = font_pcf_5x7_ISO8859_1_5x7; - current_font.width=6;current_font.height=12; current_font.data = font_pcf_6x12_ISO8859_1_6x12; - current_font.width=7; current_font.height=14; current_font.data = font_pcf_7x14_ISO8859_1_7x14; + ser_cache.idx = 0; + ser_cache.utf8idx = 0; - //current_font.width=8;current_font.height=16; current_font.data= font_ttf_Px437_ISO8_8x16; - //current_font.width=8;current_font.height=16; current_font.data= font_ttf_Px437_ISO9_8x16; - - current_font.width=8;current_font.height=16; current_font.data= font_ttf_Px437_PS2thin2_8x16; -*/ - current_font.width=8;current_font.height=16; current_font.data= font_ttf_Px437_PS2thin1_8x16; + /* + current_font.width=5; current_font.height=7; current_font.data = + font_pcf_5x7_ISO8859_1_5x7; current_font.width=6;current_font.height=12; + current_font.data = font_pcf_6x12_ISO8859_1_6x12; current_font.width=7; + current_font.height=14; current_font.data = font_pcf_7x14_ISO8859_1_7x14; + //current_font.width=8;current_font.height=16; current_font.data= + font_ttf_Px437_ISO8_8x16; + //current_font.width=8;current_font.height=16; current_font.data= + font_ttf_Px437_ISO9_8x16; - ser_cache.idx=0; + current_font.width=8;current_font.height=16; current_font.data= + font_ttf_Px437_PS2thin2_8x16; + */ + // auto detect TTF ,switch to FONT_MODE_1 + if (getenv("TTF") && access(getenv("TTF"), F_OK) != -1) { + printf("TTF=%s\n", getenv("TTF")); + current_font.width = 12; + current_font.height = 12; + current_font.data = NULL; + current_font.file = getenv("TTF"); + current_font.mode = FONT_MODE_1; - g_config.line_space=0; + if (init_ft(current_font.file, &face, &ft, current_font.width, + current_font.height, &error)) { + g_config.face = face; + g_config.ft = ft; + } else { + printf("init_ft error %s\n", error); + g_config.face = NULL; + g_config.ft = NULL; + } + } else { + current_font.mode = FONT_MODE_0; + current_font.width = 8; + current_font.height = 16; + current_font.data = font_ttf_Px437_PS2thin2_8x16; + } + + g_config.line_space = 0; g_config.align = ALIGN_LEFT; g_config.reverse = 0; - + g_config.margin.width = 0; - g_config.margin.esgs = 0; - + g_config.margin.esgs = 0; + g_config.orient = FORWARD; g_config.wordgap = 0; g_config.font = ¤t_font; - + g_config.under_line = 0; g_config.state = PRINT_STATE; @@ -123,83 +154,78 @@ void init_printer(){ g_config.feed_pitch = 2; g_config.max_pts = 2; g_config.lock = 0; - - + g_config.degree = 0; + battery_chk_tm.time = millis(); battery_chk_tm.last_status = 0; battery_chk_tm.check = 0; } -const char url[] ={"Powered by clockworkpi.com"}; +const char url[] = {"Powered by clockworkpi.com"}; -void label_print_f(CONFIG*cfg,char*label,float m,char*last){ +void label_print_f(CONFIG *cfg, char *label, float m, char *last) { char buf[48]; - uint8_t i,j; + uint8_t i, j; - - if(m == -1.0) - sprintf(buf,"%s",last); + if (m == -1.0) + sprintf(buf, "%s", last); else - sprintf(buf,"%0.2f%s",m,last); - + sprintf(buf, "%0.2f%s", m, last); + j = strlen(buf); - i = 48-strlen(label)-j-1; + i = 48 - strlen(label) - j - 1; - if(m == -1.0) - sprintf(buf,"%s%*s%s",label,i,"",last); + if (m == -1.0) + sprintf(buf, "%s%*s%s", label, i, "", last); else - sprintf(buf,"%s%*s%0.2f%s",label,i,"",m,last); - - - printer_set_font(cfg,4); + sprintf(buf, "%s%*s%0.2f%s", label, i, "", m, last); + + printer_set_font(cfg, 4); reset_cmd(); - for(i=0;idensity = 6; - - k = (os120_width/8)*os120_height; - memcpy(cfg->img->cache,os120_bits,k); - cfg->img->width = os120_width/8; + + k = (os120_width / 8) * os120_height; + memcpy(cfg->img->cache, os120_bits, k); + cfg->img->width = os120_width / 8; cfg->img->num = k; - cfg->img->revert_bits=1; + cfg->img->revert_bits = 1; cfg->align = ALIGN_CENTER; print_image8(cfg); - - cfg->img->revert_bits=0; + + cfg->img->revert_bits = 0; cfg->align = ALIGN_LEFT; - feed_pitch1(15,cfg->orient); + feed_pitch1(15, cfg->orient); cfg->align = ALIGN_CENTER; /* //selftest1 for(i=0;i<5;i++){ printer_set_font(cfg,0); reset_cmd(); - for(j=0;jalign = ALIGN_LEFT; - - feed_pitch1(32,cfg->orient); -//--------------------------------------------- + feed_pitch1(32, cfg->orient); - for(i=1;i<4;i++){ - printer_set_font(cfg,0); + //--------------------------------------------- + + for (i = 1; i < 4; i++) { + printer_set_font_mode(cfg, FONT_MODE_0); + printer_set_font(cfg, 0); reset_cmd(); - for(j=0;jorient); + parse_serial_stream(cfg, 10); + // Serial.println(); + feed_pitch1(48, cfg->orient); } - - printer_set_font(cfg,0); + printer_set_font_mode(cfg, FONT_MODE_0); + printer_set_font(cfg, 0); reset_cmd(); - for(j=0;jorient); + parse_serial_stream(cfg, 10); - - printer_set_font(cfg,0); + printer_set_font_mode(cfg, FONT_MODE_0); + printer_set_font(cfg, 0); reset_cmd(); - for(j=0;jorient); + parse_serial_stream(cfg, 10); + // Serial.println(); + feed_pitch1(48, cfg->orient); -//------------------------------------------- + printer_set_font_mode(cfg, FONT_MODE_0); + printer_set_font(cfg, 0); + reset_cmd(); + for (j = 0; j < strlen(font_names[0]); j++) { + parse_serial_stream(cfg, font_names[4][j]); + } + parse_serial_stream(cfg, 10); + + printer_set_font_mode(cfg, FONT_MODE_0); + printer_set_font(cfg, 4); + reset_cmd(); + for (ch = 33; ch < 127; ch++) { + parse_serial_stream(cfg, ch); + // Serial.print(ch,DEC); + } + parse_serial_stream(cfg, 10); + // Serial.println(); + feed_pitch1(28, cfg->orient); + + //------------------------------------------- k = temperature(); - label_print_i(cfg,"Temperature:",k," C"); -//-------------------------------------------------------- + label_print_i(cfg, "Temperature:", k, " C"); + //-------------------------------------------------------- - label_print_i(cfg,"Darkness setting:",cfg->density," (Light 0-15 Dark)"); - - label_print_i(cfg,"Paper width:",-1,"58mm"); - label_print_i(cfg,"Print width:",-1,"48mm"); - - label_print_i(cfg,"Baudrate:",115200,""); - label_print_i(cfg,"Data bits:",8,""); - label_print_i(cfg,"Stop bits:",1,""); -//------------------------------------------ + label_print_i(cfg, "Darkness setting:", cfg->density, " (Light 0-15 Dark)"); -//------------------------------------------ - label_print_f(cfg,"Firmware version:",0.1,""); - - feed_pitch1(cfg->font->height,cfg->orient); -//-------------------------------------------------------------- - printer_set_font(cfg,0); + label_print_i(cfg, "Paper width:", -1, "58mm"); + label_print_i(cfg, "Print width:", -1, "48mm"); + + label_print_i(cfg, "Baudrate:", 115200, ""); + label_print_i(cfg, "Data bits:", 8, ""); + label_print_i(cfg, "Stop bits:", 1, ""); + //------------------------------------------ + + //------------------------------------------ + label_print_f(cfg, "Firmware version:", 0.1, ""); + + feed_pitch1(cfg->font->height, cfg->orient); + //-------------------------------------------------------------- + printer_set_font_mode(cfg, FONT_MODE_0); + printer_set_font(cfg, 0); reset_cmd(); - - //Serial.println(strlen(url),DEC); - for(i=0;ifont->height * 2, cfg->orient); +} + +void printer_set_font_mode(CONFIG *cfg, int mode) { + cfg->font->mode = mode; + return; +} +void printer_set_font(CONFIG *cfg, uint8_t fnbits) { + uint8_t ret; + ret = MID(fnbits, 0, 3); + + if (cfg->font->mode == FONT_MODE_0) { + if (ret == 0) { + cfg->font->width = 8; + cfg->font->height = 16; + cfg->font->data = font_ttf_Px437_PS2thin1_8x16; + } + + if (ret == 1) { + cfg->font->width = 5; + cfg->font->height = 7; + cfg->font->data = font_pcf_5x7_ISO8859_1_5x7; + } + + if (ret == 2) { + cfg->font->width = 6; + cfg->font->height = 12; + cfg->font->data = font_pcf_6x12_ISO8859_1_6x12; + } + + if (ret == 3) { + cfg->font->width = 7; + cfg->font->height = 14; + cfg->font->data = font_pcf_7x14_ISO8859_1_7x14; + } + + if (ret == 4) { + cfg->font->width = 8; + cfg->font->height = 16; + cfg->font->data = font_ttf_Px437_PS2thin2_8x16; } } -*/ -//-------------------------------------------------------- - feed_pitch1(cfg->font->height*2,cfg->orient); - + + if (cfg->font->mode == FONT_MODE_1) { + if (ret == 0) { + cfg->font->width = 12; + cfg->font->height = 12; + } + if (ret == 1) { + cfg->font->width = 14; + cfg->font->height = 14; + } + if (ret == 2) { + cfg->font->width = 16; + cfg->font->height = 16; + } + if (ret == 3) { + cfg->font->width = 18; + cfg->font->height = 18; + } + + if (ret == 4) { + cfg->font->width = 20; + cfg->font->height = 20; + } + change_ft_size(cfg->face, cfg->font->width, cfg->font->height); + } } -void printer_set_font(CONFIG*cfg,uint8_t fnbits){ - uint8_t ret; - ret = MID(fnbits,0,3); - - if(ret==0) { - cfg->font->width = 8 ; - cfg->font->height = 16; - cfg->font->data = font_ttf_Px437_PS2thin1_8x16; - } - - if(ret==1){ - cfg->font->width = 5; - cfg->font->height = 7; - cfg->font->data = font_pcf_5x7_ISO8859_1_5x7; - } - - if(ret==2){ - cfg->font->width = 6; - cfg->font->height = 12; - cfg->font->data = font_pcf_6x12_ISO8859_1_6x12; - } - - if(ret==3){ - cfg->font->width = 7; - cfg->font->height = 14; - cfg->font->data = font_pcf_7x14_ISO8859_1_7x14; - } - - if(ret == 4){ - cfg->font->width = 8 ; - cfg->font->height = 16; - cfg->font->data = font_ttf_Px437_PS2thin2_8x16; - } -} +void parse_cmd(CONFIG *cfg, uint8_t *cmd, uint8_t cmdidx) { -void parse_cmd(CONFIG*cfg,uint8_t *cmd, uint8_t cmdidx){ - uint8_t ret; - - if(cmdidx >1){ - //ESC 2 - if(cmd[0] == ASCII_ESC && cmd[1] == 0x32){ - cfg->line_space = cfg->font->height+8; + if (cmdidx > 1) { + // ESC 2 + if (cmd[0] == ASCII_ESC && cmd[1] == 0x32) { + cfg->line_space = cfg->font->height + 8; reset_cmd(); } - //ESC @ - if(cmd[0] == ASCII_ESC && cmd[1] == 0x40){ + // ESC @ + if (cmd[0] == ASCII_ESC && cmd[1] == 0x40) { init_printer(); reset_cmd(); - } - //DC2 T printer test page - if(cmd[0] == ASCII_DC2 && cmd[1] == 0x54){ + // DC2 T printer test page + if (cmd[0] == ASCII_DC2 && cmd[1] == 0x54) { reset_cmd(); printer_test(cfg); - } } - - if(cmdidx > 2){ - //ESC j n - if(cmd[0] == ASCII_ESC && cmd[1] == 0x4a){ - - if( print_lines8(cfg) == 0) { feed_pitch1(cmd[2],cfg->orient); } - reset_cmd(); - } - //ESC d n - if(cmd[0] == ASCII_ESC && cmd[1] == 0x64){ - - if( print_lines8(cfg) == 0) { feed_pitch1(cmd[2]*cfg->font->height,cfg->orient); } - reset_cmd(); - } - //ESC ! n - if(cmd[0] == ASCII_ESC && cmd[1] == 0x21){ - printer_set_font(cfg,cmd[2]); - reset_cmd(); - } - //ESC 3 n - if(cmd[0] == ASCII_ESC && cmd[1] == 0x33){ - cfg->line_space = cmd[2]; - reset_cmd(); - } - - // ESC v n - if(cmd[0] == ASCII_ESC && cmd[1] == 0x76){ - if(temperature() > 70){ + if (cmdidx > 2) { + // ESC j n + if (cmd[0] == ASCII_ESC && cmd[1] == 0x4a) { + + if (print_lines8(cfg) == 0) { + feed_pitch1(cmd[2], cfg->orient); + } + reset_cmd(); + } + // ESC d n + if (cmd[0] == ASCII_ESC && cmd[1] == 0x64) { + + if (print_lines8(cfg) == 0) { + feed_pitch1(cmd[2] * cfg->font->height, cfg->orient); + } + reset_cmd(); + } + // ESC ! n + if (cmd[0] == ASCII_ESC && cmd[1] == 0x21) { + printer_set_font(cfg, cmd[2]); + reset_cmd(); + } + + // ESC 3 n + if (cmd[0] == ASCII_ESC && cmd[1] == 0x33) { + cfg->line_space = cmd[2]; + reset_cmd(); + } + + // ESC v n + if (cmd[0] == ASCII_ESC && cmd[1] == 0x76) { + if (temperature() > 70) { ret |= 1 << 6; - } - if ( (IsPaper() & NO_PAPER) > 0 ) { + } + if ((IsPaper() & NO_PAPER) > 0) { ret |= 1 << 2; - } - // 1<< 3 == power error - - cfg->printf(cfg,"%d",ret); - reset_cmd(); - return; - } - //ESC a n - if(cmd[0] == ASCII_ESC && cmd[1] == 0x61){ - ret = cmd[2]; - if(ret == 0 || ret == 48){ - cfg->align = ALIGN_LEFT; - } - if(ret == 1 || ret == 49){ - cfg->align = ALIGN_CENTER; - } - if(ret == 2 || ret == 50){ - cfg->align = ALIGN_RIGHT; - } - - reset_cmd(); - } - //ESC - n - if(cmd[0] == ASCII_ESC && cmd[1] == 0x2d){ - ret = cmd[2]; - if(ret == 0 || ret == 48){ - cfg->under_line =0; - } - if(ret == 1 || ret == 49){ - cfg->under_line = 1; - } - if(ret == 2 || ret == 50){ - cfg->under_line = 2; - } - - reset_cmd(); - - } + } + // 1<< 3 == power error - //ESC SP n - if(cmd[0] == ASCII_ESC && cmd[1] == 0x20){ - ret = cmd[2]; - if( ret + cfg->margin.width wordgap = ret; - } - reset_cmd(); - return; - } + cfg->printf(cfg, "%d", ret); + reset_cmd(); + return; + } + // ESC a n + if (cmd[0] == ASCII_ESC && cmd[1] == 0x61) { + ret = cmd[2]; + if (ret == 0 || ret == 48) { + cfg->align = ALIGN_LEFT; + } + if (ret == 1 || ret == 49) { + cfg->align = ALIGN_CENTER; + } + if (ret == 2 || ret == 50) { + cfg->align = ALIGN_RIGHT; + } - //DC2 # n - if(cmd[0] == ASCII_DC2 && cmd[1] == 0x23){ - ret = cmd[2]; - cfg->density = ret; - reset_cmd(); - } + reset_cmd(); + } + // ESC - n + if (cmd[0] == ASCII_ESC && cmd[1] == 0x2d) { + ret = cmd[2]; + if (ret == 0 || ret == 48) { + cfg->under_line = 0; + } + if (ret == 1 || ret == 49) { + cfg->under_line = 1; + } + if (ret == 2 || ret == 50) { + cfg->under_line = 2; + } + reset_cmd(); + } - //GS V \0 or GS V \1 - if(cmd[0] == ASCII_GS && cmd[1] == 0x56){ - - ret = cmd[2]; - reset_cmd();//When using parse_serial_stream function internally, reset_cmd() first - - print_cut_line(cfg); - - return; - } - + // ESC SP n + if (cmd[0] == ASCII_ESC && cmd[1] == 0x20) { + ret = cmd[2]; + if (ret + cfg->margin.width < MAX_DOTS) { + cfg->wordgap = ret; + } + reset_cmd(); + return; + } + + // DC2 # n + if (cmd[0] == ASCII_DC2 && cmd[1] == 0x23) { + ret = cmd[2]; + cfg->density = ret; + reset_cmd(); + } + + // GS V \0 or GS V \1 + if (cmd[0] == ASCII_GS && cmd[1] == 0x56) { + + ret = cmd[2]; + reset_cmd(); // When using parse_serial_stream function internally, + // reset_cmd() first + + print_cut_line(cfg); + + return; + } + + // ESC V n + if (cmd[0] == ASCII_ESC && cmd[1] == 0x56) { + ret = cmd[2]; + if (ret == 0x00 || ret == 0x30) { + cfg->degree = 0; + } + if (ret == 0x01 || ret == 0x31) { + cfg->degree = 90; + } + if (ret == 0x02 || ret == 0x32) { + cfg->degree = 180; + } + + if (ret == 0x03 || ret == 0x33) { + cfg->degree = 270; + } + printf("set printer degree, %d\n", cfg->degree); + return; + } } - if(cmdidx > 3){ - //GS L nL nH - if(cmd[0] == ASCII_GS && cmd[1] == 0x4c){ + if (cmdidx > 3) { + // GS L nL nH + if (cmd[0] == ASCII_GS && cmd[1] == 0x4c) { uint16_t k; - k = cmd[2]+cmd[3]*256; - if (k < MAX_DOTS - cfg->font->width){ + k = cmd[2] + cmd[3] * 256; + if (k < MAX_DOTS - cfg->font->width) { cfg->margin.width = k; } reset_cmd(); } - } - if(cmdidx > 4){ + if (cmdidx > 4) { /* if(cmd[0] == ASCII_ESC && cmd[1] == 0x2a){ if( cmd[2] == 0 || cmd[2] == 1){ @@ -567,106 +643,141 @@ void parse_cmd(CONFIG*cfg,uint8_t *cmd, uint8_t cmdidx){ cfg->img->idx = 0; } } - + reset_cmd(); } */ } - if(cmdidx > 7){ - // GS v 0 p wL wH hL hH d1…dk - if(cmd[0] == ASCII_GS && cmd[1] == 118 && cmd[2] == 48 ) { - uint16_t width = cmd[4] + cmd[5]*256; - uint16_t height = cmd[6]+cmd[7]*256; - uint16_t k; - k = width * height; - if(k<= IMAGE_MAX){ + if (cmdidx > 7) { + // GS v 0 p wL wH hL hH d1…dk + if (cmd[0] == ASCII_GS && cmd[1] == 118 && cmd[2] == 48) { + uint16_t width = cmd[4] + cmd[5] * 256; + uint16_t height = cmd[6] + cmd[7] * 256; + uint16_t k; + k = width * height; + if (k <= IMAGE_MAX) { cfg->state = GET_IMAGE; cfg->img->num = k; cfg->img->idx = 0; cfg->img->width = width; - cfg->img->need_print=1; - - } - // do not reset_cmd() - cmd_idx = 0; - ser_cache.idx=0; - + cfg->img->need_print = 1; + } + // do not reset_cmd() + cmd_idx = 0; + ser_cache.idx = 0; } } - } -void parse_serial_stream(CONFIG*cfg,uint8_t input_ch){ +void parse_serial_stream(CONFIG *cfg, uint8_t input_ch) { uint16_t a; - - if(cfg->state == GET_IMAGE){ - cfg->img->cache[cfg->img->idx] = input_ch; - cfg->img->idx++; - if(cfg->img->idx >= cfg->img->num){//image full - if(cfg->img->need_print==1){ - print_image8(cfg); + uint8_t bskip; + + if (cfg->state == GET_IMAGE) { + cfg->img->cache[cfg->img->idx] = input_ch; + cfg->img->idx++; + if (cfg->img->idx >= cfg->img->num) { // image full + if (cfg->img->need_print == 1) { + print_image8(cfg); + } + reset_cmd(); + cfg->state = PRINT_STATE; + } + } else if (cfg->state == ESC_STATE) { + cmd[cmd_idx] = input_ch; + cmd_idx++; + if (cmd_idx < 10) { + parse_cmd(cfg, cmd, cmd_idx); + } else { + reset_cmd(); + } + } else { // PRINT_STATE + switch (input_ch) { + case ASCII_LF: + if (ser_cache.idx == 0) { + feed_pitch1(cfg->font->height, cfg->orient); + } + print_lines8(cfg); + reset_cmd(); + break; + case ASCII_FF: + + print_lines8(cfg); + reset_cmd(); + break; + case ASCII_DC2: + cmd[cmd_idx] = input_ch; + cfg->state = ESC_STATE; + cmd_idx++; + break; + case ASCII_GS: + cmd[cmd_idx] = input_ch; + cfg->state = ESC_STATE; + cmd_idx++; + break; + case ASCII_ESC: + cmd[cmd_idx] = input_ch; + cfg->state = ESC_STATE; + cmd_idx++; + break; + default: + if (input_ch < 128) { + ser_cache.data[ser_cache.idx] = input_ch; + ser_cache.idx++; + } else { // utf8 + // 10xxxxxx bskip == 1 + bskip = get_slice_len(input_ch); + + if (bskip == 1) { + // append this to int32_t [8:8:8:8] 0xffffffff 4294967295 + ser_cache.data[ser_cache.idx] |= input_ch + << (8 * (ser_cache.utf8idx)); + if (ser_cache.utf8idx == + get_slice_len(ser_cache.data[ser_cache.idx] & 0xff) - 1) { + ser_cache.idx++; + ser_cache.utf8idx = 0; // next character + } else { + ser_cache.utf8idx++; + } + } + + if (bskip > 1) { + ser_cache.utf8idx = 1; + ser_cache.data[ser_cache.idx] = input_ch; + } + } + // read utf8 codename + // + if (cfg->font->mode == FONT_MODE_1) { + + a = get_serial_cache_font_width(&g_config); + a += (ser_cache.idx) * 0 + g_config.margin.width; + + } else { + a = (ser_cache.idx + 1) * current_font.width + (ser_cache.idx) * 0 + + g_config.margin.width; + } + if (a >= MAX_DOTS) // got enough points to print + { + if (cfg->font->mode == FONT_MODE_1) { + print_lines_ft(cfg); + } else { + print_lines8(cfg); } reset_cmd(); - cfg->state = PRINT_STATE; - } - }else if(cfg->state == ESC_STATE) { - cmd[cmd_idx] = input_ch; - cmd_idx++; - if(cmd_idx < 10){ - parse_cmd(cfg,cmd,cmd_idx); - }else{ - reset_cmd(); - } - }else{ //PRINT_STATE - switch(input_ch){ - case ASCII_LF: - if(ser_cache.idx == 0){ - feed_pitch1(cfg->font->height,cfg->orient); - } - print_lines8(cfg); - reset_cmd(); - break; - case ASCII_FF: - print_lines8(cfg); - reset_cmd(); - break; - case ASCII_DC2: - cmd[cmd_idx] = input_ch; - cfg->state = ESC_STATE; - cmd_idx++; - break; - case ASCII_GS: - cmd[cmd_idx] = input_ch; - cfg->state = ESC_STATE; - cmd_idx++; - break; - case ASCII_ESC: - cmd[cmd_idx] = input_ch; - cfg->state = ESC_STATE; - cmd_idx++; - break; - default: - ser_cache.data[ser_cache.idx]=input_ch; - ser_cache.idx++; - - a = (ser_cache.idx+1)*current_font.width+(ser_cache.idx)*0+ g_config.margin.width; - if( a >= MAX_DOTS) - { - print_lines8(cfg); - reset_cmd(); - } - break; } + break; } - + } } /* Virtual serial port created by socat - socat -d -d pty,link=/tmp/DEVTERM_PRINTER_OUT,raw,echo=0 pty,link=/tmp/DEVTERM_PRINTER_IN,raw,echo=0 + socat -d -d pty,link=/tmp/DEVTERM_PRINTER_OUT,raw,echo=0 + pty,link=/tmp/DEVTERM_PRINTER_IN,raw,echo=0 */ -int read_bat_cap(CONFIG*cfg) { +int read_bat_cap(CONFIG *cfg) { long ret; char c[12]; FILE *fptr; @@ -677,101 +788,93 @@ int read_bat_cap(CONFIG*cfg) { } fscanf(fptr, "%[^\n]", c); - //printf("Data from the file:%s\n", c); + // printf("Data from the file:%s\n", c); fclose(fptr); - ret = strtol(c, NULL, 10); - + ret = strtol(c, NULL, 10); + return (int)ret; - } -int bat_cap_to_pts(CONFIG*cfg,int bat) { +int bat_cap_to_pts(CONFIG *cfg, int bat) { int pts; - - pts = (int)round( (float)bat/10.0 ) + 1; + + pts = (int)round((float)bat / 10.0) + 1; return pts; } +void print_lowpower(CONFIG *cfg) { -void print_lowpower(CONFIG*cfg) { - int i; - char*msg = "Low Power,please charge"; + char *msg = "Low Power,please charge"; reset_cmd(); - printer_set_font(cfg,4); - - for(i=0;iorient); - printer_set_font(cfg,0); - - PRINTF("%s\n",msg); + feed_pitch1(128, cfg->orient); + printer_set_font(cfg, 0); + PRINTF("%s\n", msg); } -int check_battery(CONFIG*cfg){ +int check_battery(CONFIG *cfg) { int bat_cap; - + bat_cap = 0; - - if( millis() - battery_chk_tm.time > 200) { - + + if (millis() - battery_chk_tm.time > 200) { + bat_cap = read_bat_cap(cfg); - - if( bat_cap < 0){ - cfg->max_pts = 1;//no battery ,so set to the slowest - cfg->lock = 0;// unlock printer anyway + + if (bat_cap < 0) { + cfg->max_pts = 1; // no battery ,so set to the slowest + cfg->lock = 0; // unlock printer anyway PRINTF("no battery,slowest printing\n"); } - - if(bat_cap >=0 && bat_cap <= BAT_THRESHOLD) { - - if(cfg->lock == 0) { + + if (bat_cap >= 0 && bat_cap <= BAT_THRESHOLD) { + + if (cfg->lock == 0) { print_lowpower(cfg); - } - cfg->lock =1; + cfg->lock = 1; PRINTF("printer locked\n"); reset_cmd(); // clear all serial_cache - }else { - - cfg->max_pts = bat_cap_to_pts(cfg,bat_cap); - + } else { + + cfg->max_pts = bat_cap_to_pts(cfg, bat_cap); + cfg->lock = 0; } - + battery_chk_tm.time = millis(); - } - - if(cfg->lock == 1) { + + if (cfg->lock == 1) { reset_cmd(); } return 0; - } #define FIFO_FILE "/tmp/DEVTERM_PRINTER_OUT" void loop() { - + /* if (Serial.available() > 0) { // read the incoming byte @@ -779,66 +882,61 @@ void loop() { parse_serial_stream(&g_config,buf[0]); } */ - - FILE*fp=NULL; + + FILE *fp = NULL; char readbuf[2]; - - while(1) { + + while (1) { fp = NULL; - fp = fopen(FIFO_FILE,"r+b"); + fp = fopen(FIFO_FILE, "r+b"); if (fp != NULL) { - g_config.fp = fp; - while(1) - { - fread(readbuf,1, 1,fp); + g_config.fp = fp; + while (1) { + fread(readbuf, 1, 1, fp); check_battery(&g_config); - - if(g_config.lock ==0) { - //printf("read %x",readbuf[0]); - if(g_config.state == PRINT_STATE) { - if(readbuf[0] == ASCII_TAB) { - readbuf[0] = ' '; - parse_serial_stream(&g_config,readbuf[0]); - parse_serial_stream(&g_config,readbuf[0]); - } else {//not a tab - parse_serial_stream(&g_config,readbuf[0]); - } - } else { //g_config.state == PRINT_STATE - parse_serial_stream(&g_config,readbuf[0]); - } + + if (g_config.lock == 0) { + // printf("read %x",readbuf[0]); + if (g_config.state == PRINT_STATE) { + if (readbuf[0] == ASCII_TAB) { + readbuf[0] = ' '; + parse_serial_stream(&g_config, readbuf[0]); + parse_serial_stream(&g_config, readbuf[0]); + } else { // not a tab + parse_serial_stream(&g_config, readbuf[0]); + } + } else { // g_config.state != PRINT_STATE + parse_serial_stream(&g_config, readbuf[0]); + } } } fclose(fp); g_config.fp = NULL; } - + sleep(1); } -//------------------------------------------ - //printer_test(&g_config); - + //------------------------------------------ + // printer_test(&g_config); } void setup() { - + wiringPiSetupGpio(); header_init(); header_init1(); - + clear_printer_buffer(); - - //Serial.begin(115200); + + // Serial.begin(115200); init_printer(); - g_config.printf = &printf_out; - + g_config.printf = &printf_out; } -int main(int argc,char**argv) { - - - setup(); - loop(); - +int main(int argc, char **argv) { + + setup(); + loop(); } diff --git a/Code/thermal_printer/etc/systemd/system/devterm-printer.service b/Code/thermal_printer/etc/systemd/system/devterm-printer.service index f4e93ac..0f0812d 100644 --- a/Code/thermal_printer/etc/systemd/system/devterm-printer.service +++ b/Code/thermal_printer/etc/systemd/system/devterm-printer.service @@ -4,6 +4,7 @@ After=devterm-socat [Service] Type=simple +EnvironmentFile=/usr/local/etc/devterm-printer StandardOutput=file:/tmp/devterm_printer.log StandardError=file:/tmp/devterm_printer.err.log ExecStart=/usr/local/bin/devterm_thermal_printer.elf diff --git a/Code/thermal_printer/ftype.c b/Code/thermal_printer/ftype.c new file mode 100644 index 0000000..be56907 --- /dev/null +++ b/Code/thermal_printer/ftype.c @@ -0,0 +1,106 @@ +#include "ftype.h" + +#include "utf8-utils.h" + +static const UTF32 utf32_space[2] = {' ', 0}; + +bool init_ft(const char *ttf_file, FT_Face *face, FT_Library *ft, + int req_size_w, int req_size_h, char **error) { + + bool ret = false; + if (FT_Init_FreeType(ft) == 0) { + if (FT_New_Face(*ft, ttf_file, 0, face) == 0) { + + if (FT_Set_Pixel_Sizes(*face, req_size_w, req_size_h) == 0) { + ret = true; + } else { + if (error) + *error = strdup("Can't set font size"); + } + + } else { + if (error) + *error = strdup("Can't load TTF file"); + } + + } else { + if (error) + *error = strdup("Can't init freetype library"); + } + + return ret; +} + +bool change_ft_size(FT_Face face, int req_size_w, int req_size_h) { + bool ret = false; + if (FT_Set_Pixel_Sizes(face, req_size_w, req_size_h) == 0) { + ret = true; + } else { + printf("Can't set font size"); + } + + return ret; +} +/*=========================================================================== + done_ft + Clean up after we've finished wih the FreeType librar + =========================================================================*/ +void done_ft(FT_Library ft) { FT_Done_FreeType(ft); } + +/*=========================================================================== + + face_get_line_spacing + + Get the nominal line spacing, that is, the distance between glyph + baselines for vertically-adjacent rows of text. This is "nominal" because, + in "real" typesetting, we'd need to add extra room for accents, etc. + + =========================================================================*/ +int face_get_line_spacing(FT_Face face) { + return face->size->metrics.height / 64; + // There are other possibilities the give subtly different results: + // return (face->bbox.yMax - face->bbox.yMin) / 64; + // return face->height / 64; +} + +int get_slice_len(const char lb) { + + if ((lb & 0x80) == 0) + return 1; + else if ((lb & 0xE0) == 0xC0) + return 2; + else if ((lb & 0xF0) == 0xE0) + return 3; + else if ((lb & 0xF8) == 0xF0) + return 4; + return 1; +} + +UTF32 *cjk_utf8_to_utf32(const char *word) { + assert(word != NULL); + int l = strlen(word); + int u8l = utf8_strlen(word); + + char buf[5]; + + UTF32 *ret = malloc((u8l + 1) * sizeof(UTF32)); + int i = 0, j = 0; + int bskip = 1; + + while (i < l) { + + bskip = get_slice_len(word[i]); + strncpy(buf, &word[i], bskip); + if (bskip > 1) { + ret[j] = (UTF32)utf8_to_utf32(buf); + } else { + ret[j] = (UTF32)buf[0]; + } + + j++; + i += bskip; + } + + ret[u8l] = 0; + return ret; +} diff --git a/Code/thermal_printer/ftype.h b/Code/thermal_printer/ftype.h new file mode 100644 index 0000000..af7fddd --- /dev/null +++ b/Code/thermal_printer/ftype.h @@ -0,0 +1,29 @@ +#ifndef FTYPE_H +#define FTYPE_H +// clang-format off +#include +#include +#include +#include +#include + +#include +// clang-format on +#ifndef UTF8 +typedef unsigned char UTF8; +#endif + +#ifndef UTF32 +typedef int32_t UTF32; +#endif + +bool init_ft(const char *ttf_file, FT_Face *face, FT_Library *ft, + int req_size_w, int req_size_h, char **error); + +bool change_ft_size(FT_Face face, int req_size_w, int req_size_h); + +int face_get_line_spacing(FT_Face face); + +int get_slice_len(const char lb); + +#endif diff --git a/Code/thermal_printer/printer.c b/Code/thermal_printer/printer.c index 79687b2..dda7f51 100644 --- a/Code/thermal_printer/printer.c +++ b/Code/thermal_printer/printer.c @@ -1,14 +1,13 @@ +#include +#include +#include #include #include -#include #include -#include -#include #include #include - #include "config.h" #include "utils.h" @@ -18,7 +17,7 @@ extern FONT current_font; extern SerialCache ser_cache; -uint16_t STBx[] = {STB1_PIN,STB2_PIN,STB3_PIN,STB4_PIN,STB5_PIN,STB6_PIN}; +uint16_t STBx[] = {STB1_PIN, STB2_PIN, STB3_PIN, STB4_PIN, STB5_PIN, STB6_PIN}; uint8_t as; static unsigned int printer_vps_time; @@ -27,333 +26,311 @@ static uint8_t printer_temp_check; static char adc_file_path[128]; - -void printer_send_data8(uint8_t w) -{ +void printer_send_data8(uint8_t w) { /* - digitalWrite(SPI1_NSS_PIN, LOW); // manually take CSN low for SPI_1 transmission - SPI.transfer(w); //Send the HEX data 0x55 over SPI-1 port and store the received byte to the variable. + digitalWrite(SPI1_NSS_PIN, LOW); // manually take CSN low for SPI_1 + transmission SPI.transfer(w); //Send the HEX data 0x55 over SPI-1 port and + store the received byte to the variable. //SPI.transfer16(w); - digitalWrite(SPI1_NSS_PIN, HIGH); // manually take CSN high between spi transmissions + digitalWrite(SPI1_NSS_PIN, HIGH); // manually take CSN high between spi + transmissions */ - wiringPiSPIDataRW (0, &w, 1); - + wiringPiSPIDataRW(0, &w, 1); } +void clear_printer_buffer() { + uint8_t i = 0; + + for (i = 0; i < 48; i++) + printer_send_data8(0x00); -void clear_printer_buffer() -{ - uint8_t i= 0; - - for(i=0;i<48;i++) - printer_send_data8(0x00); - LATCH_ENABLE; delayus(1); LATCH_DISABLE; delayus(1); } - -uint8_t IsPaper() -{ +uint8_t IsPaper() { uint8_t status; uint8_t tmp; return IS_PAPER; - - if( millis() - printer_vps_time > 10) { + + if (millis() - printer_vps_time > 10) { ENABLE_PEM; - if(ASK4PAPER==LOW) // * LOW is what we want** - {status = IS_PAPER;} - else - {status = NO_PAPER; PRINTF("Error:NO PAPER\n"); } + if (ASK4PAPER == LOW) // * LOW is what we want** + { + status = IS_PAPER; + } else { + status = NO_PAPER; + PRINTF("Error:NO PAPER\n"); + } DISABLE_PEM; - if(printer_temp_check > 20) { + if (printer_temp_check > 20) { tmp = temperature(); - - if (tmp >= HOT){ + + if (tmp >= HOT) { PRINTF("Printer too Hot\n"); status |= HOT_PRINTER; } - printer_temp_check = 0; + printer_temp_check = 0; - }else { - printer_temp_check++; + } else { + printer_temp_check++; } - }else { + } else { status = printer_vps_last_status; } - - + printer_vps_last_status = status; printer_vps_time = millis(); - + return status; } - uint8_t header_init() { - - uint8_t pin[] = {THERMISTORPIN}; - - uint8_t x; - pinMode(LATCH_PIN,OUTPUT); - for(x=0; x < STB_NUMBER; x++){ - pinMode(STBx[x],OUTPUT); - digitalWrite(STBx[x],LOW); + uint8_t pin[] = {THERMISTORPIN}; + + uint8_t x; + pinMode(LATCH_PIN, OUTPUT); + + for (x = 0; x < STB_NUMBER; x++) { + pinMode(STBx[x], OUTPUT); + digitalWrite(STBx[x], LOW); } - + LATCH_DISABLE; - pinMode(VH_PIN,OUTPUT); - digitalWrite(VH_PIN,LOW); + pinMode(VH_PIN, OUTPUT); + digitalWrite(VH_PIN, LOW); - pinMode(PEM_PIN,INPUT); - //pinMode(PEM_CTL_PIN,OUTPUT); + pinMode(PEM_PIN, INPUT); + // pinMode(PEM_CTL_PIN,OUTPUT); + + // adc.setChannels(pin, 1); //this is actually the pin you want to measure + + pinMode(THERMISTORPIN, INPUT); // 数字io没有 模拟接口。adc 读温度暂时不搞 - - //adc.setChannels(pin, 1); //this is actually the pin you want to measure - - pinMode(THERMISTORPIN,INPUT); // 数字io没有 模拟接口。adc 读温度暂时不搞 - /* //SPI.begin(); //Initialize the SPI_1 port. SPI.setBitOrder(MSBFIRST); // Set the SPI_1 bit order SPI.setDataMode(SPI_MODE0); //Set the SPI_1 data mode 0 - SPI.setClockDivider(SPI_CLOCK_DIV16); // Slow speed (72 / 16 = 4.5 MHz SPI_1 speed) - SPI.setDataSize(DATA_SIZE_8BIT); - SPI.begin(); //Initialize the SPI_1 port. + SPI.setClockDivider(SPI_CLOCK_DIV16); // Slow speed (72 / 16 = 4.5 MHz + SPI_1 speed) SPI.setDataSize(DATA_SIZE_8BIT); SPI.begin(); //Initialize the + SPI_1 port. */ - if (!wiringPiSPISetup (0, 4500000 )) { - PRINTF("SPI init failed,exiting...\n"); + if (!wiringPiSPISetup(0, 4500000)) { + PRINTF("SPI init failed,exiting...\n"); } - + /* - pinMode(SPI1_NSS_PIN, OUTPUT); + pinMode(SPI1_NSS_PIN, OUTPUT); digitalWrite(SPI1_NSS_PIN,HIGH); */ - + printer_vps_time = 0; printer_vps_last_status = IS_PAPER; - printer_temp_check= 0; + printer_temp_check = 0; glob_file(ADC_FILE_PAT); - } - #if 1 -uint8_t current_pos = 1; +uint8_t current_pos = 1; uint8_t header_init1() { - - pinMode(PA_PIN,OUTPUT); - pinMode(PNA_PIN,OUTPUT); - pinMode(PB_PIN,OUTPUT); - pinMode(PNB_PIN,OUTPUT); + + pinMode(PA_PIN, OUTPUT); + pinMode(PNA_PIN, OUTPUT); + pinMode(PB_PIN, OUTPUT); + pinMode(PNB_PIN, OUTPUT); as = 0; return ASK4PAPER; } -void motor_stepper_pos2(uint8_t position)//forward +void motor_stepper_pos2(uint8_t position) // forward { -// position = 9 - position; -// position = (position+1)/2; + // position = 9 - position; + // position = (position+1)/2; delayMicroseconds(6700); - switch(position){ - case 0: - digitalWrite(PA_PIN,LOW); - digitalWrite(PNA_PIN,LOW); - digitalWrite(PB_PIN,LOW); - digitalWrite(PNB_PIN,LOW); - break; - case 1: - digitalWrite(PA_PIN,HIGH); - digitalWrite(PNA_PIN,LOW); - digitalWrite(PB_PIN,LOW); - digitalWrite(PNB_PIN,HIGH); - break; - case 2: - digitalWrite(PA_PIN,HIGH); - digitalWrite(PNA_PIN,LOW); - digitalWrite(PB_PIN,HIGH); - digitalWrite(PNB_PIN,LOW); + switch (position) { + case 0: + digitalWrite(PA_PIN, LOW); + digitalWrite(PNA_PIN, LOW); + digitalWrite(PB_PIN, LOW); + digitalWrite(PNB_PIN, LOW); break; - case 3: - digitalWrite(PA_PIN,LOW); - digitalWrite(PNA_PIN,HIGH); - digitalWrite(PB_PIN,HIGH); - digitalWrite(PNB_PIN,LOW); + case 1: + digitalWrite(PA_PIN, HIGH); + digitalWrite(PNA_PIN, LOW); + digitalWrite(PB_PIN, LOW); + digitalWrite(PNB_PIN, HIGH); break; - case 4: - digitalWrite(PA_PIN,LOW); - digitalWrite(PNA_PIN,HIGH); - digitalWrite(PB_PIN,LOW); - digitalWrite(PNB_PIN,HIGH); + case 2: + digitalWrite(PA_PIN, HIGH); + digitalWrite(PNA_PIN, LOW); + digitalWrite(PB_PIN, HIGH); + digitalWrite(PNB_PIN, LOW); + break; + case 3: + digitalWrite(PA_PIN, LOW); + digitalWrite(PNA_PIN, HIGH); + digitalWrite(PB_PIN, HIGH); + digitalWrite(PNB_PIN, LOW); + break; + case 4: + digitalWrite(PA_PIN, LOW); + digitalWrite(PNA_PIN, HIGH); + digitalWrite(PB_PIN, LOW); + digitalWrite(PNB_PIN, HIGH); break; - } } -uint8_t feed_pitch1(uint64_t lines, uint8_t forward_backward) -{ +uint8_t feed_pitch1(uint64_t lines, uint8_t forward_backward) { uint8_t pos = current_pos; - uint8_t restor = ~forward_backward; - + uint8_t restor = ~forward_backward; + restor &= 0x01; - - if(lines>0) - { + + if (lines > 0) { MOTOR_ENABLE1; MOTOR_ENABLE2; ENABLE_VH; - while(lines>0) - { - motor_stepper_pos2(pos); /* 0.0625mm */ - - if(pos >= 1 && pos <= 4) - pos = pos + (1 - 2*forward_backward); // adding or subtracting - if(pos < 1 || pos > 4) - pos = pos + (4 - 8*restor); // restoring pos + while (lines > 0) { + motor_stepper_pos2(pos); /* 0.0625mm */ + + if (pos >= 1 && pos <= 4) + pos = pos + (1 - 2 * forward_backward); // adding or subtracting + if (pos < 1 || pos > 4) + pos = pos + (4 - 8 * restor); // restoring pos lines--; } MOTOR_DISABLE1; MOTOR_DISABLE2; DISABLE_VH; - - } - else - { + + } else { return ERROR_FEED_PITCH; } current_pos = pos; return 0; - } -void print_dots_8bit_split(CONFIG*cfg,uint8_t *Array, uint8_t characters) -{ - uint8_t i=0,y=0, MAX=MAXPIXELS; +void print_dots_8bit_split(CONFIG *cfg, uint8_t *Array, uint8_t characters) { + uint8_t i = 0, y = 0, MAX = MAXPIXELS; uint8_t blank; uint16_t pts; uint8_t temp[MAXPIXELS]; uint8_t _array[MAXPIXELS]; pts = 0; - memcpy(_array,Array,MAXPIXELS); - - while( (i< characters) && (i < MAX)) { + memcpy(_array, Array, MAXPIXELS); + + while ((i < characters) && (i < MAX)) { pts = pts + bits_number(Array[i]); - - if(pts > cfg->max_pts) { - memset(temp,0,MAXPIXELS); - memcpy(temp,_array,i); - print_dots_8bit(cfg,temp,characters,0); + + if (pts > cfg->max_pts) { + memset(temp, 0, MAXPIXELS); + memcpy(temp, _array, i); + print_dots_8bit(cfg, temp, characters, 0); pts = bits_number(_array[i]); - memset(_array,0,i); - }else if(pts==cfg->max_pts) { - memset(temp,0,MAXPIXELS); - memcpy(temp,_array,i+1); - print_dots_8bit(cfg,temp,characters,0); - pts=0; - memset(_array,0,i+1); + memset(_array, 0, i); + } else if (pts == cfg->max_pts) { + memset(temp, 0, MAXPIXELS); + memcpy(temp, _array, i + 1); + print_dots_8bit(cfg, temp, characters, 0); + pts = 0; + memset(_array, 0, i + 1); } i++; } - if(pts >0){ - print_dots_8bit(cfg,_array,characters,0); + if (pts > 0) { + print_dots_8bit(cfg, _array, characters, 0); pts = 0; } - feed_pitch1(cfg->feed_pitch,cfg->orient); + feed_pitch1(cfg->feed_pitch, cfg->orient); return; } -void print_dots_8bit(CONFIG*cfg,uint8_t *Array, uint8_t characters,uint8_t feed_num) -{ - uint8_t i=0,y=0, MAX=MAXPIXELS; +void print_dots_8bit(CONFIG *cfg, uint8_t *Array, uint8_t characters, + uint8_t feed_num) { + uint8_t i = 0, y = 0, MAX = MAXPIXELS; uint8_t blank; - - ENABLE_VH; - if(cfg->align == 0) { - while((ialign==1){// center - blank = 0; - blank = (MAX-characters)/2; - - for(i=0;ialign==2){ - blank = MAX-characters; - for(i=0;idensity*46); - digitalWrite(STBx[y],LOW); - delayus(14); - i++; - } - - y++; - } + if (cfg->align == 0) { + while ((i < characters) && (i < MAX)) { + printer_send_data8(Array[i]); + i++; + } + while (i < MAX) { + printer_send_data8(0x00); + i++; + } + } else if (cfg->align == 1) { // center + blank = 0; + blank = (MAX - characters) / 2; + for (i = 0; i < blank; i++) { + printer_send_data8(0x00); + } + for (i = 0; i < characters; i++) { + printer_send_data8(Array[i]); + } + for (i = 0; i < (MAX - characters - blank); i++) { + printer_send_data8(0x00); + } + } else if (cfg->align == 2) { + blank = MAX - characters; + for (i = 0; i < blank; i++) { + printer_send_data8(0x00); + } + for (i = 0; i < characters; i++) { + printer_send_data8(Array[i]); + } + } - feed_pitch1(feed_num,cfg->orient); + LATCH_ENABLE; + delayus(1); + LATCH_DISABLE; + delayMicroseconds(1); - - DISABLE_VH; + i = 0; - return; + while (y < STB_NUMBER) { + + while (i < 10) { + + digitalWrite(STBx[y], HIGH); + delayus(HEAT_TIME + cfg->density * 46); + digitalWrite(STBx[y], LOW); + delayus(14); + i++; + } + + y++; + } + + feed_pitch1(feed_num, cfg->orient); + + DISABLE_VH; + + return; } -uint16_t read_adc(char*adc_file) { +uint16_t read_adc(char *adc_file) { long ret; char c[16]; FILE *fptr; @@ -363,275 +340,466 @@ uint16_t read_adc(char*adc_file) { return 0; } fscanf(fptr, "%[^\n]", c); - //printf("Data from the file:\n%s", c); + // printf("Data from the file:\n%s", c); fclose(fptr); ret = strtol(c, NULL, 10); - //printf("the number ret %d\n",ret); + // printf("the number ret %d\n",ret); return (uint16_t)ret; } - uint16_t temperature() { - - + double Rthermistor = 0, TempThermistor = 0; - uint16_t ADCSamples=0; + uint16_t ADCSamples = 0; int Sample = 1; uint16_t ADCConvertedValue; - - while(Sample<=NumSamples) - { - //ADCSamples += analogRead(THERMISTORPIN); //stm32 - ADCSamples += read_adc(adc_file_path); - Sample++; + while (Sample <= NumSamples) { + // ADCSamples += analogRead(THERMISTORPIN); //stm32 + ADCSamples += read_adc(adc_file_path); + Sample++; } - //Thermistor Resistance at x Kelvin - ADCConvertedValue = (double)ADCSamples/NumSamples; - Rthermistor = ( (double)ADCResolution/ ADCConvertedValue) - 1; - Rthermistor = (double)SeriesResistor/Rthermistor; - //Thermistor temperature in Kelvin - TempThermistor = Rthermistor / RthNominal ; - TempThermistor = log(TempThermistor); + // Thermistor Resistance at x Kelvin + ADCConvertedValue = (double)ADCSamples / NumSamples; + Rthermistor = ((double)ADCResolution / ADCConvertedValue) - 1; + Rthermistor = (double)SeriesResistor / Rthermistor; + // Thermistor temperature in Kelvin + TempThermistor = Rthermistor / RthNominal; + TempThermistor = log(TempThermistor); TempThermistor /= BCoefficent; - TempThermistor += (1/(TempNominal + 273.15)); - TempThermistor = 1/TempThermistor; + TempThermistor += (1 / (TempNominal + 273.15)); + TempThermistor = 1 / TempThermistor; - return (uint16_t)(TempThermistor - 273.15); - - - //return (uint16_t)(0); + return (uint16_t)(TempThermistor - 273.15); + + // return (uint16_t)(0); } -int glob_file(char*av) { +int glob_file(char *av) { glob_t globlist; - if (glob(av, GLOB_PERIOD|GLOB_NOSORT, NULL, &globlist) == GLOB_NOSPACE || glob(av, GLOB_PERIOD|GLOB_NOSORT, NULL, &globlist) == GLOB_NOMATCH) + if (glob(av, GLOB_PERIOD | GLOB_NOSORT, NULL, &globlist) == GLOB_NOSPACE || + glob(av, GLOB_PERIOD | GLOB_NOSORT, NULL, &globlist) == GLOB_NOMATCH) return -1; - if (glob(av, GLOB_PERIOD|GLOB_NOSORT, NULL, &globlist) == GLOB_ABORTED) + if (glob(av, GLOB_PERIOD | GLOB_NOSORT, NULL, &globlist) == GLOB_ABORTED) return 1; - if(globlist.gl_pathc > 0) { - strcpy(adc_file_path,globlist.gl_pathv[0]); + if (globlist.gl_pathc > 0) { + strcpy(adc_file_path, globlist.gl_pathv[0]); } return 0; - } - #endif +uint16_t get_serial_cache_font_width(CONFIG *cfg) { -uint8_t print_lines8(CONFIG*cfg) { - uint8_t i,j,k; + int i; + uint8_t *ch; + uint32_t codename; + int w; + w = 0; + i = 0; + while (i < ser_cache.idx) { + ch = (uint8_t *)&ser_cache.data[i]; + codename = utf8_to_utf32(ch); + FT_UInt gi = FT_Get_Char_Index(cfg->face, codename); + FT_Load_Glyph(cfg->face, gi, FT_LOAD_NO_BITMAP); + w += cfg->face->glyph->metrics.horiAdvance / 64; + i++; + } + return w + cfg->font->width; +} + +// print with freetype font dots glyph +uint8_t print_lines_ft(CONFIG *cfg) { + uint8_t i, j, k; int8_t w; - uint8_t *data; - uint8_t row,pad; - uint16_t addr; - - uint16_t line_bits; - uint8_t dot_line_data[MAXPIXELS]; - uint8_t dot_line_idx=0; - uint8_t dot_line_bitsidx=0; + uint8_t dot_line_idx = 0; + uint8_t dot_line_bitsidx = 0; - uint8_t lastidx,lastw,lastj; - int8_t left; + uint8_t lastidx, lastw, lastj; + uint8_t row, row_cnt; + uint16_t line_bits; + + int8_t left = ser_cache.idx; uint8_t rv; - pad = current_font.width %BITS8; - - if(pad > 0){ - pad = 1; - } + line_bits = cfg->margin.width; + dot_line_idx = line_bits / 8; + dot_line_bitsidx = line_bits % 8; - i = 0; - i = current_font.width/BITS8; - - pad = i+pad; - - row = 0; - rv = IsPaper(); - - data = (uint8_t*)malloc(sizeof(uint8_t)*(pad+1)); - i=0; + lastidx = 0; + lastw = 0; + lastj = 0; - - line_bits=cfg->margin.width; - - dot_line_idx = line_bits/8; - dot_line_bitsidx = line_bits%8; - left = ser_cache.idx; - lastidx=0; - lastw=0; - lastj=0; + uint32_t codename; + uint8_t *ch; + printf("left = %d\n", left); + int line_height = (cfg->face->size->metrics.ascender - + cfg->face->size->metrics.descender) >> + 6; + int baseline_height = + abs(cfg->face->descender) * current_font.height / cfg->face->units_per_EM; + int dpx = 64; + FT_Matrix matrix; - //DEBUG("left",left); - while(left>0){ + while (left > 0) { i = lastidx; - while(rowmargin.width; - dot_line_idx = line_bits/8; - dot_line_bitsidx = line_bits%8; - memset(dot_line_data,0,MAXPIXELS); + row_cnt = 0; + row = 0; + while (row < line_height) { + line_bits = cfg->margin.width; + dot_line_idx = line_bits / 8; + dot_line_bitsidx = line_bits % 8; + memset(dot_line_data, 0, MAXPIXELS); + // line by line bitmap dots to print i = lastidx; - //DEBUG("i",i) - //DEBUG("ser_cache.idx",ser_cache.idx) - while( i degree / 360) * 3.14159 * 2) * + 0x10000L); + matrix.xy = (FT_Fixed)(-sin(((double)cfg->degree / 360) * 3.14159 * 2) * + 0x10000L); + matrix.yx = (FT_Fixed)(sin(((double)cfg->degree / 360) * 3.14159 * 2) * + 0x10000L); + matrix.yy = (FT_Fixed)(cos(((double)cfg->degree / 360) * 3.14159 * 2) * + 0x10000L); + FT_Set_Transform(cfg->face, &matrix, NULL); + + FT_UInt gi = FT_Get_Char_Index(cfg->face, codename); + FT_Load_Glyph(cfg->face, gi, FT_LOAD_DEFAULT); + int y_off = line_height - baseline_height - + cfg->face->glyph->metrics.horiBearingY / dpx; + int glyph_width = cfg->face->glyph->metrics.width / dpx; + int glyph_height = cfg->face->glyph->metrics.height / dpx; + int advance = cfg->face->glyph->metrics.horiAdvance / dpx; + + int x_off = (advance - glyph_width) / 2; + + int bitmap_rows = cfg->face->glyph->bitmap.rows; + int bitmap_width = cfg->face->glyph->bitmap.width; + // FT_Render_Glyph(cfg->face->glyph, FT_RENDER_MODE_NORMAL); + FT_Render_Glyph(cfg->face->glyph, FT_RENDER_MODE_MONO); // disable AA + + j = 0; + w = 0; + if (lastj != 0) { + j = lastj; } - j=0; w=0; - if(lastj !=0){j= lastj;} - if(lastw !=0) { w = lastw;} - - while(w < current_font.width){ - if(w > 0 && ( w%8) == 0)j++; - if(dot_line_bitsidx > 7){ - dot_line_idx++; - dot_line_bitsidx=0; - } - - k = (data[j] >> (7-(w%8))) &1; - //Serial.print(data[j],HEX); - if( k > 0){ - dot_line_data[dot_line_idx] |= 1 << (7-dot_line_bitsidx); - //Serial.print("1"); - } - - dot_line_bitsidx++; - w++; - line_bits++; - if(line_bits >= MAX_DOTS)break; + if (lastw != 0) { + w = lastw; + } + while (w < advance) { + // if(w > 0 && (w%8) == 0) j++; + if (dot_line_bitsidx > 7) { + dot_line_idx++; + dot_line_bitsidx = 0; } - ///word gap - k=0; - while( k < cfg->wordgap ){ - if(dot_line_bitsidx > 7){ - dot_line_idx++; - dot_line_bitsidx=0; - } + // unsigned char p = cfg->face->glyph->bitmap.buffer[row * + // cfg->face->glyph->bitmap.pitch + w]; + unsigned char p = 0; + int pitch = abs(cfg->face->glyph->bitmap.pitch); - k++; - dot_line_bitsidx++; - line_bits++; - if(line_bits >= MAX_DOTS)break; + if (w >= x_off && row >= y_off) { + row_cnt = row - y_off; + if (row_cnt < bitmap_rows) { + // p = + // (cfg->face->glyph->bitmap.buffer[row_cnt*cfg->face->glyph->bitmap.pitch+j] + // >> (7-w%8)) & 1;//disable AA + j = (w - x_off) / 8; + p = cfg->face->glyph->bitmap.buffer[row_cnt * pitch + j]; + p = p & (128 >> ((w - x_off) & 7)); + } } - - if(line_bits < MAX_DOTS){ + + if (p) { + printf("#"); + dot_line_data[dot_line_idx] |= 1 << (7 - dot_line_bitsidx); + } else { + printf("0"); + } + + dot_line_bitsidx++; + w++; + line_bits++; + if (line_bits >= MAX_DOTS) + break; + } + // word gap + k = 0; + while (k < cfg->wordgap) { + if (dot_line_bitsidx > 7) { + dot_line_idx++; + dot_line_bitsidx = 0; + } + k++; + dot_line_bitsidx++; + line_bits++; + if (line_bits >= MAX_DOTS) + break; + } + + if (line_bits < MAX_DOTS) { i++; } - - if(line_bits >= MAX_DOTS || i >=ser_cache.idx){ - - if(row == (current_font.height-1)) {// last of the row loop - if(w >= current_font.width){ - lastidx = i+1; - lastw =0; - lastj =0; - }else { + + if (line_bits >= MAX_DOTS || i >= ser_cache.idx) { + + if (row == (line_height - 1)) { // last of the row loop + if (w >= advance) { + lastidx = i + 1; + lastw = 0; + lastj = 0; + } else { lastidx = i; lastw = w; lastj = j; } } - + break; } } rv = IsPaper(); - if( rv == IS_PAPER){ - //DEBUG("dot_line_idx",dot_line_idx); - //DEBUG("dot_line_bits",dot_line_bitsidx); - print_dots_8bit_split(cfg,dot_line_data,dot_line_idx+1); - } + if (rv == IS_PAPER) { + // DEBUG("dot_line_idx",dot_line_idx); + // DEBUG("dot_line_bits",dot_line_bitsidx); + print_dots_8bit_split(cfg, dot_line_data, dot_line_idx + 1); + } + row++; + printf("\n"); + } + left = left - lastidx; + row = 0; + /* + if(cfg->line_space > cfg->font->height){ + feed_pitch1(cfg->line_space - cfg->font->height,cfg->orient); + } + */ + } +} + +uint8_t print_lines8(CONFIG *cfg) { + + if (cfg->font->mode == FONT_MODE_1) { + return print_lines_ft(cfg); + } + uint8_t i, j, k; + int8_t w; + uint8_t *data; + uint8_t row, pad; + uint16_t addr; + + uint16_t line_bits; + + uint8_t dot_line_data[MAXPIXELS]; + uint8_t dot_line_idx = 0; + uint8_t dot_line_bitsidx = 0; + + uint8_t lastidx, lastw, lastj; + int8_t left; + uint8_t rv; + + pad = current_font.width % BITS8; + + if (pad > 0) { + pad = 1; + } + + i = 0; + i = current_font.width / BITS8; + + pad = i + pad; + + row = 0; + rv = IsPaper(); + + data = (uint8_t *)malloc(sizeof(uint8_t) * (pad + 1)); + i = 0; + + line_bits = cfg->margin.width; + + dot_line_idx = line_bits / 8; + dot_line_bitsidx = line_bits % 8; + left = ser_cache.idx; + lastidx = 0; + lastw = 0; + lastj = 0; + + // DEBUG("left",left); + while (left > 0) { + i = lastidx; + while (row < current_font.height) { + + line_bits = cfg->margin.width; + dot_line_idx = line_bits / 8; + dot_line_bitsidx = line_bits % 8; + memset(dot_line_data, 0, MAXPIXELS); + i = lastidx; + // DEBUG("i",i) + // DEBUG("ser_cache.idx",ser_cache.idx) + while (i < ser_cache.idx) { + addr = pad * (uint8_t)ser_cache.data[i] * current_font.height; + for (j = 0; j < pad; j++) { + data[j] = current_font.data[addr + row * pad + j]; + } + j = 0; + w = 0; + if (lastj != 0) { + j = lastj; + } + if (lastw != 0) { + w = lastw; + } + + while (w < current_font.width) { + if (w > 0 && (w % 8) == 0) + j++; + if (dot_line_bitsidx > 7) { + dot_line_idx++; + dot_line_bitsidx = 0; + } + + k = (data[j] >> (7 - (w % 8))) & 1; + // Serial.print(data[j],HEX); + if (k > 0) { + dot_line_data[dot_line_idx] |= 1 << (7 - dot_line_bitsidx); + // Serial.print("1"); + } + + dot_line_bitsidx++; + w++; + line_bits++; + if (line_bits >= MAX_DOTS) + break; + } + + /// word gap + k = 0; + while (k < cfg->wordgap) { + if (dot_line_bitsidx > 7) { + dot_line_idx++; + dot_line_bitsidx = 0; + } + + k++; + dot_line_bitsidx++; + line_bits++; + if (line_bits >= MAX_DOTS) + break; + } + + if (line_bits < MAX_DOTS) { + i++; + } + + if (line_bits >= MAX_DOTS || i >= ser_cache.idx) { + + if (row == (current_font.height - 1)) { // last of the row loop + if (w >= current_font.width) { + lastidx = i + 1; + lastw = 0; + lastj = 0; + } else { + lastidx = i; + lastw = w; + lastj = j; + } + } + + break; + } + } + rv = IsPaper(); + if (rv == IS_PAPER) { + // DEBUG("dot_line_idx",dot_line_idx); + // DEBUG("dot_line_bits",dot_line_bitsidx); + print_dots_8bit_split(cfg, dot_line_data, dot_line_idx + 1); + } row++; } left = left - lastidx; row = 0; - - if(cfg->line_space > cfg->font->height){ - feed_pitch1(cfg->line_space - cfg->font->height,cfg->orient); + + if (cfg->line_space > cfg->font->height) { + feed_pitch1(cfg->line_space - cfg->font->height, cfg->orient); } - } - - //Serial.println("print ever"); + + // Serial.println("print ever"); free(data); return rv; } - -uint8_t print_image8(CONFIG*cfg){ +uint8_t print_image8(CONFIG *cfg) { uint16_t height; - uint16_t x,y,addr; - + uint16_t x, y, addr; + uint8_t rv; uint8_t LinePixels[MAXPIXELS]; - uint8_t maxchars= PRINTER_BITS/8; - height = cfg->img->num / cfg->img->width; - y=0; + uint8_t maxchars = PRINTER_BITS / 8; + height = cfg->img->num / cfg->img->width; + y = 0; addr = 0; - + rv = IsPaper(); - while(y < height ) - { - x=0; - while( x < cfg->img->width ) - { - addr = x+y*cfg->img->width; + while (y < height) { + x = 0; + while (x < cfg->img->width) { + addr = x + y * cfg->img->width; - - if(cfg->img->revert_bits > 0)//LSB + if (cfg->img->revert_bits > 0) // LSB LinePixels[x] = invert_bit(cfg->img->cache[addr]); else LinePixels[x] = cfg->img->cache[addr]; - + x++; } rv = IsPaper(); - if( rv == IS_PAPER) { - print_dots_8bit_split(cfg,LinePixels,x); + if (rv == IS_PAPER) { + print_dots_8bit_split(cfg, LinePixels, x); } - - //feed_pitch1(FEED_PITCH,cfg->orient); + + // feed_pitch1(FEED_PITCH,cfg->orient); y++; } - //feed_pitch1(cfg->feed_pitch,cfg->orient); - cfg->img->need_print= 0; - + // feed_pitch1(cfg->feed_pitch,cfg->orient); + cfg->img->need_print = 0; + cfg->img->num = 0; cfg->img->idx = 0; cfg->img->width = 0; - + return rv; } -void print_cut_line(CONFIG*cfg){ - uint8_t bs,i; +void print_cut_line(CONFIG *cfg) { + uint8_t bs, i; + + bs = PRINTER_BITS / cfg->font->width; + bs -= 1; - bs= PRINTER_BITS/ cfg->font->width; - bs-=1; - reset_cmd(); - - for(i=0;i +#include "utf8-utils.h" + +// ----------------------------------------------------- utf8_surrogate_len --- +size_t +utf8_surrogate_len( const char* character ) +{ + size_t result = 0; + char test_char; + + if (!character) + return 0; + + test_char = character[0]; + + if ((test_char & 0x80) == 0) + return 1; + + while (test_char & 0x80) + { + test_char <<= 1; + result++; + } + + return result; +} + +// ------------------------------------------------------------ utf8_strlen --- +size_t +utf8_strlen( const char* string ) +{ + const char* ptr = string; + size_t result = 0; + + while (*ptr) + { + ptr += utf8_surrogate_len(ptr); + result++; + } + + return result; +} + +uint32_t +utf8_to_utf32( const char * character ) +{ + if( !character ) + { + return -1; + } + + if( ( character[0] & 0x80 ) == 0x0 ) + { + return character[0]; + } + + if( ( character[0] & 0xE0 ) == 0xC0 ) + { + return ( ( character[0] & 0x3F ) << 6 ) | ( character[1] & 0x3F ); + } + + if( ( character[0] & 0xF0 ) == 0xE0 ) + { + return ( ( character[0] & 0x1F ) << ( 6 + 6 ) ) | ( ( character[1] & 0x3F ) << 6 ) | ( character[2] & 0x3F ); + } + + if( ( character[0] & 0xF8 ) == 0xF0 ) + { + return ( ( character[0] & 0x0F ) << ( 6 + 6 + 6 ) ) | ( ( character[1] & 0x3F ) << ( 6 + 6 ) ) | ( ( character[2] & 0x3F ) << 6 ) | ( character[3] & 0x3F ); + } + + if( ( character[0] & 0xFC ) == 0xF8 ) + { + return ( ( character[0] & 0x07 ) << ( 6 + 6 + 6 + 6 ) ) | ( ( character[1] & 0x3F ) << ( 6 + 6 + 6 ) ) | ( ( character[2] & 0x3F ) << ( 6 + 6 ) ) | ( ( character[3] & 0x3F ) << 6 ) | ( character[4] & 0x3F ); + } + + return 0xFFFD; // invalid character +} diff --git a/Code/thermal_printer/utf8-utils.h b/Code/thermal_printer/utf8-utils.h new file mode 100644 index 0000000..a626201 --- /dev/null +++ b/Code/thermal_printer/utf8-utils.h @@ -0,0 +1,68 @@ +/* Freetype GL - A C OpenGL Freetype engine + * + * Distributed under the OSI-approved BSD 2-Clause License. See accompanying + * file `LICENSE` for more details. + */ +#ifndef __UTF8_UTILS_H__ +#define __UTF8_UTILS_H__ + +#include +#include + +#ifdef __cplusplus +extern "C" { + +namespace ftgl { +#endif + +/** + * @file utf8-utils.h + * @author Marcel Metz + * + * defgroup utf8-utils UTF-8 Utilities + * + * @{ + */ + + /** + * Returns the size in bytes of a given UTF-8 encoded character surrogate + * + * @param character An UTF-8 encoded character + * + * @return The length of the surrogate in bytes. + */ + size_t + utf8_surrogate_len( const char* character ); + + /** + * Return the length of the given UTF-8 encoded and + * NULL terminated string. + * + * @param string An UTF-8 encoded string + * + * @return The length of the string in characters. + */ + size_t + utf8_strlen( const char* string ); + + /** + * Converts a given UTF-8 encoded character to its UTF-32 LE equivalent + * + * @param character An UTF-8 encoded character + * + * @return The equivalent of the given character in UTF-32 LE + * encoding. + */ + uint32_t + utf8_to_utf32( const char * character ); + +/** + * @} + */ + +#ifdef __cplusplus +} +} +#endif + +#endif /* #define __UTF8_UTILS_H__ */ diff --git a/Code/thermal_printer/utils.c b/Code/thermal_printer/utils.c index bebb80d..32c0597 100644 --- a/Code/thermal_printer/utils.c +++ b/Code/thermal_printer/utils.c @@ -1,24 +1,20 @@ #include "utils.h" -void delayus(unsigned int _us){ - delayMicroseconds(_us); +void delayus(unsigned int _us) { delayMicroseconds(_us); } + +uint8_t invert_bit(uint8_t a) { + + return ((a & 0x01) << 7) | ((a & 0x02) << 5) | ((a & 0x04) << 3) | + ((a & 0x08) << 1) | ((a & 0x10) >> 1) | ((a & 0x20) >> 3) | + ((a & 0x40) >> 5) | ((a & 0x80) >> 7); } -uint8_t invert_bit(uint8_t a){ - - return ((a&0x01)<<7)|((a&0x02)<<5)|((a&0x04)<<3)|((a&0x08)<<1)|((a&0x10)>>1)|((a&0x20)>>3)|((a&0x40)>>5)|((a&0x80)>>7); - +uint8_t bits_number(uint8_t n) // count bits "1" +{ + uint8_t count = 0; + while (n) { + count += n & 1; + n >>= 1; + } + return count; } - -uint8_t bits_number(uint8_t n)//count bits "1" -{ - uint8_t count = 0; - while (n) { - count += n & 1; - n >>= 1; - } - return count; -} - - - diff --git a/Code/thermal_printer/utils.h b/Code/thermal_printer/utils.h index 605b11c..42477b6 100644 --- a/Code/thermal_printer/utils.h +++ b/Code/thermal_printer/utils.h @@ -1,19 +1,26 @@ #ifndef UTILS_H #define UTILS_H +#include #include #include -#include #include #include #define SEP printf(" "); // a is string, b is number -#define DEBUG(a,b) printf(a);SEP;printf("%d\n",b); +#define DEBUG(a, b) \ + printf(a); \ + SEP; \ + printf("%d\n", b); #define ALINE printf("\n"); -#define PRINTF(...) { printf(__VA_ARGS__);fflush(stdout);} +#define PRINTF(...) \ + { \ + printf(__VA_ARGS__); \ + fflush(stdout); \ + } void delayus(unsigned int _us); @@ -21,5 +28,4 @@ uint8_t invert_bit(uint8_t a); uint8_t bits_number(uint8_t n); - #endif