and the luaprinter code is actually pretty short:

Code

#ifndef MAME_LUAPRINTER_H
#define MAME_LUAPRINTER_H

#pragma once

#include "emu.h"
#include "png.h"
#include "emuopts.h"

class luaprinter {

public:

	int initvalue;
	device_t * m_lp_mydevice;
	luaprinter(device_t & thisdevice, int testvalue);

	static std::vector<luaprinter *> luaprinterlist;

	const static int BUFFERSIZE = 128000;
	std::array<unsigned char, BUFFERSIZE>  m_printerbuffer;
	int m_lp_head = 0;
	int m_lp_tail = 0;
	bitmap_rgb32 *m_lp_bitmap;  // pointer to bitmap
	int m_xpos = 0;
	int m_ypos = 0;
	std::string m_luaprintername;
	int m_page_count;
	static time_t m_lp_session_time;

	void addtolist(luaprinter & myprinter){ luaprinterlist.emplace_back(& myprinter);}
	void setprintername(std::string name){ m_luaprintername = name; }
	std::string getprintername(){ return m_luaprintername; }

	int getinitvalue(){ return initvalue; }

	void clearpage(){ m_lp_bitmap->fill(0xffffff);};
	void drawpixel(int x, int y, int pixelval) { m_lp_bitmap->pix32(y,x) = pixelval; };
	int getpixel(int x, int y){return m_lp_bitmap->pix32(y,x);};
	void setheadpos(int x, int y){m_xpos=x;m_ypos=y;};

	int pagewidth() { return m_lp_bitmap->width(); }
	int pageheight() { return m_lp_bitmap->height(); }

	device_t* getrootdev();
	void savepage();
	
	std::tuple<std::array<unsigned char,BUFFERSIZE>&,int,int>  getbuffer() { return std::make_tuple(std::ref(m_printerbuffer),m_lp_head,m_lp_tail);};   
	int getnextchar();
	void putnextchar(int c);
	void register_bitmap(bitmap_rgb32 &mybitmap){m_lp_bitmap = &mybitmap;}
	std::tuple<int,int>getheadpos() { return std::make_tuple(m_xpos,m_ypos); };
	int count(){ return luaprinterlist.size();}
	std::string fixcolons(std::string in);
	std::string sessiontime();
	std::string tagname();
	std::string simplename();
};


#endif  // MAME_LUAPRINTER_H

luaprinter.cpp:
Code

#include "luaprinter.h"

luaprinter::luaprinter(device_t& thisdevice, int testvalue){ 
	m_lp_mydevice = &thisdevice; 
	time(&m_lp_session_time);  
	initvalue = testvalue;
	printf("yes you can");
	fflush(stdout);
	
	};

int luaprinter::getnextchar() {
	if (m_lp_head==m_lp_tail) return -1; 
	else {
		int retval = m_printerbuffer.at(m_lp_tail++); 
		m_lp_tail %= BUFFERSIZE; 
		return retval;
	}
}

void luaprinter::putnextchar(int c) {
	m_printerbuffer.at(m_lp_head++)=c; 
	m_lp_head %= BUFFERSIZE;
}

std::string luaprinter::fixcolons(std::string in) {
    std::string final;
    for(std::string::const_iterator it = in.begin(); it != in.end(); ++it)
    {
        if((*it) != ':')
        {
            final += *it;
        }
        else final += '-';
    }
    return final;
}

std::string luaprinter::sessiontime() {
   struct tm *info;
   char buffer[120];
   info = localtime( &m_lp_session_time );
   strftime(buffer,120,"%Y-%m-%d %H-%M-%S", info);
   return std::string(buffer);
}

std::string luaprinter::tagname() {
	device_t * dev;
	device_t * lastdev;
	dev = m_lp_mydevice;
	std::string s(dev->owner()->shortname());
	while (dev) {
		lastdev = dev;
		dev=dev->owner();
	}
	lastdev=lastdev; // complains about non use
	return fixcolons(std::string(lastdev->shortname())+std::string(m_lp_mydevice->tag()));
}

std::string luaprinter::simplename() {
	device_t * dev;
	device_t * lastdev;
	dev = m_lp_mydevice;
	std::string s(dev->owner()->shortname());
	while (dev){
		s=std::string(dev->shortname())+std::string(" ")+s;
		lastdev = dev;
		dev=dev->owner();
	}
	lastdev=lastdev; // complains about non use
	return s;
}

device_t* luaprinter::getrootdev(){
	device_t* dev;
	device_t* lastdev = NULL;
	dev = m_lp_mydevice;
	while (dev){
		lastdev = dev;
		dev=dev->owner();
	}
	lastdev=lastdev; // complains about non use
	return lastdev;
}

void luaprinter::savepage(){
    emu_file file(std::string(m_lp_mydevice->machine().options().snapshot_directory()) + std::string("/") +
				std::string(getrootdev()->shortname() ) + std::string("/"), 
				OPEN_FLAG_WRITE | OPEN_FLAG_CREATE | OPEN_FLAG_CREATE_PATHS);
    auto const filerr = file.open(std::string(getprintername())+" Page "+std::to_string(m_page_count++)+".PNG");

    if (filerr == osd_file::error::NONE)
        {
            static const rgb_t png_palette[] = { rgb_t::white(), rgb_t::black() };
            // save the paper into a png
            png_write_bitmap(file, nullptr, (bitmap_t &) (* m_lp_bitmap), 2, png_palette);
        }
}



in luaengine.cpp:
Code
        auto luaprinter_type = sol().registry().create_simple_usertype<luaprinter>("new", sol::no_constructor);
        luaprinter_type.set("clearpage", &luaprinter::clearpage);
        luaprinter_type.set("drawpixel", &luaprinter::drawpixel);
        luaprinter_type.set("getpixel", &luaprinter::getpixel);
        luaprinter_type.set("setheadpos", &luaprinter::setheadpos);
        luaprinter_type.set("savepage", &luaprinter::savepage);
        luaprinter_type.set("getbuffer", &luaprinter::getbuffer);
        luaprinter_type.set("getnextchar", &luaprinter::getnextchar);
        luaprinter_type.set("putnextchar", &luaprinter::putnextchar);
        luaprinter_type.set("getheadpos", &luaprinter::getheadpos);
        luaprinter_type.set("setprintername", &luaprinter::setprintername);
        luaprinter_type.set("getprintername", &luaprinter::getprintername);
        luaprinter_type.set("fixcolons", &luaprinter::fixcolons);
        luaprinter_type.set("sessiontime", &luaprinter::sessiontime);
        luaprinter_type.set("simplename", &luaprinter::simplename);
        luaprinter_type.set("tagname", &luaprinter::tagname);
        luaprinter_type.set("count", &luaprinter::count);
        luaprinter_type.set("getinitvalue", &luaprinter::getinitvalue);
        luaprinter_type.set("pagewidth", &luaprinter::pagewidth);
        luaprinter_type.set("pageheight", &luaprinter::pageheight);

	sol().registry().set_usertype("luaprinter", luaprinter_type);




         machine_type.set("lp", sol::property([this](running_machine &m) {
                        sol::table table = sol().create_table();
                        int i=1;
                        for(auto p : luaprinter::luaprinterlist)
                        {
                        	table[i++] = p;
                        };
                        return table;
                }));